Page 1 of 2

Center MessageRequester

Posted: Tue Dec 18, 2018 6:29 pm
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

Re: Center MessageRequester

Posted: Tue Dec 18, 2018 8:01 pm
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
..
..


Re: Center MessageRequester

Posted: Tue Dec 18, 2018 8:06 pm
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 

Re: Center MessageRequester

Posted: Tue Dec 18, 2018 8:51 pm
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

Re: Center MessageRequester

Posted: Wed Dec 19, 2018 8:41 am
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

Re: Center MessageRequester

Posted: Wed Dec 19, 2018 8:55 am
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")

Re: Center MessageRequester

Posted: Fri Dec 21, 2018 10:50 pm
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

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 2:09 am
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.

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 3:01 am
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)

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 3:12 am
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.

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 12:19 pm
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? :?

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 4:22 pm
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.

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 4:49 pm
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
..
..


Re: Center MessageRequester

Posted: Sat Dec 22, 2018 5:28 pm
by skywalk
Bingo RASHAD :D

Re: Center MessageRequester

Posted: Sat Dec 22, 2018 5:34 pm
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)