Announcement

Collapse
No announcement yet.

Nmtvcustomdraw?

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

    Nmtvcustomdraw?

    I've tried many partial examples and Adam Drake's PowerBasic TRACE Log Viewer (from Poffs) and I'm still having a problem understanding how to do what I want.

    What I want to do is be able to set the color of a treeview node based on variable criteria. The app uses two treeviews with treeview one being the different menus and the right treeview being the program names available based on the selected node in treeview one. Each time a new item is clicked in treeview one, the list of items displayed in treeview 2 changes. Clicking on an item in treeview 2 launches the program specified.

    The app has users with different levels of access and those nodes they can't use should still show but in gray instead of black for the text.

    Anyone have an easier example of coloring some treeview nodes a different color than the rest as they are being drawn?

    Also I've found several ListView samples using NM_CUSTOMDRAW. Is it done exactly the same way there as for Treeviews?

    Bob Mechler

    #2
    > Is it done exactly the same way there as for Treeviews?

    Yup.
    NM_CUSTOMDRAW (tree view) Notification

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

    Sent by a tree-view control to notify its parent window about drawing operations. This notification is sent in the form of a WM_NOTIFY message.

    Syntax

    NM_CUSTOMDRAW

    lpNMCustomDraw = (LPNMTVCUSTOMDRAW) lParam;
    Parameters

    lpNMCustomDraw
    Pointer to an NMTVCUSTOMDRAW structure that contains and receives information about the drawing operation. The dwItemSpec member of the nmcd member of this structure contains the handle of the item being drawn. The lItemlParam member of the nmcd member of this structure contains the lParam of the item being drawn.
    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


      #3
      Originally posted by BOB MECHLER View Post
      What I want to do is be able to set the color of a treeview node based on variable criteria.
      There is an example in the forums by Steve Mc Gregor of a treeview using three state images, one being grayed. Maybe that would help? http://www.powerbasic.com/support/pb...angechildstate

      Comment


        #4
        Steve's example doesn't use the NM_CUSTOMDRAW event.

        Still hoping someone will share some code snippet. Just lazy I guess. Chris Boss pointed me to the MSDN doc on NM_CUSTOMDRAW but the code is in C/C++ which I'm not well versed in.

        Bob Mechler

        Comment


          #5
          Using Pbforms and Adam Drake's example the following does what I want.
          Code:
          #PBFORMS CREATED V1.51
          #COMPILE EXE
          #DIM ALL
          
          '------------------------------------------------------------------------------
          '   ** Includes **
          '------------------------------------------------------------------------------
          #PBFORMS BEGIN INCLUDES 
          %USEMACROS = 1
          #IF NOT %DEF(%WINAPI)
          	#INCLUDE "WIN32API.INC"
          #ENDIF
          #IF NOT %DEF(%COMMCTRL_INC)
          	#INCLUDE "COMMCTRL.INC"
          #ENDIF
          #INCLUDE "PBForms.INC"
          #PBFORMS END INCLUDES
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Constants **
          '------------------------------------------------------------------------------
          #PBFORMS BEGIN CONSTANTS 
          %IDD_DIALOG1         =  101
          %TRV_SYSTREEVIEW32_1 = 1001
          #PBFORMS END CONSTANTS
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Declarations **
          '------------------------------------------------------------------------------
          DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
          DECLARE FUNCTION SampleTreeViewInsertItem(BYVAL hTree AS DWORD, BYVAL hParent _
          	AS DWORD, sItem AS STRING) AS LONG
          DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          #PBFORMS DECLARATIONS
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Main Application Entry Point **
          '------------------------------------------------------------------------------
          FUNCTION PBMAIN()
          	PBFormsInitComCtls (%ICC_WIN95_CLASSES OR %ICC_DATE_CLASSES OR _
          		%ICC_INTERNET_CLASSES)
          
          	ShowDIALOG1 %HWND_DESKTOP
          END FUNCTION
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** CallBacks **
          '------------------------------------------------------------------------------
          CALLBACK FUNCTION ShowDIALOG1Proc()
            LOCAL tvi       AS TV_ITEM
            LOCAL tvmsg     AS NMTVCUSTOMDRAW PTR
            LOCAL lRet      AS LONG
            LOCAL rc        AS RECT
            LOCAL lRet2     AS LONG
            LOCAL szBuf     AS ASCIIZ*2048
          
          	SELECT CASE AS LONG CBMSG
          		CASE %WM_INITDIALOG
          			' Initialization handler
          
              CASE %WM_NOTIFY
                tvMsg  = CBLPARAM
                IF (@tvmsg.nmcd.hdr.code=%NM_CUSTOMDRAW) AND (@tvmsg.nmcd.hdr.idfrom=%TRV_SYSTREEVIEW32_1) THEN
                  SELECT CASE @tvmsg.nmcd.dwDrawStage
                      CASE %CDDS_PREPAINT
                        FUNCTION=%CDRF_NOTIFYITEMDRAW
                      CASE %CDDS_ITEMPREPAINT
                        lRet=TreeView_GetItemRect(@tvmsg.nmcd.hdr.hwndFrom,@tvmsg.nmcd.dwItemSpec,rc,%TRUE)
                        IF ISTRUE(lRet) THEN
                          tvi.mask=%TVIF_TEXT
                          tvi.cchTextMax=SIZEOF(szBuf)
                          tvi.pszText=VARPTR(szBuf)
                          [email protected]
                          tvi.pszText=VARPTR(szBuf)
                          tvi.cchTextMax=2048
                          lRet2=TreeView_GetItem(@tvmsg.nmcd.hdr.hwndFrom, tvi)
                          IF ISTRUE(lRet2) THEN
                            IF INSTR(trim$(szBuf),"Grey") THEN
                                IF ((@tvmsg.nmcd.uItemState AND %CDIS_SELECTED)<>%CDIS_SELECTED) THEN
                                    @tvmsg.ClrText=%GRAY
                                    @tvmsg.ClrTextBk=%WHITE 'RGB(200,0,0)
                                    FUNCTION=%CDRF_NEWFONT
                                ELSE
                                    FUNCTION=%CDRF_DODEFAULT
                                END IF
                            ELSE
                                FUNCTION=%CDRF_DODEFAULT
                            END IF
                          ELSE
                              FUNCTION=%CDRF_DODEFAULT
                          END IF
                        ELSE
                            FUNCTION=%CDRF_DODEFAULT
                        END IF
                  END SELECT
                END IF
              
          		CASE %WM_NCACTIVATE
          			STATIC hWndSaveFocus AS DWORD
          			IF ISFALSE CBWPARAM THEN
          				' Save control focus
          				hWndSaveFocus = GetFocus()
          			ELSEIF hWndSaveFocus THEN
          				' Restore control focus
          				SetFocus(hWndSaveFocus)
          				hWndSaveFocus = 0
          			END IF
          
          		CASE %WM_COMMAND
          			' Process control notifications
          			SELECT CASE AS LONG CBCTL
          				CASE %TRV_SYSTREEVIEW32_1
          
          			END SELECT
          	END SELECT
          END FUNCTION
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Sample Code **
          '------------------------------------------------------------------------------
          FUNCTION SampleTreeViewInsertItem(BYVAL hTree AS DWORD, BYVAL hParent AS _
          	DWORD, sItem AS STRING) AS LONG
          	LOCAL tTVItem   AS TV_ITEM
          	LOCAL tTVInsert AS TV_INSERTSTRUCT
          
          	IF hParent THEN
          		tTVItem.mask      = %TVIF_CHILDREN OR %TVIF_HANDLE
          		tTVItem.hItem     = hParent
          		tTVItem.cchildren = 1
          		TreeView_SetItem(hTree, tTVItem)
          	END IF
          
          	tTVInsert.hParent              = hParent
          	tTVInsert.Item.Item.mask       = %TVIF_TEXT
          	tTVInsert.Item.Item.pszText    = STRPTR(sItem)
          	tTVInsert.Item.Item.cchTextMax = LEN(sItem)
          
          	FUNCTION = TreeView_InsertItem(hTree, tTVInsert)
          END FUNCTION
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          FUNCTION SampleTreeView(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lCount _
          	AS LONG) AS LONG
          	LOCAL i       AS LONG
          	LOCAL j       AS LONG
          	LOCAL k       AS LONG
          	LOCAL hCtl    AS DWORD
          	LOCAL hRoot   AS DWORD
          	LOCAL hParent AS DWORD
          
          	CONTROL HANDLE hDlg, lID TO hCtl
          
          	FOR i = 1 TO lCount
          		hRoot = SampleTreeViewInsertItem(hCtl, %NULL, USING$("Root#", i))
          		FOR j = 1 TO lCount
          			hParent = SampleTreeViewInsertItem(hCtl, hRoot, USING$("Item#", j))
          			FOR k = 1 TO lCount
          			  IF k mod 2 = 0 THEN
          			  	CALL SampleTreeViewInsertItem(hCtl, hParent, USING$("SubItem#_.#", j, _
          					  k))
          				ELSE
          			  	CALL SampleTreeViewInsertItem(hCtl, hParent, USING$("SubItemGrey#_.#", j, _
          					  k))
          				END IF	  
          			NEXT k
          		NEXT j
          	NEXT i
          END FUNCTION
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Dialogs **
          '------------------------------------------------------------------------------
          FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          	LOCAL lRslt AS LONG
          
          #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
          	LOCAL hDlg  AS DWORD
          
          	DIALOG NEW hParent, "Dialog1", 91, 70, 490, 338, %WS_POPUP OR %WS_BORDER OR _
          		%WS_DLGFRAME OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR _
          		%WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR _
          		%DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
          		%WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
          	CONTROL ADD "SysTreeView32", hDlg, %TRV_SYSTREEVIEW32_1, "SysTreeView32_1", _
          		101, 63, 289, 152, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR _
          		%TVS_HASBUTTONS OR %TVS_HASLINES OR %TVS_LINESATROOT OR _
          		%TVS_SHOWSELALWAYS, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE OR _
          		%WS_EX_RIGHTSCROLLBAR 
          #PBFORMS END DIALOG
          
          	SampleTreeView hDlg, %TRV_SYSTREEVIEW32_1, 3
          
          	DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
          
          #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
          #PBFORMS END CLEANUP
          
          	FUNCTION = lRslt
          END FUNCTION
          '------------------------------------------------------------------------------

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎