New Update :
Now you can use any size for the images (Reasonable Size)
It will be resized to be fine with any TitleBar height
#1 :
Modified by MV
For solving a problem when the minimize animation is enabled in win 7
Code: Select all
Global x,y,w,buttonW,buttonH,borderW,r1.RECT,r2.RECT,ma
Procedure CreateButton(image,x,y,text.s="×",color=$c0c0c0)
If CreateImage(image,x,y)
StartDrawing(ImageOutput(image))
Box(1,1,x-2,y-2,color)
Plot(0,y-1,color)
Plot(x-1,y-1,color)
DrawingMode(#PB_2DDrawing_Transparent)
x-TextWidth(text)
y-TextHeight(text)
DrawText(x>>1,y>>1,text)
StopDrawing()
EndIf
EndProcedure
Procedure WndProc(hwnd, uMsg, wParam, lParam)
GetWindowRect_(WindowID(1),r.RECT)
x = r\right - (buttonW*5) - borderW ;5 = for Max & Restore & Min and 2 more buttons
y = r\top
If OSVersion() <= #PB_OS_Windows_XP
y = y + 5
Else
x = x + 20
EndIf
; If w < 20
; x = x = r\right - (buttonW*4) - borderW + 8
; EndIf
result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_NOTIFY
Case #WM_GETMINMAXINFO
*pMinMax.MINMAXINFO = lParam
*pMinMax\ptMinTrackSize\x=buttonW*5
Case #WM_SIZE,#WM_MOVE,#WM_PAINT
GetWindowRect_(GadgetID(10),r1.RECT)
GetWindowRect_(GadgetID(11),r2.RECT)
r1\top = r1\top + 2
r2\top = r2\top + 2
If GetWindowState(1) = #PB_Window_Maximize
If OSVersion() <= #PB_OS_Windows_XP
ResizeWindow(2,x,y,(w+4)*2,buttonH)
Else
ResizeWindow(2,x-4,y+6,(w+4)*2,buttonH)
EndIf
Else
ResizeWindow(2,x,y,(w+4)*2,buttonH)
EndIf
EndSelect
ProcedureReturn result
EndProcedure
anm.ANIMATIONINFO\cbSize = SizeOf(ANIMATIONINFO)
SystemParametersInfo_($0048, SizeOf(ANIMATIONINFO), @anm, 0)
ma = anm.ANIMATIONINFO\iMinAnimate
If ma = 1
anm.ANIMATIONINFO\iMinAnimate = 0
SystemParametersInfo_($0049, SizeOf(ANIMATIONINFO), @anm, 0)
EndIf
;LoadImage(0, "state1.bmp") ;24 x 24 pixel Button state 1
;LoadImage(1, "state2.bmp") ;24 x 24 pixel Button state 2
OpenWindow(1,0,0,600,400,"Add Button to Title Bar",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
w = GetSystemMetrics_(#SM_CYCAPTION) - 2 ;2 to 3
CreateButton(0,w,w)
CreateButton(1,w,w,"?",$f0c0c0)
buttonW = GetSystemMetrics_(#SM_CXSIZE)
buttonH = GetSystemMetrics_(#SM_CYSIZE)
borderW = GetSystemMetrics_(#SM_CXBORDER)
OpenWindow(2,0,0,w*5,w,"",#WS_POPUP,WindowID(1))
SetWindowColor(2,#Red)
SetWindowLongPtr_(WindowID(2),#GWL_EXSTYLE,GetWindowLongPtr_(WindowID(2),#GWL_EXSTYLE)|#WS_EX_LAYERED)
SetLayeredWindowAttributes_(WindowID(2),#Red,0,#LWA_COLORKEY)
;ResizeImage(0,w,w)
;ResizeImage(1,w,w)
;ImageGadget(10,0,0,0,0,ImageID(0))
;ImageGadget(11,0,0,0,0,ImageID(0))
ImageGadget(10,4,0,w,w,ImageID(0))
ImageGadget(11,4+w,0,w,w,ImageID(0))
SetWindowCallback(@WndProc())
SetActiveWindow(1)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
If ma = 1
anm.ANIMATIONINFO\iMinAnimate = 1
SystemParametersInfo_($0049, SizeOf(ANIMATIONINFO), @anm, 0)
EndIf
Q = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 10
Debug "Button 10 Pressed"
Case 11
Debug "Button 11 Pressed"
EndSelect
SetActiveWindow(1)
Case #WM_MOUSEMOVE,#WM_NCMOUSEMOVE
GetCursorPos_(@p.POINT)
If PtInRect_(@r1,PeekQ(@p))
SetGadgetState(10,ImageID(1))
Else
SetGadgetState(10,ImageID(0))
EndIf
If PtInRect_(@r2,PeekQ(@p))
SetGadgetState(11,ImageID(1))
Else
SetGadgetState(11,ImageID(0))
EndIf
EndSelect
Until Q =1
End
#2 :
Keeping the min anim enabled
#2-1: Using Delay(350) to compensate the lag in timing
#2-2:Or using Windows Timer to do the job
Code: Select all
Global x,y,w,buttonW,buttonH,borderW,r1.RECT,r2.RECT,ma
Procedure WndProc(hwnd, uMsg, wParam, lParam)
GetWindowRect_(WindowID(1),r.RECT)
x = r\right - (buttonW*5) - borderW ;5 = for Max & Restore & Min and 2 more buttons
y = r\top
If OSVersion() <= #PB_OS_Windows_XP
y = y + 5
Else
x = x + 20
EndIf
; If w < 20
; x = x = r\right - (buttonW*4) - borderW + 8
; EndIf
result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_NOTIFY
Case #WM_GETMINMAXINFO
*pMinMax.MINMAXINFO = lParam
*pMinMax\ptMinTrackSize\x=buttonW*5
Case #WM_SIZE,#WM_MOVE,#WM_PAINT
GetWindowRect_(GadgetID(10),r1.RECT)
GetWindowRect_(GadgetID(11),r2.RECT)
r1\top = r1\top + 2
r2\top = r2\top + 2
If GetWindowState(1) = #PB_Window_Maximize
If OSVersion() <= #PB_OS_Windows_XP
ResizeWindow(2,x,y,(w+4)*2,buttonH)
Else
ResizeWindow(2,x-4,y+6,(w+4)*2,buttonH)
EndIf
Else
ResizeWindow(2,x,y,(w+4)*2,buttonH)
EndIf
EndSelect
ProcedureReturn result
EndProcedure
anm.ANIMATIONINFO\cbSize = SizeOf(ANIMATIONINFO)
SystemParametersInfo_($0048, SizeOf(ANIMATIONINFO), @anm, 0)
ma = anm.ANIMATIONINFO\iMinAnimate
LoadImage(0, "state1.bmp") ;24 x 24 pixel Button state 1
LoadImage(1, "state2.bmp") ;24 x 24 pixel Button state 2
OpenWindow(1,0,0,600,400,"Add Button to Title Bar",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
;If GetWindowLongPtr_(WindowID(1),#GWL_EXSTYLE) <= 256
w = GetSystemMetrics_(#SM_CYCAPTION) - 2
buttonW = GetSystemMetrics_(#SM_CXSIZE)
buttonH = GetSystemMetrics_(#SM_CYSIZE)
borderW = GetSystemMetrics_(#SM_CXBORDER)
; Else
; w = GetSystemMetrics_(#SM_CYSMCAPTION) - 2
; buttonW = w
; buttonH = w
; EndIf
OpenWindow(2,0,0,w*5,w,"",#WS_POPUP,WindowID(1))
SetWindowColor(2,#Red)
SetWindowLongPtr_(WindowID(2),#GWL_EXSTYLE,GetWindowLongPtr_(WindowID(2),#GWL_EXSTYLE)|#WS_EX_LAYERED)
SetLayeredWindowAttributes_(WindowID(2),#Red,0,#LWA_COLORKEY)
ResizeImage(0,w,w)
ResizeImage(1,w,w)
ImageGadget(10,4,0,w,w,ImageID(0))
ImageGadget(11,4+w,0,w,w,ImageID(0))
SetWindowCallback(@WndProc())
SetActiveWindow(1)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Q = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 10
Debug "Button 10 Pressed"
Case 11
Debug "Button 11 Pressed"
EndSelect
SetActiveWindow(1)
Case #PB_Event_MinimizeWindow
If ma = 1 And OSVersion() > #PB_OS_Windows_XP
HideWindow(2,1)
EndIf
Case #PB_Event_RestoreWindow
If ma = 1 And OSVersion() > #PB_OS_Windows_XP
Delay(350)
;AddWindowTimer(2, 10, 350)
HideWindow(2,0)
SetActiveWindow(1)
EndIf
;Case #PB_Event_Timer
;RemoveWindowTimer(2,10)
;HideWindow(2,0)
;SetActiveWindow(1)
Case #WM_MOUSEMOVE,#WM_NCMOUSEMOVE
GetCursorPos_(@p.POINT)
; GetWindowRect_(GadgetID(10),r1.RECT)
; GetWindowRect_(GadgetID(11),r2.RECT)
; r1\top = r1\top + 2
; r2\top = r2\top + 2
If PtInRect_(@r1,PeekQ(@p))
SetGadgetState(10,ImageID(1))
Else
SetGadgetState(10,ImageID(0))
EndIf
If PtInRect_(@r2,PeekQ(@p))
SetGadgetState(11,ImageID(1))
Else
SetGadgetState(11,ImageID(0))
EndIf
EndSelect
Until Q =1
End
To do:(If I have some free time)
Using Transparent Images to be compatible with different color schemes
Edit:
#1 Just updated taking into consideration yrriti & MV suggestions
Have fun