Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB/WIN - Tabbed text in ownerdrawn combobox

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

  • PB/WIN - Tabbed text in ownerdrawn combobox

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' OwnerDrawn ComboBox with Tabbed text.
    ' Public Domain by Borje Hagsten, January 2004.
    ' As always, use at own responsibility.
    '--------------------------------------------------------------------
    ' Save as CBTABS.BAS (whatever), compile and run..
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #COMPILE EXE
    '--------------------------------------------------------------------
    #INCLUDE "WIN32API.INC"   ' API declares
    '--------------------------------------------------------------------
    %ID_COMBO1 = 120          ' ComboBox id
    '--------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION DlgProc
    DECLARE FUNCTION DrawTabTextCombo (BYVAL hWnd AS DWORD, BYVAL wParam AS DWORD, _
                                       BYVAL lParam AS LONG) AS LONG
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Main entrance
    '--------------------------------------------------------------------
    FUNCTION PBMAIN
     
      LOCAL c AS LONG, hDlg AS DWORD
     
      DIALOG NEW 0, "OwnerDrawn tabbed text ComboBox", , , 150, 50, _
                 %WS_CAPTION OR %WS_SYSMENU TO hDlg
      '------------------------------------------------------------------
     
      CONTROL ADD COMBOBOX, hDlg, %ID_COMBO1, , 5, 5, 140, 120, _
                  %CBS_OWNERDRAWFIXED OR %CBS_HASSTRINGS OR %CBS_DROPDOWNLIST OR _
                  %WS_TABSTOP OR %WS_VSCROLL, %WS_EX_CLIENTEDGE
     
      CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close", 95, 32, 50, 14
      '------------------------------------------------------------------
     
      FOR c = 1 TO 20  ' Fill combobox with some tab delimited values
         COMBOBOX ADD hDlg, %ID_COMBO1, "Item" + STR$(c) + _
                                        $TAB + FORMAT$(c * 8, "0.00") + _
                                        $TAB + STR$(c * 16) + _
                                        $TAB + STR$(c * 32)
      NEXT
      COMBOBOX SELECT hDlg, %ID_COMBO1, 1  ' select first item
      '------------------------------------------------------------------
     
      DIALOG SHOW MODAL hDlg, CALL DlgProc
    END FUNCTION
     
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Main dialog callback
    '--------------------------------------------------------------------
    CALLBACK FUNCTION DlgProc
     
      SELECT CASE CBMSG
         CASE %WM_INITDIALOG   ' <- is sent right before dialog is shown.
     
         CASE %WM_COMMAND      ' <- a control is calling
            SELECT CASE CBCTL  ' <- look at control's id
               CASE %IDCANCEL  ' Cancel button or Escape key was pressed
                  IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                     DIALOG END CBHNDL  ' Close dialog
                  END IF
     
               CASE %ID_COMBO1  ' respond to ComboBox notifications
                  IF CBCTLMSG = %CBN_SELCHANGE THEN     ' on selection change
                     ' do whatever ..
     
                  ELSEIF CBCTLMSG = %CBN_SELENDOK THEN  ' user selected something
                     ' do whatever ..
     
                  END IF
     
            END SELECT
     
         CASE %WM_DESTROY  ' <- is sent when dialog is about to be destroyed (close)
     
         CASE %WM_DRAWITEM ' Pass this one on to DrawTabTextCombo procedure
            IF CBWPARAM = %ID_COMBO1 THEN
               FUNCTION = DrawTabTextCombo(GetDlgItem(CBHNDL, %ID_COMBO1), CBWPARAM, CBLPARAM)
            END IF
     
      END SELECT
     
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' WM_DRAWITEM procedure for OwnerDrawn ComboBox
    ' Note: OwnerDrawn means exactly what it says. The owner - you, need
    ' to draw everything via code. Please feel free to experiment here..
    '--------------------------------------------------------------------
    FUNCTION DrawTabTextCombo(BYVAL hWnd AS DWORD, BYVAL wParam AS DWORD, _
                              BYVAL lParam AS LONG) AS LONG
     
      LOCAL zTxt AS ASCIIZ * 300, lpDis AS DRAWITEMSTRUCT PTR
     
      lpDis = lParam
      IF @lpDis.itemID = &HFFFFFFFF& THEN EXIT FUNCTION 'empty list, take a break..
     
      SELECT CASE AS LONG @lpDis.itemAction
         CASE %ODA_DRAWENTIRE, %ODA_SELECT
            '--------------------------------------------------------------------
            ' CLEAR BACKGROUND
            '--------------------------------------------------------------------
            IF (@lpDis.itemState AND %ODS_SELECTED) = 0 THEN               ' if not selected
               SetBkColor @lpDis.hDC, GetSysColor(%COLOR_WINDOW)           ' text background
               SetTextColor @lpDis.hDC, GetSysColor(%COLOR_WINDOWTEXT)     ' text color
               FillRect @lpDis.hDC, @lpDis.rcItem, GetSysColorBrush(%COLOR_WINDOW) 'clear background
            ELSE
               SetBkColor @lpDis.hDC, GetSysColor(%COLOR_HIGHLIGHT)        'sel text background
               SetTextColor @lpDis.hDC, GetSysColor(%COLOR_HIGHLIGHTTEXT)  'sel text color
               FillRect @lpDis.hDC, @lpDis.rcItem, GetSysColorBrush(%COLOR_HIGHLIGHT) 'clear background
            END IF
     
            '--------------------------------------------------------------------
            ' GET TEXT
            '--------------------------------------------------------------------
            SendMessage hWnd, %CB_GETLBTEXT, @lpDis.itemID, VARPTR(zTxt)  ' Get text
     
            '--------------------------------------------------------------------
            ' DRAW TEXT, using TabbedTextOut
            '--------------------------------------------------------------------
            LOCAL xPos AS LONG, yPos AS LONG, tm AS TEXTMETRIC
     
            ' CALCULATE X-POS  (edit part and listbox differs a bit..)
            IF (@lpDis.itemState AND %ODS_COMBOBOXEDIT) THEN
               xPos = 6  ' if to draw in edit part (%ODS_COMBOBOXEDIT = &H1000)
            ELSE
               xPos = 3  ' listbox part
            END IF
     
            ' CALCULATE Y-POS  (vcenter in line)
            GetTextMetrics @lpDis.hDC, tm  ' we need character height
            yPos = @lpDis.rcItem.nTop + ((@lpDis.rcItem.nBottom - @lpDis.rcItem.nTop) - tm.tmHeight) \ 2
     
            ' SETUP TABSTOP POSITIONS
            ' Note: here each "column" is set to 60 pixels wide, and by using
            ' negative values, we get right-alignment - good for numeric values.
            ' If left-aligned text in "columns" is preferred, use positive values.
            REDIM tabpos(2) AS LONG  ' start at 120 pixels for text after first $TAB..
            tabpos(0) =  -120 : tabpos(1) = -180 : tabpos(2) = -240
     
            TabbedTextOut @lpDis.hDC, xPos, yPos, zTxt, LEN(zTxt), UBOUND(tabpos) + 1, tabpos(0), 0
            '--------------------------------------------------------------------
     
     
            '--------------------------------------------------------------------
            ' DRAW TEXT, using DrawText
            '--------------------------------------------------------------------
            ' Can use also DrawText with %DT_EXPANDTABS flag set, but then we
            ' get no right-aligned columns. Instead of the above, then use:
          '  DrawText @lpDis.hDC, zTxt, LEN(zTxt), @lpDis.rcItem, _
          '           %DT_SINGLELINE OR %DT_LEFT OR %DT_VCENTER OR %DT_EXPANDTABS
            '--------------------------------------------------------------------
     
     
            '--------------------------------------------------------------------
            ' DRAW FOCUS RECT AROUND SELECTED ITEM
            '--------------------------------------------------------------------
            IF (@lpDis.itemState AND %ODS_SELECTED) THEN      ' if selected
               CALL DrawFocusRect(@lpDis.hDC, @lpDis.rcItem)  ' draw a focus rectangle around all
            END IF
     
            FUNCTION = %TRUE
     
      END SELECT
     
    END FUNCTION

    ------------------
    http://www.tolkenxp.com/pb
    Download: incLean, PBcodec, custom controls and code, etc.
    Borje Hagsten - [email protected]
Working...
X