MessageRequester centered with your main window

Share your advanced PureBasic knowledge/code with the community.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

MessageRequester centered with your main window

Post 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 :wink:
“Fear is a reaction. Courage is a decision.” - WC
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Deleted...
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Post by +18 »

Very useful .THX for sharing :)
Can you remove flickering it, when move The messageRequister?
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

+18 wrote:Very useful .THX for sharing :)
Can you remove flickering it, when move The messageRequister?
:oops: 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 :)
“Fear is a reaction. Courage is a decision.” - WC
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

:D 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 ? :wink:
“Fear is a reaction. Courage is a decision.” - WC
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Post by +18 »

flaith wrote::D 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 ? :wink:
+1 :D
THX netmaestro
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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? :wink: )
BERESHEIT
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post 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? :wink: )
:D you're absolutely right
“Fear is a reaction. Courage is a decision.” - WC
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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? :wink: )
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?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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)
BERESHEIT
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Ahhh....OK I see now, thanks netmaestro :)
Post Reply