This code should pretty much size the button to fit correctly on the titlebar of the window. To test it, I added code to change the window style to ToolWindow and it looked right. Also, the memory leak should be fixed.
Code: Select all
;=====================================================================
; Program: TitleBar Button Example - skinned look
; Author: netmaestro
; Date: December 27, 2006
; Target OS: Microsoft Windows All
; Target Compiler: PureBasic 4.02
; License: Free, Unrestricted, credit appreciated
; but not required
; Required Resources: http://www.networkmaestro.com/TitleButton.zip
;=====================================================================
Global pencilup=LoadImage_(GetModuleHandle_(0),"IconUp",#IMAGE_ICON,0,0,0)
Global pencilhover=LoadImage_(GetModuleHandle_(0),"IconHover",#IMAGE_ICON,0,0,0)
Global pencilpushed=LoadImage_(GetModuleHandle_(0),"IconPushed",#IMAGE_ICON,0,0,0)
Global WindowDC, ButtonSize, ButtonTop
Procedure ButtonAction()
SetGadgetText(0, GetGadgetText(0)+"Pencil... ")
EndProcedure
Procedure MouseOnButton()
GetCursorPos_(@pt.point)
If pt\x >= WindowX(0)+540 And pt\x <= WindowX(0)+560
If pt\y >=WindowY(0)+ButtonTop And pt\y <= WindowY(0)+ButtonTop+ButtonSize
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndIf
EndProcedure
Procedure ProcessTitleButtons()
If MouseOnButton()
While GetAsyncKeyState_(#VK_LBUTTON) & 32768
If MouseOnButton()
DrawIconEx_(WindowDC, 540,ButtonTop, pencilpushed, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
Else
DrawIconEx_(WindowDC, 540,ButtonTop, pencilup, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
EndIf
Delay(1)
Wend
While WindowEvent():Wend
If MouseOnButton()
ButtonAction()
EndIf
DrawIconEx_(WindowDC, 540,ButtonTop, pencilup, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
EndIf
EndProcedure
Procedure CallBack(hwnd, msg, wparam, lparam)
result = #PB_ProcessPureBasicEvents
Select msg
Case #WM_PAINT,#WM_ACTIVATE,#WM_MOVE
DrawIconEx_(WindowDC, 540,ButtonTop, pencilup, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
Case #WM_SYSCOMMAND
ProcessTitleButtons()
EndSelect
ProcedureReturn result
EndProcedure
Procedure TitleButtonHover()
Repeat
If GetAsyncKeyState_(#VK_LBUTTON) & 32768 = 0
If MouseOnButton()
DrawIconEx_(WindowDC, 540,ButtonTop, pencilhover, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
Else
DrawIconEx_(WindowDC, 540,ButtonTop, pencilup, ButtonSize, ButtonSize, 0, #Null, #DI_NORMAL )
EndIf
EndIf
Delay(50)
ForEver
EndProcedure
OpenWindow(0,0,0,640,480,"TitleBarButton Example by netmaestro",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
WindowDC = GetWindowDC_(WindowID(0))
wi.WINDOWINFO
wi\cbSize = SizeOf(WINDOWINFO)
GetWindowInfo_(WindowID(0), @wi)
TitleBarHeight = (wi\rcClient\top - wi\rcWindow\top)
ButtonSize = TitleBarHeight * 0.62
buttonTop = TitleBarHeight * 0.25
SetWindowCallback(@CallBack())
CreateThread(@TitleButtonHover(), 0)
CreateGadgetList(WindowID(0))
TextGadget(0,220,200,240,200,"")
Repeat : Until WaitWindowEvent() = #WM_CLOSE
ReleaseDC_(WindowID(0), WindowDC)
End