Announcement

Collapse
No announcement yet.

Centering the ChooseFont dialog

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Centering the ChooseFont dialog

    Quick question...

    I am trying to center the ChooseFont dialog, I've hooked it and I've
    trapped the WM_INITDIALOG to try and center it. I cut and pasted the
    centering code from Borje's fine "center Open dialog" hook that works
    perfect for the Open Dialog hook.

    Any suggestions that can help me?

    Thanks!
    Jules

    Simplified snippet...

    Code:
    '------------------------------------------------------------------------------
    ' Choose Font dialog Hook procedure to center dialog within main application...
    '------------------------------------------------------------------------------
    FUNCTION ChooseFontDlgHookProc(BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                                   BYVAL wParam AS LONG, BYVAL lParam AS LONG ) AS LONG
     
      LOCAL X AS LONG, Y AS LONG, R AS RECT  
      
      SELECT CASE wMsg
      
         CASE %WM_INITDIALOG
      
                  'CENTER DIALOG IN PARENT
                  GetWindowRect GetParent(GetParent(hWnd)), R    'get parent's data - dialog is child of child..
     
                  X = R.nLeft + ((R.nRight   - R.nLeft) \ 2)      'calculate parent's width
                  Y = R.nTop  + ((R.nBottom  - R.nTop)  \ 2)      'calculate parent's height
     
                  GetWindowRect GetParent(hWnd), R               'get dialog's width and height
                  X = X - (( R.nRight   - R.nLeft ) \ 2)
                  Y = Y - (( R.nBottom  - R.nTop  ) \ 2)
    
                  SetWindowPos GetParent(hWnd), %NULL, X, Y, 0, 0, %SWP_NOSIZE OR %SWP_NOZORDER 'set centered pos 
       END SELECT
    END FUNCTION
      
    '------------------------------------------------------------------------------
    ' Choose Font for a control...
    '------------------------------------------------------------------------------
    SUB ChooseQTPFont(BYVAL hDlg AS LONG)
     
        LOCAL cf AS CHOOSEFONTAPI
        LOCAL lf AS LOGFONT
     
        cf.lStructSize    = SIZEOF(cf)
        cf.lpLogFont      = VARPTR(lf)
     
        cf.Flags          = %CF_SCREENFONTS OR  _
                            %CF_EFFECTS OR _
                            %CF_ENABLEHOOK  OR _
                            %CF_INITTOLOGFONTSTRUCT
       
        'cf.rgbColors      = cfColor
     
        cf.lpfnHook       = CODEPTR(ChooseFontDlgHookProc)
     
        IF ChooseFont(cf) THEN
                MSGBOX lf.lfFaceName
        END IF
     
    END SUB
    Best regards
    Jules
    www.rpmarchildon.com

  • #2
    It's missing one line:
    Code:
    cf.hwndOwner = hDlg
    And assuming that hDlg passed to the ChooseQTPFont() sub is the actual parent, then there is one too many layers of GetParent() involved everywhere.



    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Try this:
      Code:
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
      #INCLUDE "comdlg32.inc"
      '------------------------------------------------------------------------------
      ' Choose Font dialog Hook procedure to center dialog within main application...
      '------------------------------------------------------------------------------
      FUNCTION ChooseFontDlgHookProc(BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                                     BYVAL wParam AS LONG, BYVAL lParam AS LONG ) AS LONG
        LOCAL X AS LONG, Y AS LONG, R AS RECT
       
        SELECT CASE wMsg
           CASE %WM_INITDIALOG
                    'CENTER DIALOG IN PARENT
                    GetWindowRect [b]GetParent(hWnd)[/b], R    'get parent's data 
                    X = R.nLeft + ((R.nRight   - R.nLeft) \ 2)      'calculate parent's width
                    Y = R.nTop  + ((R.nBottom  - R.nTop)  \ 2)      'calculate parent's height
       
                    GetWindowRect [b]hWnd[/b], R               'get dialog's width and height
                    R.nLeft = X - (( R.nRight   - R.nLeft ) \ 2)
                    R.nTop = Y - (( R.nBottom  - R.nTop  ) \ 2)
       
                    SetWindowPos [b]hWnd[/b], %NULL, R.nLeft, R.nTop, 0, 0, %SWP_NOSIZE OR %SWP_NOZORDER 'set centered pos
         END SELECT
      END FUNCTION
       
      '------------------------------------------------------------------------------
      ' Choose Font for a control...
      '------------------------------------------------------------------------------
      SUB ChooseQTPFont(BYVAL hDlg AS LONG)
          LOCAL cf AS CHOOSEFONTAPI
          LOCAL lf AS LOGFONT
       
          cf.lStructSize    = SIZEOF(cf)
          cf.lpLogFont      = VARPTR(lf)
      [b]    cf.hWndOwner      = hDlg[/b]
          cf.Flags          = %CF_SCREENFONTS OR  _
                              %CF_EFFECTS OR _
                              %CF_ENABLEHOOK  OR _
                              %CF_INITTOLOGFONTSTRUCT
          'cf.rgbColors      = cfColor
          cf.lpfnHook       = CODEPTR(ChooseFontDlgHookProc)
       
          IF ChooseFont(cf) THEN
                  MSGBOX lf.lfFaceName
          END IF
      END SUB
       
      CALLBACK FUNCTION cbdlg
          IF CBMSG = %WM_INITDIALOG THEN PostMessage CBHNDL, %WM_USER + 999&, 0, 0
          IF CBMSG = %WM_USER + 999& THEN Chooseqtpfont CBHNDL
      END FUNCTION
       
      FUNCTION PBMAIN
          LOCAL hDlg AS DWORD
          DIALOG NEW 0, "test", 100, 100, 400, 300 TO hDlg
          DIALOG SHOW MODAL hDlg CALL cbdlg
      END FUNCTION
      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        Excellent!

        Thank you Lance, it was the cf.hWndOwner member I overlooked. I have not
        decided if I want it centered to main application window or the modal dialog.
        Tested both, my preference is centering it relative to the popup window.

        Many thanks again...

        Regards,
        Jules
        Best regards
        Jules
        www.rpmarchildon.com

        Comment

        Working...
        X