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.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg As DWord, iCount As Long, hBMP as DWord, hDC 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
Graphic Bitmap New 75,75 To hBMP
Graphic Attach hBMP, 0
Graphic Get DC To hDC
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pDis As DrawItemStruct Ptr, sTxt As String, y&
Select Case Cb.Msg
Case %WM_DrawItem
pDis = Cb.LParam
If (@pDis.ItemState And %ODS_Selected) Then
Graphic Color %Black, %Red 'paint/draw ... something to the memory bitmap
Graphic Clear
Graphic Ellipse (5,5)-(70,70), %Blue
For y& = 0 To 75 : Graphic Line (0, y&) - (75, y&), RGB(y&, 0, y&) : Next
Graphic Box (5,5)-(70,70), 0, %White
Graphic Set Pos (5,30)
Graphic Print "Where is she?"
Bitblt @pDis.hDC,0,0,75,75,hDC,0,0,%SrcCopy 'this does NOT work.
Else
Graphic Color %Blue, %Blue 'paint/draw ... something to the memory bitmap
Graphic Clear
For y& = 0 To 75 : Graphic Line (0, y&) - (75, y&), RGB(y&, y&, 0) : Next
Graphic Box (5,5)-(70,70), 0, %White
Graphic Render "cowgirl", (20,20)-(55,55)
Bitblt @pDis.hDC,0,0,75,75,hDC,0,0,%SrcCopy 'this does NOT work.
End If
End Select
End Function
'gbs_00433
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm