Page 1 of 1
MessageRequester centered with your main window
Posted: Fri Jul 03, 2009 9:14 pm
by flaith
Hi, maybe already discussed, sorry
it is just a tip to make your messagerequester centered with your main window :
Code: Select all
#WIN_MAIN = 0
Global WinMainX, WinMainY, WinMainWidth, WinMainHeight
Procedure RequesterCallBack(hWnd, uMsg, wParam, lParam)
hWnd = FindWindow_(#Null,"Requester Center")
If hWnd
GetWindowRect_(hWnd,cr.RECT)
GetWindowRect_(WindowID(#WIN_MAIN),cr_main.RECT)
WinMainX = WindowX(#WIN_MAIN)
WinMainY = WindowY(#WIN_MAIN)
WinMainWidth = cr_main\right - cr_main\left
WinMainHeight= cr_main\bottom - cr_main\top
width = cr\right - cr\left
height = cr\bottom - cr\top
x = WinMainX + (WinMainWidth - width) /2
y = WinMainY + (WinMainHeight - height) /2
SetWindowPos_(hWnd,0,x,y,0,0,#SWP_NOSIZE|#SWP_NOZORDER)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
OpenWindow(#WIN_MAIN,0,0,320,140,"Main Window",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowCallback(@RequesterCallBack())
TextGadget(0,60,40,190,30,"Move the window and clic me",#PB_Text_Center)
ButtonGadget(1,80,80,150,22,"- Clic me -")
Repeat
EventID = WaitWindowEvent(10)
Select EventID
Case #PB_Event_Gadget
Select EventGadget()
Case 1
MessageRequester("Requester Center","Center requester to the Main Window !")
EndSelect
EndSelect
Until EventID = #PB_Event_CloseWindow
there surely a simple or a quicker way to do this

Posted: Fri Jul 03, 2009 10:26 pm
by Edwin Knoppert
Deleted...
Posted: Sat Jul 04, 2009 2:21 pm
by +18
Very useful .THX for sharing
Can you remove flickering it, when move The messageRequister?
Posted: Sat Jul 04, 2009 3:11 pm
by flaith
+18 wrote:Very useful .THX for sharing
Can you remove flickering it, when move The messageRequister?

haven't seen it before, thx for the return
As it's a callback, when you move the requester, he force to make it center each time, will try to find a way to stop that

Posted: Sat Jul 04, 2009 6:07 pm
by netmaestro
Alternate way:
Code: Select all
Global Hook
Procedure HookProc(nCode, wParam, lParam)
Select nCode
Case #HCBT_CREATEWND
*pcbt.CBT_CREATEWND = lParam
*pcs.CREATESTRUCT = *pcbt\lpcs
cn$ = Space(#MAX_PATH)
GetClassName_(wParam, @cn$, #MAX_PATH-1)
If cn$ = "#32770"
*pcs\x = WindowX(0)+WindowWidth(0)/2-*pcs\cx/2
*pcs\y = WindowY(0)+WindowHeight(0)/2-*pcs\cy/2
EndIf
EndSelect
ProcedureReturn CallNextHookEx_(Hook, nCode, wParam, lParam)
EndProcedure
Procedure Messagerequester_wc(title$, body$, flags=0)
Hook = SetWindowsHookEx_(#WH_CBT, @HookProc(), #Null, GetCurrentThreadId_())
result = MessageRequester(title$, body$, flags)
UnhookWindowsHookEx_(Hook)
ProcedureReturn result
EndProcedure
OpenWindow(0,0,0,640,480,"")
Debug MessageRequester_wc("Hello!","Wanna do some coding?",#MB_YESNOCANCEL)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Posted: Sat Jul 04, 2009 6:41 pm
by flaith

I knew you'll find a way to do that !
thanks a lot netmaestro, you're really one of the best master of that forum.
but now could you please give us a little explanation of that code ?

Posted: Sun Jul 05, 2009 1:54 pm
by +18
flaith wrote:
I knew you'll find a way to do that !
thanks a lot netmaestro, you're really one of the best master of that forum.
but now could you please give us a little explanation of that code ?

+1
THX netmaestro
Posted: Sun Jul 05, 2009 4:34 pm
by netmaestro
but now could you please give us a little explanation of that code ?
The CBT hook, or Computer Based Training hook, is a versatile tool you can use to get and change information relating to windows at every stage of their lives. In this case, we're setting the hook and then running the requester. Before it is actually created the HCBT_CREATEWND event gives us a peek at its intended specifications and we have an opportunity to change them to fit our needs. We want to make changes to its x and y parameters and so our structured pointer writes the new values in at the appropriate offsets. When the window creation actually happens, our values are what gets used. Once this is accomplished, there is no more use for the hook and so we just unhook it after the messagerequester line. Another common place to unhook would be in the HCBT_ACTIVATE event, which happens when the window is about to be activated.
P.S. The CBT hook is so versatile that it can be used for almost anything. I used it once to open my kitchen drain. It was hard at first because I knew that hook could do it, but how to get the code out of the computer? I almost gave up, but then it hit me: Memory leak! I just needed a memory leak to get the code out and into the drain, so I did a forum search for anything by srod and... success!
(Sundays should be "lighten up" day on the forums - don't you think?

)
Posted: Sun Jul 05, 2009 7:45 pm
by flaith
netmaestro wrote:but now could you please give us a little explanation of that code ?
The CBT hook, or Computer Based Training hook, is a versatile tool you can use to get and change information relating to windows at every stage of their lives. In this case, we're setting the hook and then running the requester. Before it is actually created the HCBT_CREATEWND event gives us a peek at its intended specifications and we have an opportunity to change them to fit our needs. We want to make changes to its x and y parameters and so our structured pointer writes the new values in at the appropriate offsets. When the window creation actually happens, our values are what gets used. Once this is accomplished, there is no more use for the hook and so we just unhook it after the messagerequester line. Another common place to unhook would be in the HCBT_ACTIVATE event, which happens when the window is about to be activated.
P.S. The CBT hook is so versatile that it can be used for almost anything. I used it once to open my kitchen drain. It was hard at first because I knew that hook could do it, but how to get the code out of the computer? I almost gave up, but then it hit me: Memory leak! I just needed a memory leak to get the code out and into the drain, so I did a forum search for anything by srod and... success!
thanks for taking time for your explanation
(Sundays should be "lighten up" day on the forums - don't you think?

)

you're absolutely right
Posted: Mon Jul 06, 2009 10:08 am
by SFSxOI
netmaestro wrote:but now could you please give us a little explanation of that code ?
The CBT hook, or Computer Based Training hook, is a versatile tool you can use to get and change information relating to windows at every stage of their lives. In this case, we're setting the hook and then running the requester. Before it is actually created the HCBT_CREATEWND event gives us a peek at its intended specifications and we have an opportunity to change them to fit our needs. We want to make changes to its x and y parameters and so our structured pointer writes the new values in at the appropriate offsets. When the window creation actually happens, our values are what gets used. Once this is accomplished, there is no more use for the hook and so we just unhook it after the messagerequester line. Another common place to unhook would be in the HCBT_ACTIVATE event, which happens when the window is about to be activated.
P.S. The CBT hook is so versatile that it can be used for almost anything. I used it once to open my kitchen drain. It was hard at first because I knew that hook could do it, but how to get the code out of the computer? I almost gave up, but then it hit me: Memory leak! I just needed a memory leak to get the code out and into the drain, so I did a forum search for anything by srod and... success!
(Sundays should be "lighten up" day on the forums - don't you think?

)
Very nice, Thank You
When you say "..writes the new values in at the appropriate offsets", do you mean memory offsets or the X and Y window parameters?
Posted: Mon Jul 06, 2009 8:47 pm
by netmaestro
do you mean memory offsets or the X and Y window parameters?
Both actually. The X and Y window parameters live in the CREATESTRUCT-structured memory block whose address is found in lParam. You have to access them with a structured pointer, or, alternatively, the really clunky PokeI(*lparam+OffsetOf(CBT_CREATEWND\lpcs)+OffsetOf(CREATESTRUCT\x), newx) or the shorter but obfuscated PokeI(lParam+28, newx)
Posted: Mon Jul 06, 2009 10:45 pm
by SFSxOI
Ahhh....OK I see now, thanks netmaestro
