Center MessageRequester

Just starting out? Need help? Post your questions and find answers here.
Little_man
Enthusiast
Enthusiast
Posts: 152
Joined: Fri Mar 29, 2013 4:55 pm
Location: The Netherland

Center MessageRequester

Post by Little_man »

How can I center the Messagerequester on any window ?.
(see comments in code ; ##### <----- xxxxx)

Code: Select all


;My Purebasic version 5.11.

EnableExplicit

Enumeration
  #WIN_Main
  #WIN_Text
  #WIN_Button1
  #WIN_Button2
EndEnumeration

;-Dialog hook to set font in MessageRequester.
Global Hook.i
Global IDYesText.s, IDNoText.s, IDCancelText.s
Global WinMainX.i, WinMainY.i, WinMainWidth.i, WinMainHeight.i, EventID.i
Global MessageTitle.s, MessageText.s

Global FontID_Body.l   = LoadFont(0, "verdana", 8, #PB_Font_HighQuality | #PB_Font_Italic)
Global FontID_Button.l = LoadFont(1, "verdana", 8, #PB_Font_HighQuality | #PB_Font_Italic | #PB_Font_Bold)

Procedure ChangeTextFont(hwnd, lParam)
  ;Find messagebox text control and buttons.
  Global CN.s = Space(#MAX_PATH)

  GetClassName_(hwnd, @CN, #MAX_PATH)

  Select UCase(CN)
    Case "STATIC"
      If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        ;Change font Body.
        SendMessage_(hwnd, #WM_SETFONT, FontID_Body, 1)
        UnhookWindowsHookEx_(Hook)

        ProcedureReturn #False
      EndIf

    Case "BUTTON"
      If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        ;Change font Buttons.
        SendMessage_(hwnd, #WM_SETFONT, FontID_Button, 1)

        ProcedureReturn #True
      EndIf
  EndSelect
  
  ProcedureReturn #True
EndProcedure

Procedure.l HookProc_HaWi(nCode, wParam, lParam)
  If nCode = #HCBT_ACTIVATE
    ;Find text control and Change Font.
    EnumChildWindows_(wParam, @ChangeTextFont(), 0)

    ;Change Button(s) Text.
    If IDYesText
      SetDlgItemText_ (wParam, #IDYES,    IDYesText)
      SetDlgItemText_ (wParam, #IDNO,     IDNoText)
      SetDlgItemText_ (wParam, #IDCANCEL, IDCancelText)
    EndIf
    UnhookWindowsHookEx_(Hook)
  EndIf

  ;ProcedureReturn CallNextHookEx_(Hook, nCode, wParam, lParam)
EndProcedure

;-Message requester with customized font/button captions.
Procedure.l MessageRequester_HaWi(Title.s, Text.s, flags.l = #PB_MessageRequester_Ok, YesText.s = #NULL$, NoText.s = #NULL$, CancelText.s = #NULL$)
  Protected.l MaxLength, Result.l

  ;Set Button(s) Text, ;NB: "Yes" text controls all buttons:
  ;                                   If set, ALL buttons are changed, If empty, NO buttons are changed.
  IDYesText    = YesText
  IDNoText     = NoText
  IDCancelText = CancelText

  Hook = SetWindowsHookEx_(#WH_CBT, @HookProc_HaWi(), #Null, GetCurrentThreadId_())

  ;Add more space for bigger font, NB: specific changes for Windows 10!!!.
  MaxLength = Len(Text) 
  ;Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)) + #LF$ + #LF$ + " ", flags)                           ;For Big font changes for Windows 10!!!.
  Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)), flags)

  ProcedureReturn Result
EndProcedure

Procedure RequesterCallBack(hWnd, uMsg, wParam, lParam)
  Protected Cr.Rect, Cr_Main.Rect
  Protected.i Width, Height, X, Y

  hWnd = FindWindow_("#32770", #Null)

  If hWnd
    GetWindowRect_(hWnd, Cr.Rect)
    GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect)        ; ##### <----- For every window, how to do it.
    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

Procedure Main()
  OpenWindow(#WIN_Main,0, 0, 320, 140, "Main Window" + ".", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  SetWindowCallback(@RequesterCallBack())
  
  TextGadget(#WIN_Text, 60, 40, 190,30, "Move the window and click on ME" + " " + "!!...", #PB_Text_Center)
  ButtonGadget(#WIN_Button1, 80,  80, 150, 25, "- Click on ME -"): SetGadgetFont(#WIN_Button1, FontID_Button)
  ButtonGadget(#WIN_Button2, 80, 110, 150, 22, "End"): SetGadgetFont(#WIN_Button2, FontID_Button)
EndProcedure

Main()

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()

        Case #WIN_Button1
          MessageTitle = Space(2) + "Info" + "."
          MessageText = #LF$ + "Information" + ":" + #LF$ + #LF$
          MessageText + "The message is centered on the window" + "." + #LF$ + #LF$
          Messagetext + "By clicking on the " + Chr(34) + "OK" + Chr(34) + " button this information window is closed" + "."

          MessageRequester_HaWi(MessageTitle, MessageText, #MB_ICONEXCLAMATION | #PB_MessageRequester_YesNoCancel, "&Yes", "&No", "&Cancel")
        Case #WIN_Button2
          End
      EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow 
kind regards,
Little_Man
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Center MessageRequester

Post by RASHAD »

Hi

Code: Select all

..
..
Global Hook.i,ww,hh
..
..
Procedure RequesterCallBack(hWnd, uMsg, wParam, lParam)
  Protected Cr.Rect, Cr_Main.Rect
  Protected.i Width, Height, X, Y

  hWnd = FindWindow_("#32770", #Null)

  If hWnd
    GetWindowRect_(hWnd, Cr.Rect)
    ww = cr\right-cr\left
    hh = cr\bottom - cr\top
    MoveWindow_(hWnd,GetSystemMetrics_(#SM_CXSCREEN)>>1 - ww>>1,GetSystemMetrics_(#SM_CYSCREEN)>>1 -hh>>1,ww,hh,1)
  EndIf

  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
..
..

Egypt my love
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Center MessageRequester

Post by netmaestro »

I fixed it so it works. I basically just moved your centering code to the hook proc. No callback is necessary when using the hook.

Code: Select all

;My Purebasic version 5.11.

;EnableExplicit

Enumeration
  #WIN_Main
  #WIN_Text
  #WIN_Button1
  #WIN_Button2
EndEnumeration

;-Dialog hook to set font in MessageRequester.
Global Hook.i
Global IDYesText.s, IDNoText.s, IDCancelText.s
Global WinMainX.i, WinMainY.i, WinMainWidth.i, WinMainHeight.i, EventID.i
Global MessageTitle.s, MessageText.s

Global FontID_Body.l   = LoadFont(0, "verdana", 8, #PB_Font_HighQuality | #PB_Font_Italic)
Global FontID_Button.l = LoadFont(1, "verdana", 8, #PB_Font_HighQuality | #PB_Font_Italic | #PB_Font_Bold)

Procedure ChangeTextFont(hwnd, lParam)
  ;Find messagebox text control and buttons.
  Global CN.s = Space(#MAX_PATH)
  
  GetClassName_(hwnd, @CN, #MAX_PATH)
  
  Select UCase(CN)
    Case "STATIC"
      If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        ;Change font Body.
        SendMessage_(hwnd, #WM_SETFONT, FontID_Body, 1)
        UnhookWindowsHookEx_(Hook)
        
        ProcedureReturn #False
      EndIf
      
    Case "BUTTON"
      If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        ;Change font Buttons.
        SendMessage_(hwnd, #WM_SETFONT, FontID_Button, 1)
        
        ProcedureReturn #True
      EndIf
  EndSelect
  
  ProcedureReturn #True
EndProcedure

Procedure.l HookProc_HaWi(nCode, wParam, lParam)
  If nCode = #HCBT_ACTIVATE
    hwnd=wParam
    If hWnd
      GetWindowRect_(hWnd, Cr.Rect)
      GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect)        ; ##### <----- For every window, how to do it.
      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
    
    ;Find text control and Change Font.
    EnumChildWindows_(wParam, @ChangeTextFont(), 0)
    
    ;Change Button(s) Text.
    If IDYesText
      SetDlgItemText_ (wParam, #IDYES,    IDYesText)
      SetDlgItemText_ (wParam, #IDNO,     IDNoText)
      SetDlgItemText_ (wParam, #IDCANCEL, IDCancelText)
    EndIf
    UnhookWindowsHookEx_(Hook)
  EndIf
  
  ;ProcedureReturn CallNextHookEx_(Hook, nCode, wParam, lParam)
EndProcedure

;-Message requester with customized font/button captions.
Procedure.l MessageRequester_HaWi(Title.s, Text.s, flags.l = #PB_MessageRequester_Ok, YesText.s = #Null$, NoText.s = #Null$, CancelText.s = #Null$)
  Protected.l MaxLength, Result.l
  
  ;Set Button(s) Text, ;NB: "Yes" text controls all buttons:
  ;                                   If set, ALL buttons are changed, If empty, NO buttons are changed.
  IDYesText    = YesText
  IDNoText     = NoText
  IDCancelText = CancelText
  
  Hook = SetWindowsHookEx_(#WH_CBT, @HookProc_HaWi(), #Null, GetCurrentThreadId_())
  
  ;Add more space for bigger font, NB: specific changes for Windows 10!!!.
  MaxLength = Len(Text) 
  ;Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)) + #LF$ + #LF$ + " ", flags)                           ;For Big font changes for Windows 10!!!.
  Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)), flags)
  
  ProcedureReturn Result
EndProcedure

; Procedure RequesterCallBack(hWnd, uMsg, wParam, lParam)
;   Protected Cr.Rect, Cr_Main.Rect
;   Protected.i Width, Height, X, Y
;   
;   hWnd = FindWindow_("#32770", #Null)
;   
;   If hWnd
;     GetWindowRect_(hWnd, Cr.Rect)
;     GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect)        ; ##### <----- For every window, how to do it.
;     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

Procedure Main()
  OpenWindow(#WIN_Main,0, 0, 320, 140, "Main Window" + ".", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ;SetWindowCallback(@RequesterCallBack())
  
  TextGadget(#WIN_Text, 60, 40, 190,30, "Move the window and click on ME" + " " + "!!...", #PB_Text_Center)
  ButtonGadget(#WIN_Button1, 80,  80, 150, 25, "- Click on ME -"): SetGadgetFont(#WIN_Button1, FontID_Button)
  ButtonGadget(#WIN_Button2, 80, 110, 150, 22, "End"): SetGadgetFont(#WIN_Button2, FontID_Button)
EndProcedure

Main()

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case #WIN_Button1
          MessageTitle = Space(2) + "Info" + "."
          MessageText = #LF$ + "Information" + ":" + #LF$ + #LF$
          MessageText + "The message is centered on the window" + "." + #LF$ + #LF$
          Messagetext + "By clicking on the " + Chr(34) + "OK" + Chr(34) + " button this information window is closed" + "."
          
          MessageRequester_HaWi(MessageTitle, MessageText, #MB_ICONEXCLAMATION | #PB_MessageRequester_YesNoCancel, "&Yes", "&No", "&Cancel")
        Case #WIN_Button2
          End
      EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow 
BERESHEIT
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Center MessageRequester

Post by Sicro »

On an old program from me, I also wanted the MessageRequester to always open in the center of the program window.
My solution at that time was to temporarily reduce the size of the work area:

Code: Select all

Global MessageBoxTimer, ScreenRECT.RECT

Procedure ResetWorkAreaSize(arg1, arg2, arg3, arg4)
  SystemParametersInfo_(#SPI_SETWORKAREA, 0, @ScreenRECT, 0)
  KillTimer_(MessageBoxTimer, 1)
EndProcedure

Procedure MessageBoxCentered(title$, text$, flags=0)
  Protected winRECT.RECT
  
  SystemParametersInfo_(#SPI_GETWORKAREA, 0, @ScreenRECT, 0)
  GetWindowRect_(WindowID(GetActiveWindow()), @winRECT)
  
  SystemParametersInfo_(#SPI_SETWORKAREA, 0, @winRECT, 0)
  SetTimer_(WindowID(GetActiveWindow()), 1, 1, @ResetWorkAreaSize())
  
  ProcedureReturn MessageRequester(title$, text$, flags)
EndProcedure

Define event

OpenWindow(0, #PB_Ignore, #PB_Ignore, 400, 400, "test", #PB_Window_SystemMenu)
ButtonGadget(0, 20, 20, 100, 30, "Open MsgBox")

Repeat
  event = WaitWindowEvent()
  If event = #PB_Event_Gadget
    MessageBoxCentered("Test", "This is a test!")
  EndIf
Until event = #PB_Event_CloseWindow
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Little_man
Enthusiast
Enthusiast
Posts: 152
Joined: Fri Mar 29, 2013 4:55 pm
Location: The Netherland

Re: Center MessageRequester

Post by Little_man »

Thanks "netmaestro",

in your code the MessageRequester is not Active !...( <-- Fixed).

GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect) ; ##### <----- For every window, how to do it. ( #WIN_Main replaced with GetActiveWindow() ).
WinMainX = WindowX(#WIN_Main) ; ##### <----- For every window, how to do it. ( #WIN_Main replaced with GetActiveWindow() ).
WinMainY = WindowY(#WIN_Main) ; ##### <----- For every window, how to do it. ( #WIN_Main replaced with GetActiveWindow() ).

Little_man
Last edited by Little_man on Wed Dec 19, 2018 12:35 pm, edited 3 times in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Center MessageRequester

Post by Dude »

I've been using the following code forever... the original was here: viewtopic.php?p=266242#p266242

Code: Select all

Procedure RequesterPositionCB(Window,Message,wParam,lParam)
   Shared RequesterPositionX.l,RequesterPositionY.l,RequesterPositionCB.l,RequesterPositionHook.l,RequesterPositionWindow.l
   Select Message
      Case #WM_DESTROY : If RequesterPositionWindow : CloseWindow(RequesterPositionWindow) : RequesterPositionWindow=0 : EndIf : SetWindowLongPtr_(Window,#GWL_WNDPROC,RequesterPositionCB)
      Case #WM_CREATE : SetWindowPos_(Window,0,RequesterPositionX,RequesterPositionY,0,0,#SWP_NOSIZE|#SWP_NOACTIVATE)
      Case #WM_INITDIALOG : SetWindowPos_(Window,0,RequesterPositionX,RequesterPositionY,0,0,#SWP_NOSIZE|#SWP_NOACTIVATE)
   EndSelect
   ProcedureReturn CallWindowProc_(RequesterPositionCB,Window,Message,wParam,lParam)
EndProcedure
Procedure RequesterPositionHook(idHook,wParam,lParam)
   Shared RequesterPositionX.l,RequesterPositionY.l,RequesterPositionCB.l,RequesterPositionHook.l,RequesterPositionWindow.l
   Protected *RequesterPositionCWP.CWPSTRUCT=lParam,Message=*RequesterPositionCWP\Message,Window=*RequesterPositionCWP\hwnd,Result,WindowClass.s=Space(999)
   GetClassName_(Window,WindowClass,990) : If LCase(WindowClass)="inputrequester" : Message=#WM_INITDIALOG : EndIf
   Select Message
      Case #WM_INITDIALOG
         RequesterPositionCB=SetWindowLongPtr_(Window,#GWL_WNDPROC,@RequesterPositionCB())
         Result=CallNextHookEx_(RequesterPositionHook,idHook,wParam,lParam)
         UnhookWindowsHookEx_(RequesterPositionHook)
         RequesterPositionHook=0
      Default
         Result=CallNextHookEx_(RequesterPositionHook,idHook,wParam,lParam)
   EndSelect
   ProcedureReturn Result
EndProcedure
Procedure SetRequesterPosition(x,y)
  Shared RequesterPositionX.l,RequesterPositionY.l,RequesterPositionCB.l,RequesterPositionHook.l,RequesterPositionWindow.l
  RequesterPositionX=x : RequesterPositionY=y
  Protected WindowID
  If Not IsWindow(GetActiveWindow())
    RequesterPositionWindow=OpenWindow(#PB_Any,x,y,0,0,"",#PB_Window_Invisible)
    WindowID=WindowID(RequesterPositionWindow)
  Else
    WindowID=WindowID(GetActiveWindow())
  EndIf
  RequesterPositionHook=SetWindowsHookEx_(#WH_CALLWNDPROC,@RequesterPositionHook(),GetModuleHandle_(0),GetWindowThreadProcessId_(WindowID,0))
EndProcedure

SetRequesterPosition(10,10) : MessageRequester("Message","Hi")
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

Interesting, if you run this code with a hidden gadget, the keyboard inputs are lost?

Code: Select all

Enumeration
  #WIN_Main
  #WIN_Text
  #WIN_Button1
  #WIN_Button2
EndEnumeration
Global Hook.i
Global IDYesText.s, IDNoText.s, IDCancelText.s
Global WinMainX.i, WinMainY.i, WinMainWidth.i, WinMainHeight.i, EventID.i
Global MessageTitle.s, MessageText.s
Global FontID_Body.l   = LoadFont(0, "Consolas", 12, #PB_Font_HighQuality)
Global FontID_Button.l = LoadFont(1, "Consolas", 12, #PB_Font_HighQuality | #PB_Font_Bold)
Procedure ChangeTextFont(hwnd, lParam)
  ;Find messagebox text control and buttons.
  Global CN.s = Space(#MAX_PATH)
  GetClassName_(hwnd, @CN, #MAX_PATH)
  Select UCase(CN)
  Case "STATIC"
    If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
      ;Change font Body.
      SendMessage_(hwnd, #WM_SETFONT, FontID_Body, 1)
      UnhookWindowsHookEx_(Hook)
      ProcedureReturn #False
    EndIf
  Case "BUTTON"
    If GetWindowLong_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
      ;Change font Buttons.
      SendMessage_(hwnd, #WM_SETFONT, FontID_Button, 1)
      ProcedureReturn #True
    EndIf
  EndSelect
  ProcedureReturn #True
EndProcedure
Procedure.l HookProc_HaWi(nCode, wParam, lParam)
  If nCode = #HCBT_ACTIVATE
    hwnd=wParam
    If hWnd
      GetWindowRect_(hWnd, Cr.Rect)
      GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect)        ; ##### <----- For every window, how to do it.
      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
    ;Find text control and Change Font.
    EnumChildWindows_(wParam, @ChangeTextFont(), 0)
    ;Change Button(s) Text.
    If IDYesText
      SetDlgItemText_ (wParam, #IDYES,    IDYesText)
      SetDlgItemText_ (wParam, #IDNO,     IDNoText)
      SetDlgItemText_ (wParam, #IDCANCEL, IDCancelText)
    EndIf
    UnhookWindowsHookEx_(Hook)
  EndIf
EndProcedure
Procedure.l MessageRequester_HaWi(Title.s, Text.s, flags.l = #PB_MessageRequester_Ok, YesText.s = #Null$, NoText.s = #Null$, CancelText.s = #Null$)
  Protected.l MaxLength, Result.l
  ;Set Button(s) Text, ;NB: "Yes" text controls all buttons:
  ;                                   If set, ALL buttons are changed, If empty, NO buttons are changed.
  IDYesText    = YesText
  IDNoText     = NoText
  IDCancelText = CancelText
  Hook = SetWindowsHookEx_(#WH_CBT, @HookProc_HaWi(), #Null, GetCurrentThreadId_())
  MaxLength = Len(Text)
  Protected.i i, nLines = CountString(Text, #CRLF$)
  Text = Text + Space(1.05 * MaxLength)
  For i = 1 To nLines
    Text + #CRLF$
  Next i
  ;Add more space for bigger font, NB: specific changes for Windows 10!!!.
  MaxLength = Len(Text)
  ;Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)) + #LF$ + #LF$ + " ", flags)                           ;For Big font changes for Windows 10!!!.
  Result    = MessageRequester(Title, Text + Space(1.0 * Len(Text)), flags)
  ProcedureReturn Result
EndProcedure
OpenWindow(#WIN_Main,0, 0, 320, 140, "Main Window" + ".", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
TextGadget(#WIN_Text, 60, 40, 190,30, "Move the window and click on ME" + " " + "!!...", #PB_Text_Center)
ButtonGadget(#WIN_Button1, 80,  80, 150, 25, "- Click on ME -")
SetGadgetFont(#WIN_Button1, FontID_Button)
ButtonGadget(#WIN_Button2, 80, 110, 150, 22, "End")
SetGadgetFont(#WIN_Button2, FontID_Button)
Repeat
  EventID = WaitWindowEvent()
  Select EventID
  Case #PB_Event_Gadget
    Select EventGadget()
    Case #WIN_Button1
      Messagetext = "Hiding the button lost my keyboard inputs?" + #CRLF$
      HideGadget(#WIN_Button1,1)  ;<-- Why does this cause keyboard to lose focus?
      MessageRequester_HaWi(MessageTitle, MessageText, #MB_ICONEXCLAMATION | #PB_MessageRequester_YesNoCancel, "&Ok", "&No", "&Abort")
      HideGadget(#WIN_Button1,0)
    Case #WIN_Button2
      Break
    EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow
Last edited by skywalk on Sat Dec 22, 2018 4:24 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Center MessageRequester

Post by netmaestro »

You may have to compile the hook proc as a shared dll in order for the hook to be immune to focus issues.
BERESHEIT
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Center MessageRequester

Post by chi »

this seems to work...

Code: Select all

    Case #WIN_Button1
      Messagetext = "Hiding the button lost my keyboard inputs?" + #CRLF$      
      SetWindowPos_(GadgetID(#WIN_Button1), 0, 0, 0, 0, 0, #SWP_NOSIZE|#SWP_NOMOVE|#SWP_HIDEWINDOW) ;HideGadget(#WIN_Button1,1)  ;<-- Why does this cause keyboard to lose focus?
      MessageRequester_HaWi(MessageTitle, MessageText, #MB_ICONEXCLAMATION | #PB_MessageRequester_YesNoCancel, "&Ok", "&No", "&Abort")      
      SetWindowPos_(GadgetID(#WIN_Button1), 0, 0, 0, 0, 0, #SWP_NOSIZE|#SWP_NOMOVE|#SWP_SHOWWINDOW) ;HideGadget(#WIN_Button1,0)
Et cetera is my worst enemy
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

Chi - did you actually hide the button? Your code shows it commented?

Netmaestro - That goes beyond the scope of my app. I will have to make a custom prompt window to get larger fonts and proper text and location. A pity though since the Message requested() is so easy.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Center MessageRequester

Post by chi »

skywalk wrote:Chi - did you actually hide the button? Your code shows it commented?
hmm, when I replace your "Case #WIN_Button1..." with the few lines I posted above, the "- Click on ME -" button disappears after I click on it and I can cycle through the Yes, No and Abort buttons with Tab (on the newly created MessageBox). After I close this MessageBox, the - Click on ME -" button reappears on the main window...
Did I miss something? :?
Et cetera is my worst enemy
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

Ah, I did not see your api call on my phone screen. :oops:
Yes, that works with just the HideWindow() replacement api.

Code: Select all

  ;HideGadget(#WIN_Button1,1)  ;<-- Why does this cause keyboard to lose focus?
  SetWindowPos_(GadgetID(#WIN_Button1), 0, 0, 0, 0, 0, #SWP_NOSIZE|#SWP_NOMOVE|#SWP_HIDEWINDOW)  ;<-- This works.
  HideGadget(#WIN_Button1,0)   ; api not required to resume.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Center MessageRequester

Post by RASHAD »

Hi skywalk
change :

Code: Select all

Procedure.l HookProc_HaWi(nCode, wParam, lParam)
  If nCode = #HCBT_ACTIVATE
    hwnd=wParam
    SetFocus_(hWnd)     ;*******  Just add *******
    If hWnd
      GetWindowRect_(hWnd, Cr.Rect)
      GetWindowRect_(WindowID(#WIN_Main), Cr_Main.Rect)        ; ##### <----- For every window, how to do it.
      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
..
..

Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

Bingo RASHAD :D
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

ebs asked if anyone had Windows specific code to modify the dialog window for larger fonts?
The buttons and dialog window are clipped with 18 pitch?
If this is doable within the current hook, I could drop building a custom window dialog.
Global FontID_Body.l = LoadFont(0, "Consolas", 18, #PB_Font_HighQuality)
Global FontID_Button.l = LoadFont(1, "Consolas", 18, #PB_Font_HighQuality | #PB_Font_Bold)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply