MessageRequesterEx(): spice up your messagerequester a bit
Posted: Thu Jan 10, 2008 5:43 am
Here we make a PB messagerequester accept a background color or optional background image, custom text color and screen position. Usage is easy, just fill a structure as shown and call it. Could have bugs or leaks, I wrote it pretty quickly.
And a little test prog:
Code: Select all
;=================================================================
; Program: MessageRequesterEx library command
; Author: Lloyd Gallant (netmaestro)
; Date: January 9, 2008
; Target OS: Microsoft Windows All
; Target Compiler: PureBasic 4.0 and later
; License: Free, unrestricted, no warranty
;=================================================================
Enumeration
#MB_COLOR
#MB_IMAGE
EndEnumeration
Structure MBDATA
x.l ; In, desired x position on screen
y.l ; In, desired y position on screen
bkContent.l ; In, long value representing imageid or colorref depending on type
bkContentType.l ; In, specifies whether bkContent is an image or a colorref
txtColor.l ; In, specifies desired text color for message box
staticBrush.l ; reserved, do not use
bkImageID.l ; reserved, do not use
cx.l ; reserved, do not use
cy.l ; reserved, do not use
EndStructure
Global hook, mbd.MBDATA
Procedure dlgProc(hwnd, msg, wparam, lparam)
oldproc = GetProp_(hwnd, "oldproc")
Select msg
Case #WM_CTLCOLORSTATIC
SetBkMode_(wparam, #TRANSPARENT)
SetTextColor_(wParam, mbd\txtColor)
ProcedureReturn mbd\staticBrush
Case #WM_NCDESTROY
RemoveProp_(hwnd, "oldproc")
DeleteObject_(mbd\staticBrush)
FreeImage(mbd\bkImageID)
EndSelect
ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
EndProcedure
Procedure MBProc(nCode, wParam, lParam)
Static hwndmain
Select nCode
Case #HCBT_CREATEWND
*pcbt.CBT_CREATEWND = lParam
*pcs.CREATESTRUCT = *pcbt\lpcs
Select *pcs\lpszClass
Case 32770
hwndmain = wParam
*pcs\x = mbd\x
*pcs\y = mbd\y
mbd\cx = *pcs\cx - 2*GetSystemMetrics_(#SM_CXFIXEDFRAME)
mbd\cy = *pcs\cy - 2*GetSystemMetrics_(#SM_CYFIXEDFRAME)-GetSystemMetrics_(#SM_CYCAPTION)
SetProp_(wparam, "oldproc", SetWindowLong_(wparam,#GWL_WNDPROC,@dlgProc()))
EndSelect
Case #HCBT_ACTIVATE
If wParam = hwndmain
Select mbd\bkContentType
Case #MB_COLOR
img = CreateImage(#PB_Any, mbd\cx, mbd\cy, 32)
StartDrawing(ImageOutput(img))
Box(0,0,mbd\cx, mbd\cy, mbd\bkContent)
StopDrawing()
mbd\bkImageID = img
Case #MB_IMAGE
ResizeImage(mbd\bkContent,mbd\cx,mbd\cy)
mbd\bkImageID = mbd\bkContent
EndSelect
CreateGadgetList(wParam)
ig = ImageGadget(#PB_Any, 0,0,0,0,ImageID(mbd\bkImageID))
SetWindowPos_(GadgetID(ig), #HWND_BOTTOM,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
SetWindowLong_(GadgetID(ig),#GWL_STYLE,GetWindowLong_(GadgetID(ig),#GWL_STYLE)|#WS_CLIPSIBLINGS)
DisableGadget(ig, #True)
mbd\staticBrush = CreatePatternBrush_(ImageID(mbd\bkImageID))
ProcedureReturn 0
EndIf
EndSelect
ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam)
EndProcedure
Procedure MessageRequesterEx(title$, text$, flags=0)
hook = SetWindowsHookEx_(#WH_CBT, @MBProc(), #Null, GetCurrentThreadId_())
result = MessageRequester(title$, text$, flags)
UnhookWindowsHookEx_(hook)
ProcedureReturn result
EndProcedure
Code: Select all
IncludeFile "MessageRequesterEx.pbi"
; Make a colored message box at 300,300
With mbd
\x = 300
\y = 300
\bkContent = #White
\bkContentType = #MB_COLOR
\txtColor = #Blue
EndWith
MessageRequesterEx("Notice", "The rain in Spain falls mainly on the plain")
; make an image-background message box at 100,100
CreateImage(2, 220,80,32)
StartDrawing(ImageOutput(2))
Box(0,0,220,80,#White)
Circle(60,40,40,RGB(225,255,225))
Circle(150,40,40,RGB(225,255,225))
StopDrawing()
With mbd
\x = 100
\y = 100
\bkContent = 2
\bkContentType = #MB_IMAGE
\txtColor = #Blue
EndWith
MessageRequesterEx("Notice", "The rain in Spain falls mainly on the plain")