Code: Alles auswählen
; *****************************************************
; Code : MessageRequester with "Don't ask me again"
; Author : Sparkie
; Date : November 17, 2006
; OS : Windows only
; License : It's all yours to use as you so desire
; (props/credit are welcome)
; *****************************************************
Global hHook.l = 0, oldProc.l = 0, askagain.l = 0
;...This one is still missing in PB def
#BS_TEXT = 0
;...Make sure Checkbox ID <> any of the other MessageRequester buttons
#CheckBoxID = 12
;...Subclassed MessageRequester Procedure()
Procedure MsgBoxProc(hwnd.l, msg.l, wParam.l, lParam.l)
result.l = CallWindowProc_(oldProc, hwnd, msg, wParam, lParam)
Select msg
Case #WM_DESTROY
;...Get handle to our checkbox
hCheckBox.l = GetDlgItem_(hwnd, #CheckBoxID)
;...Get the checkbox state. <0 = not checked> <1 = checked>
;...For this test I'll set a Global var to = CheckBox state
;...In a real app you could set the value in a config/ini file
;...or maybe a use a Registry setting
askagain = SendMessage_(hCheckBox, #BM_GETCHECK, 0, 0)
EndSelect
ProcedureReturn result
EndProcedure
;...Our hook Procedure()
Procedure CBTProc(nCode.l, wParam.l, lParam.l)
result.l = CallNextHookEx_(hHook, nCode,wParam,lParam)
;...Creation of MessageRequester is here
If nCode = #HCBT_CREATEWND
*cbt_cw.CBT_CREATEWND = lParam
*lpcs.CREATESTRUCT = *cbt_cw\lpcs
;...Take action only if this is our MessageRequester
If *lpcs\hWndParent = WindowID(0) And *lpcs\lpszClass = 32770
;...Resize the MessageRequester to make room for our CheckBoxGadget
*lpcs\cy + 30
EndIf
result = 0
EndIf
;...MessageRequester is about to become active
;...Here we will add our "Don't ask me this again" checkbox
If nCode = #HCBT_ACTIVATE
hMsgBox.l = wParam
;...Subclass the MessageRequester
oldProc = SetWindowLong_(hMsgBox, #GWL_WNDPROC, @MsgBoxProc())
hCheckBox.l = CreateWindowEx_(0, "Button", "Don't ask me this again.", #BS_AUTOCHECKBOX | #BS_TEXT | #WS_GROUP | #WS_TABSTOP | #WS_CHILD | #WS_VISIBLE, 10, 95, 150, 25, hMsgBox, #CheckBoxID, GetModuleHandle_(0), 0)
hFont.l = GetStockObject_(#DEFAULT_GUI_FONT)
SendMessage_(hCheckBox, #WM_SETFONT, hFont, 0)
;...Release the hook
If hHook
UnhookWindowsHookEx_(hHook)
hHook = 0
EndIf
result = 0
EndIf
ProcedureReturn result
EndProcedure
If OpenWindow(0, 100, 100, 300, 200, "Don't ask me again", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
ButtonGadget(1, 10, 40, 280, 22, "Display Custom MessageRequester")
TextGadget(2, 10, 80, 280, 25, "")
Repeat
Event.l = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 1
;...Create a local hook to watch for MessageRequester
If askagain = 0
hHook = SetWindowsHookEx_(#WH_CBT, @CBTProc(), 0, GetCurrentThreadId_())
MessageRequester("Welcome", "Do you want to continue?", #MB_YESNO | #MB_ICONQUESTION)
Else
MessageRequester("Sorry", "You requested not to see that message again.", #MB_OK | #MB_ICONINFORMATION)
EndIf
Select askagain
Case 0
SetGadgetText(2, "Ask me again was not checked")
Case 1
SetGadgetText(2, "Ask me again was checked")
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
;...Just in case it's still there
If hHook
UnhookWindowsHookEx_(hHook)
hHook = 0
EndIf
EndDer Autor des Code will versuchen, den Code am Wochenende aufzuräumen und neu in das Forum zu stellen. Er sagt außerdem, dass vor jedem MessageRequester, das eine Checkbox enthalten soll, dieser Window-Hook aufgerufen werden muss (falls jemand mehrere Messagerequester in sein Programm verbauen will).