auf eine Frage im englischen Forum hin und weil ich es selbst brauchte, habe ich mich mit dem OwnerDrawing von Buttons beschäftigt. Sind noch einige kleine Fehlerchen drin, an denen ich noch arbeiten muss, besonders die Geschwindigkeit beim mehrmaligen hintereinander Drücken der Buttons ist noch nicht optimal. Wenn jemand dafür eine Lösung weiß, wäre das super ... danke!
Ansonsten viel Spaß hiermit:
Code: Alles auswählen
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Author: Christian
; Date: 22. December 2004
;
;
; ToDo:
; *****
; - #PB_Button_Multiline
; - #PB_Button_Toggle
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; --> Constants
#ODS_HOTLIGHT = 40
; --> This is needed for PB's drawing functions to work
Structure PBDrawingStruct
Type.l
WindowHandle.l
DC.l
ReleaseProcedure.l
EndStructure
mydraw.PBDrawingStruct
mydraw\Type = 1
Global mydraw
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Procedures
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; --> Get the styles of a Gadget
Procedure.l GetStyle(GadgetID.l)
ProcedureReturn GetWindowLong_(GadgetID, #GWL_STYLE)
EndProcedure
; --> Windowcallback
Procedure myWindowCallback(hwnd, msg, wparam, lparam)
result = #PB_ProcessPureBasicEvents
Select msg
Case #WM_DRAWITEM
; -- Get handle and DeviceContext
*lpdis.DRAWITEMSTRUCT = lparam
mydraw\WindowHandle = *lpdis\hwndItem
hDC = GetDC_(mydraw\WindowHandle)
; -- Get GadgetRect
XPos = *lpdis\rcItem\left
YPos = *lpdis\rcItem\top
Width = *lpdis\rcItem\right-*lpdis\rcItem\left
Height = *lpdis\rcItem\bottom-*lpdis\rcItem\top
; -- Get font information and GadgetText
GadgetFont = SendMessage_(mydraw\WindowHandle, #WM_GETFONT, 0, 0)
GetObject_(GadgetFont, SizeOf(LOGFONT), @lf.LOGFONT)
FontHeight.l = Abs(lf\lfHeight)
GadgetText.s = Space(1000)
GetWindowText_(mydraw\WindowHandle, @GadgetText, 1000)
; -- Start with OwnerDrawing
Select *lpdis\CtlType
Case #ODT_BUTTON
; -- Draw disabled button
If *lpdis\itemState & #ODS_DISABLED
If StartDrawing(mydraw)
; -- Border
LineXY(XPos, YPos, Width - 1, 0, GetSysColor_(5))
LineXY(XPos, YPos, 0, Height - 1, GetSysColor_(5))
LineXY(XPos + Width - 1, YPos, XPos + Width - 1, YPos + Height - 1, GetSysColor_(3))
LineXY(XPos, YPos + Height - 1, XPos + Width - 1, YPos + Height - 1, GetSysColor_(3))
; -- BackgroundColor
Box(XPos + 1, YPos + 1, Width - 2, Height - 2, GetSysColor_(4))
; -- Text
DrawingMode(1)
DrawingFont(GadgetFont)
If GetStyle(mydraw\WindowHandle) & #PB_Button_Left
FrontColor(Red(GetSysColor_(5)), Green(GetSysColor_(5)), Blue(GetSysColor_(5)))
Locate(5 + 1, (Height - YPos - FontHeight)/2 + 1)
DrawText(GadgetText)
FrontColor(Red(GetSysColor_(3)), Green(GetSysColor_(3)), Blue(GetSysColor_(3)))
Locate(5, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
ElseIf GetStyle(mydraw\WindowHandle) & #PB_Button_Right
FrontColor(Red(GetSysColor_(5)), Green(GetSysColor_(5)), Blue(GetSysColor_(5)))
Locate(Width - TextLength(GadgetText) - 5 + 1, (Height - YPos - FontHeight)/2 + 1)
DrawText(GadgetText)
FrontColor(Red(GetSysColor_(3)), Green(GetSysColor_(3)), Blue(GetSysColor_(3)))
Locate(Width - TextLength(GadgetText) - 5, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
Else
FrontColor(Red(GetSysColor_(5)), Green(GetSysColor_(5)), Blue(GetSysColor_(5)))
Locate((Width - XPos - TextLength(GadgetText))/2 + 1, (Height - YPos - FontHeight)/2 + 1)
DrawText(GadgetText)
FrontColor(Red(GetSysColor_(3)), Green(GetSysColor_(3)), Blue(GetSysColor_(3)))
Locate((Width - XPos - TextLength(GadgetText))/2, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
EndIf
StopDrawing()
EndIf
; -- Draw toogled button
ElseIf *lpdis\itemState & #ODS_SELECTED
If StartDrawing(mydraw)
; -- Border
; -- Border
LineXY(XPos, YPos, Width - 1, 0, GetSysColor_(3))
LineXY(XPos, YPos, 0, Height - 1, GetSysColor_(3))
LineXY(XPos + Width - 1, YPos, XPos + Width - 1, YPos + Height - 1, GetSysColor_(5))
LineXY(XPos, YPos + Height - 1, XPos + Width - 1, YPos + Height - 1, GetSysColor_(5))
; -- BackgroundColor
Box(XPos + 1, YPos + 1, Width - 2, Height - 2, GetSysColor_(4))
; -- Text
DrawingMode(1) : FrontColor(0, 0, 0)
DrawingFont(GadgetFont)
Locate((Width - XPos - TextLength(GadgetText))/2 + 1, (Height - YPos - FontHeight)/2 + 1)
DrawText(GadgetText)
StopDrawing()
EndIf
Else
; -- Draw normal button
If StartDrawing(mydraw)
; -- Border
LineXY(XPos, YPos, Width - 1, 0, GetSysColor_(5))
LineXY(XPos, YPos, 0, Height - 1, GetSysColor_(5))
LineXY(XPos + Width - 1, YPos, XPos + Width - 1, YPos + Height - 1, GetSysColor_(3))
LineXY(XPos, YPos + Height - 1, XPos + Width - 1, YPos + Height - 1, GetSysColor_(3))
; -- BackgroundColor
Box(XPos + 1, YPos + 1, Width - 2, Height - 2, GetSysColor_(4))
; -- Text
DrawingMode(1) : FrontColor(0, 0, 0)
DrawingFont(GadgetFont)
If GetStyle(mydraw\WindowHandle) & #PB_Button_Left
;If GetStyle(mydraw\WindowHandle) = #PB_Button_MultiLine
Locate(5, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
ElseIf GetStyle(mydraw\WindowHandle) & #PB_Button_Right
Locate(Width - TextLength(GadgetText) - 5, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
Else
Locate((Width - XPos - TextLength(GadgetText))/2, (Height - YPos - FontHeight)/2)
DrawText(GadgetText)
EndIf
StopDrawing()
EndIf
EndIf
If *lpdis\itemState & #ODS_FOCUS
; -- Draw the FocusRect
*lpdis\rcItem\left + 3
*lpdis\rcItem\top + 3
*lpdis\rcItem\right - 3
*lpdis\rcItem\bottom - 3
DrawFocusRect_(hDC, *lpdis\rcItem)
EndIf
EndSelect
result = #TRUE
EndSelect
ProcedureReturn result
EndProcedure
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Program
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If OpenWindow(0, 0, 0, 300, 200,#PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Christians OwnerDrawn Buttons") And CreateGadgetList(WindowID(0))
SetWindowCallback(@myWindowCallback())
ButtonGadget(0, 50, 50, 200, 50, "Owner Drawn Button 1", #BS_OWNERDRAW)
ButtonGadget(1, 50, 105, 200, 50, "Owner Drawn Button 2", #BS_OWNERDRAW)
;repeat : debug WaitwindowEvent() : forever
Repeat
event = WaitWindowEvent()
Select event
Case #PB_EventGadget
Select EventGadgetID()
Case 0
MessageRequester("Info", "Button 1 pressed!", 0)
Case 1
MessageRequester("Info", "Button 2 pressed!", 0)
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow
EndIf
End
- Drawing für disabled Gadgets hinzugefügt.