Announcement

Collapse
No announcement yet.

Coloring Controls

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Coloring Controls

    Below is a variation on the code Semen Matusovski publish a few
    weeks ago on coloring buttons. It works well, but I'd like to
    add one generalization to it:

    From inside the CALLBACK function I'd like to
    get access to the string declared on the DDT
    CONTROL ADD BUTTON call. In this case either
    the string "11111" or "22222".

    I've looked thru the DRAWITEMSTRUCT definitions and find no
    link to the string the button created.

    Does anyone know this information, or perhaps know the fact
    that it is NOT "visible" from inside the CALLBACK function?
    ---------------------------------------------------------------
    #COMPILE EXE
    #REGISTER NONE
    #INCLUDE "Win32Api.Inc"
    %ID_Button=301
    GLOBAL BrushRed AS LONG

    CALLBACK FUNCTION hDlg_CB
    SELECT CASE CBMSG
    CASE %WM_DRAWITEM
    DIM t AS DRAWITEMSTRUCT PTR
    t=CBLPARAM '**** DONT'T UNDERSTAND HOW THIS WORKs *****
    x&[email protected]@t.rcItem.nleft
    y&[email protected]@t.rcItem.ntop
    'Fill area with Red and frame it with black
    CALL FillRect(@t.hDc,@t.rcItem,BrushRed)
    CALL FrameRect(@t.hDc,@t.rcItem,GetStockObject(%BLACK_BRUSH))
    SetBkMode @t.hDC,%TRANSPARENT

    'invert the tRectangle if the button is selected
    IF(@t.itemState AND %ODS_SELECTED) THEN
    CALL InvertRect(@t.hDC,@t.rcItem)
    SetTextColor @t.hDC,%BLACK
    ELSE
    SetTextColor @t.hDC,%WHITE
    END IF
    DrawText @t.hDC,"",-1,BYVAL VARPTR(@t.RcItem),%DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
    FUNCTION=%True
    END SELECT
    END FUNCTION

    FUNCTION PBMAIN() AS LONG
    DIM BrushRed AS LONG
    LOCAL hDlg AS LONG
    LOCAL Lb AS LOGBRUSH
    Lb.lbStyle=%BS_SOLID
    Lb.lbColor=%Red:BrushRed=CreateBrushIndirect(Lb)
    DIALOG NEW 0,"Color Button Example",0,0,105,120,%DS_CENTER OR %WS_SYSMENU TO hDlg
    CONTROL ADD BUTTON,hDlg,%ID_Button+0,"11111",10,10,80,12,%WS_TABSTOP OR %BS_OWNERDRAW
    CONTROL ADD BUTTON,hDlg,%ID_Button+1,"22222",10,50,80,12,%WS_TABSTOP OR %BS_OWNERDRAW
    DIALOG SHOW MODAL hDlg CALL hDlg_CB
    END FUNCTION

  • #2
    Code:
    #Compile Exe
    #Register None
    #Include "Win32Api.Inc"
    %ID_Button=301
    Global BrushRed As Long
    
    CallBack Function hDlg_CB
    Static Txt$
    
      Select Case CbMsg
       Case %WM_DRAWITEM
        Dim t As DRAWITEMSTRUCT Ptr
        t=CbLparam
        x&[email protected]@t.rcItem.nleft
        y&[email protected]@t.rcItem.ntop
        'Fill area with Red and frame it with black
        Call FillRect(@t.hDc,@t.rcItem,BrushRed)
        Call FrameRect(@t.hDc,@t.rcItem,GetStockObject(%BLACK_BRUSH))
        SetBkMode @t.hDC,%TRANSPARENT
    
    'invert the tRectangle if the button is selected
        If(@t.itemState And %ODS_SELECTED) Then
         Call InvertRect(@t.hDC,@t.rcItem)
         SetTextColor @t.hDC,%BLACK
        Else
         SetTextColor @t.hDC,%WHITE
        End If
        Control Get Text CbHndl,CbCtl To txt$
        DrawText @t.hDC,ByVal StrPtr(txt$),-1,ByVal VarPtr(@t.RcItem),%DT_SINGLELINE Or %DT_CENTER Or %DT_VCENTER
        Function=%True
      End Select
    End Function
    
    Function PbMain() As Long
    Local hDlg As Long
    Local Lb As LOGBRUSH
      Lb.lbStyle=%BS_SOLID
      Lb.lbColor=%Red:BrushRed=CreateBrushIndirect(Lb)
       Dialog New 0,"Color Button Example",0,0,105,120,%DS_CENTER Or %WS_SYSMENU To hDlg
       Control Add Button,hDlg,%ID_Button+0,"11111",10,10,80,12,%WS_TABSTOP Or %BS_OWNERDRAW
       Control Add Button,hDlg,%ID_Button+1,"22222",10,50,80,12,%WS_TABSTOP Or %BS_OWNERDRAW
       Dialog Show Modal hDlg Call hDlg_CB
    End Function
    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se



    [This message has been edited by Fred Oxenby (edited February 05, 2000).]
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Comment


    • #3
      Thanks Fred! I was looking for the information as part of the
      button's structure. Forgot about the CONTROL calls.

      I found however that for combo boxes, the return of the value
      to text$ is 4 bytes. It looks like a pointer, but I'm not sure
      what it is pointing at, or how to feed it as the third
      parameter to DRAWTEXT which expects a string.

      Anyone have any advice? My test program with one button and one
      combo box is below.
      -----------------------------------------------------------------
      #COMPILE EXE
      #REGISTER NONE
      #INCLUDE "Win32Api.Inc"
      %Button=301:%Combo=401
      GLOBAL Listing() AS STRING
      GLOBAL switch() AS LONG
      GLOBAL hDlg AS LONG

      CALLBACK FUNCTION CB
      DIM tDrawItem AS DRAWITEMSTRUCT PTR
      DIM BrushLtBr AS STATIC LONG, BrushWhite AS STATIC LONG, BrushBlue AS STATIC LONG, BrushRed AS STATIC LONG
      tDrawItem = CBLPARAM
      SELECT CASE CBMSG
      CASE %WM_INITDIALOG
      LOCAL Lb AS LOGBRUSH
      Lb.lbStyle = %BS_SOLID
      Lb.lbColor = &H80C0FF: BrushLtBr = CreateBrushIndirect(Lb)
      Lb.lbColor = %White : BrushWhite = CreateBrushIndirect(Lb)
      Lb.lbColor = %Blue : BrushBlue = CreateBrushIndirect(Lb)
      Lb.lbColor = %Red : BrushRed = CreateBrushIndirect(Lb)
      FUNCTION = %TRUE

      CASE %WM_DRAWITEM
      x&[email protected]@tDrawItem.rcItem.nleft
      y&[email protected]@tDrawItem.rcItem.ntop
      'Fill area with white and frame it with black
      CALL FillRect(@tDrawItem.hDc,@tDrawItem.rcItem,BrushRed)
      CALL FrameRect(@tDrawItem.hDc,@tDrawItem.rcItem,GetStockObject(%BLACK_BRUSH))
      IF switch(CBCTL)=0 THEN INCR switch(CBCTL):_
      CONTROL GET TEXT CBHNDL,CBCTL TO t$:_
      MSGBOX STR$(CBCTL)+"|"+t$
      IF CBCTL=301 THEN
      SetBkMode @tDrawItem.hDC, %TRANSPARENT
      SetTextColor @tDrawItem.hDC, %WHITE
      DrawText @tDrawItem.hDC,BYCOPY t$,-1,BYVAL VARPTR(@tDrawItem.RcItem),%DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
      ELSE
      SetBkMode @tDrawItem.hDC, %TRANSPARENT
      SetTextColor @tDrawItem.hDC, %WHITE
      t$=Listing(1)+CHR$(0)+Listing(2)+CHR$(0)+Listing(3)+CHR$(0)
      DrawText @tDrawItem.hDC,BYCOPY t$,10,BYVAL VARPTR(@tDrawItem.RcItem),%DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
      END IF
      FUNCTION=%False
      END SELECT
      END FUNCTION

      FUNCTION PBMAIN()
      DIM switch(101:401) AS LONG
      DIM Listing(1:3) AS STRING:Listing(1)="aaaaaaaa":Listing(2)="bbbbbbbbb":Listing(3)="cccccccc":
      DIALOG NEW 0 ,"Text display Example",0,0, 105, 100, %DS_CENTER OR %WS_SYSMENU TO hDlg
      CONTROL ADD COMBOBOX,hDlg,%Combo,Listing(),9,60,82,50,%CBS_DROPDOWNLIST OR %WS_TABSTOP OR %CBS_OWNERDRAWVARIABLE
      CONTROL ADD BUTTON, hDlg, %Button, "This is the Button Text", 10, 40, 80, 12, %WS_TABSTOP OR %BS_OWNERDRAW
      COMBOBOX SELECT hDlg,%Combo,1
      DIALOG SHOW MODAL hDlg CALL CB
      END FUNCTION

      ------------------

      Comment

      Working...
      X