Center MessageRequester

Just starting out? Need help? Post your questions and find answers here.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Center MessageRequester

Post by RASHAD »

Hi again skywalk
You are welcome and Happy new coming year
You mean this

Code: Select all

Global Hook.l
Global IDYesText.s, IDNoText.s, IDCancelText.s

Global FontID_12Regular.L, FontID_12Bold.L

Procedure ChangeTextFont(hwnd, lParam)
  ; find messagebox text control and buttons
  CN.S = Space(#MAX_PATH)
  GetClassName_(hwnd, @CN, #MAX_PATH)
  Select UCase(CN)
    Case "STATIC"
      If GetWindowLongPtr_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        ; change font
        SendMessage_(hwnd, #WM_SETFONT,FontID_12Regular, 1)
        UnhookWindowsHookEx_(Hook)
        ProcedureReturn 0
      EndIf
    Case "BUTTON"
      If GetWindowLongPtr_(hwnd, #GWL_STYLE) & #SS_BITMAP = 0
        SetWindowPos_(hwnd, 0, 0, 0, 80, 25,  #SWP_NOZORDER |#SWP_NOMOVE)
        ; change font
        SendMessage_(hwnd, #WM_SETFONT, FontID_12Bold, 1)
        ProcedureReturn #True
      EndIf
  EndSelect
 
  ProcedureReturn #True
EndProcedure

Procedure.L HookProc(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 MessageRequesterEBS(Title.S, Text.S, flags.L = #PB_MessageRequester_Ok, YesText.S = #Null$, NoText.S = #Null$, CancelText.S = #Null$)
  ; 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(), #Null, GetCurrentThreadId_())
 
  ; add more space for bigger font
  ; NB: specific changes for Windows 10!!!
  MaxLength.L = Len(Text)
  result.L = MessageRequester(Title, Text + Space(1.0 * Len(Text)) + #LF$ + #LF$ + " ", flags)
 
  ProcedureReturn result
EndProcedure

FontID_12Regular = LoadFont(0, "Cosolas", 20)
FontID_12Bold = LoadFont(1, "Tahoma", 12, #PB_Font_Bold|#PB_Font_HighQuality)

MessageRequesterEBS("Change Font in Message Requester", "This is a bigger font (20 pt)" + #LF$ + "in a Message Requester", #MB_ICONINFORMATION|#PB_MessageRequester_YesNoCancel, "One", "Two", "Three")
Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 3996
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Center MessageRequester

Post by skywalk »

Happy almost New Year!
Yes, that is the code that fails with larger fonts, starting at fontsize>=16.
The buttons and dialog window apparently are sized prior to the hook changes.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply