Date: 02-16-2022
Return to Index
created by gbSnippets
'Many controls support the "ownerdraw" style, which requires that the
'application draw the control. Using that style, a control will not
'automatically update itself.
'When the ownerdraw style is applied to a control, the %WM_DrawItem
'is sent whenver the control needs to be redrawn.
'Primary Code:
'First, use the ownerdraw style for the specific control, such as
'%BS_OwnerDraw in this button example:
Control Add Button, hDlg, 100,"Push", 75,50,75,75, %BS_OwnerDraw
'Then, provide the drawing routine in response to the %WM_DrawItem message.
'In this example, a white box with a black frame is drawn. See the next
'paragraph for more information on how these lines work:
Case %WM_DrawItem
pDis = CB.lParam
FillRect (@pDis.hDC, @pDis.rcItem, GetStockObject(%WHITE_BRUSH)) 'white fill
FrameRect (@pDis.hDC, @pDis.rcItem, GetStockObject(%BLACK_BRUSH)) 'black frame
'The lParam argument to the message provides a pointer to a DrawItemStruc
'structure that information useful in redrawing the control. Here are the
'members of the structure (see MSDN for more information):
CtlType 'type of control - %ODT_Button, ...
CtlID 'control identifier
itemID 'mneu item identifier or item in listbox/combobox
itemAction 'action to take - %ODA_DrawEntire, ...
itemState 'visual state after drawing action takes place - %ODS_Selected, ...
hwndItem 'handle to control, with some exceptions
hDC 'handle to device context for drawing operations on the control
rcItem 'rectangle that defines boundary of control
itemData 'appplication-defined data
'In the example above, API were used to draw the control. The built-in PowerBASIC
'statements can also be used. One approach is to create a memory bitmap on which
'to draw content, then copy the bitmap over to the control hDC - as with this
'example.
'create the memory bitmap - use over and over
Graphic Bitmap New 75,75 To hBMP
Graphic Attach hBMP, 0
Graphic Get DC To hDC
Case %WM_DrawItem
Graphic Color %Red, %Blue 'paint/draw ... something to the memory bitmap
pDis = CB.lParam
Bitblt @pDis.hDC,0,0,75,75,hDC,0,0,%SrcCopy 'this does NOT work.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, iCount As Long, hBMP as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Button Test",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 75,50,75,75, %BS_OwnerDraw
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pDis As DrawItemStruct Ptr, sTxt As String
Select Case CB.Msg
Case %WM_DrawItem
pDis = CB.lParam
If (@pDis.ItemState AND %ODS_Selected) Then
sTxt = "Pressed"
FillRect (@pDis.hDC, @pDis.rcItem, GetStockObject(%WHITE_BRUSH)) 'white fill
FrameRect (@pDis.hDC, @pDis.rcItem, GetStockObject(%BLACK_BRUSH)) 'black frame
DrawText @pDis.hDC, ByVal StrPTR(sTxt), -1, ByVal VarPTR(@pDis.rcItem), %DT_Center
Else
sTxt = "Not Pressed"
FillRect @pDis.hDC, @pDis.rcItem, GetStockObject(%Black_BRUSH) 'black fill
FrameRect @pDis.hDC, @pDis.rcItem, GetStockObject(%White_BRUSH) 'white frame
DrawText @pDis.hDC, ByVal StrPTR(sTxt), -1, ByVal VarPTR(@pDis.rcItem), %DT_Center
End If
End Select
End Function
'gbs_00432
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm