Announcement

Collapse
No announcement yet.

OWNER DRAW - How to set left margin for text?

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

  • OWNER DRAW - How to set left margin for text?

    OWNER DRAW - How do you to set a left margin (or top or bottom or right) for text in a LISTBOX?

  • #2
    Can be done via rcItem in DRAWITEMSTRUCT, like:
    Code:
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    
    %ID_LIST1 = 101
    %mLeft    = 10  ' ownerdrawn listbox item margins (oixels)
    %mRight   = 5
    %mTop     = 5
    %mBottom  = 3
    
    '====================================================================
    FUNCTION PBMAIN
      LOCAL hDlg AS DWORD
    
      DIALOG NEW 0, "ListBox margins..", , , 220, 180, %WS_CAPTION OR %WS_SYSMENU TO hDlg
    
      CONTROL ADD LISTBOX, hDlg, %ID_LIST1, , 5, 5, 210, 154, _
                           %WS_CHILD OR %LBS_OWNERDRAWFIXED OR %LBS_HASSTRINGS OR _
                           %WS_TABSTOP OR %WS_VSCROLL, %WS_EX_CLIENTEDGE
    
      CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close", 164, 161, 50, 14
    
      DIALOG SHOW MODAL hDlg, CALL DlgProc
    END FUNCTION
    
    '====================================================================
    CALLBACK FUNCTION DlgProc
      LOCAL i AS LONG, sText AS STRING, _
            lpdis AS DRAWITEMSTRUCT PTR, _
            zTxt AS ASCIIZ * 200 'is 200 bytes/item enough..?
    
      SELECT CASE CBMSG
      CASE %WM_INITDIALOG
         CONTROL SEND CBHNDL, %ID_LIST1, %LB_GETITEMHEIGHT, 0, 0 TO i               'Get height of line
         CONTROL SEND CBHNDL, %ID_LIST1, %LB_SETITEMHEIGHT, 0, i + %mTop + %mBottom 'Add top/bottom margins, if any
         FOR i = 1 TO 50 'fill list with some data to show
            LISTBOX ADD CBHNDL, %ID_LIST1, "This is item #" + FORMAT$(i)
         NEXT
    
      CASE %WM_COMMAND
         SELECT CASE CBCTL
         CASE %IDCANCEL
            IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL
    
         CASE %ID_LIST1
            SELECT CASE CBCTLMSG
            CASE %LBN_DBLCLK
               LISTBOX GET TEXT CBHNDL, %ID_LIST1 TO sText
               MSGBOX sText, _
                      %MB_OK OR %MB_ICONINFORMATION OR %MB_TASKMODAL, _
                      "List message"
    
            CASE %LBN_SELCHANGE
            END SELECT
         END SELECT
    
    
      CASE %WM_DRAWITEM
         IF CBWPARAM = %ID_LIST1 THEN 'make sure it's our list (if several exists)
            lpdis = CBLPARAM
            IF @lpdis.itemID = &HFFFFFFFF THEN EXIT FUNCTION 'if empty list, jump out..
    
            SELECT CASE @lpdis.itemAction
            CASE %ODA_DRAWENTIRE, %ODA_SELECT ''DRAW BACKGROUND
               IF (@lpdis.itemState AND %ODS_SELECTED) = 0 THEN 'if not selected, use these colors
                  FillRect @lpdis.hDC, @lpdis.rcItem, GetSysColorBrush(%COLOR_WINDOW)
                  SetBkColor @lpdis.hDC, GetSysColor(%COLOR_WINDOW)
                  SetTextColor @lpdis.hDC, GetSysColor(%COLOR_WINDOWTEXT)
               ELSE
                  FillRect @lpdis.hDC, @lpdis.rcItem, GetSysColorBrush(%COLOR_HIGHLIGHT)
                  SetBkColor @lpdis.hDC, GetSysColor(%COLOR_HIGHLIGHT)
                  SetTextColor @lpdis.hDC, GetSysColor(%COLOR_HIGHLIGHTTEXT)
               END IF
    
               'DRAW TEXT
               SetRect @lpdis.rcItem, @lpdis.rcItem.nLeft + %mLeft,  @lpdis.rcItem.nTop + %mTop, _
                                      @lpdis.rcItem.nRight - %mRight, @lpdis.rcItem.nBottom - %mBottom  ' Set margins
               CONTROL SEND CBHNDL, %ID_LIST1, %LB_GETTEXT, @lpdis.itemID, VARPTR(zTxt)  'Get text to draw
               DrawText @lpdis.hDC, zTxt, LEN(zTxt), @lpdis.rcItem, %DT_LEFT  ' draw text
    
               FUNCTION = %TRUE
            END SELECT
         END IF
    
      END SELECT
    
    END FUNCTION
    Last edited by Borje Hagsten; 8 Dec 2018, 09:45 AM.

    Comment


    • #3
      Thanks Borje!

      Comment

      Working...
      X