dcsimg
Results 1 to 16 of 16

Thread: [RESOLVED] Form Titlebar Text Centering

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Resolved [RESOLVED] Form Titlebar Text Centering

    When I have Form1.Caption = "Some Text" it always shows it on the left. How can I center the text

  2. #2
    Lively Member
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    116

    Re: Form Titlebar Text Centering

    Quote Originally Posted by Code Dummy View Post
    When I have Form1.Caption = "Some Text" it always shows it on the left.
    No it doesn't. At least not under Windows 8.1. It's centered here.

    Quote Originally Posted by Code Dummy View Post
    How can I center the text
    I'd leave that to the System's discretion. Probably except if you're targeting at one specific OS version.

    Wolfgang

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,935

    Re: Form Titlebar Text Centering

    Here...this works (at least) on Windows 10

    centerFormCaption.zip

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,857

    Re: Form Titlebar Text Centering

    I also would advise letting the system decide where it should go, better to have it consistent with other programs. If you code it in such a way as to pad it to the center on one system it may get pushed off to the right on another.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Re: Form Titlebar Text Centering

    I don't see what difference it makes about the OS. If you know the width of your Form isn't that the same on all OS's even if the width differs in size from one OS to another the value you get from Form1.Width would be correct. So, knowing the width how do I center text

  6. #6
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,685

    Re: Form Titlebar Text Centering

    Quote Originally Posted by SamOscarBrown View Post
    Here...this works (at least) on Windows 10

    centerFormCaption.zip
    This code is not taking into account the size of the font, hence here where I have set a bigger font it does not center the text properly.

    To fix that, I addded this line after the line formName.Font.Name = strPuffer:

    Code:
    formName.Font.Size = -ncm.lfCaptionFont.lfHeight / (1440 / Screen.TwipsPerPixelY) * 72
    Still, it is not taking into account if the font is bold and italic, but those are less likely to be changed.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,935

    Re: Form Titlebar Text Centering

    Correct Ed....but I would have hope my example would suffice in 95% of cases. :-)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Re: Form Titlebar Text Centering

    It doesn't work on XP

  9. #9
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,685

    Re: Form Titlebar Text Centering

    The WM_GETTITLEBARINFOEX message is available from Vista and later.

    Attached is a modified version that works also on XP.

    Note: it assumes that there are the three buttons at the title bar. If it is only the close button it will require a change (to delete '* 3.05').
    Attached Files Attached Files

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Re: Form Titlebar Text Centering

    Quote Originally Posted by Eduardo- View Post
    The WM_GETTITLEBARINFOEX message is available from Vista and later.

    Attached is a modified version that works also on XP.

    Note: it assumes that there are the three buttons at the title bar. If it is only the close button it will require a change (to delete '* 3.05').
    So does that mean to delete this line

    Code:
    theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * 3.05 * 15 - lngTitle) / 2

  11. #11
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,685

    Re: Form Titlebar Text Centering

    Quote Originally Posted by Code Dummy View Post
    So does that mean to delete this line

    Code:
    theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * 3.05 * 15 - lngTitle) / 2
    I said to delete '* 3.05', not the whole line.
    If you delete the whole line it won't work.

    But only there is the need to delete '* 3.05' from the line in the case that the form has just the close button and not the minimize and maximize buttons.

    Or even better, do it automatically. Replace the line with this one:

    Code:
    theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * If(formName.MinButton, 3.05, 1) * 15 - lngTitle) / 2
    Attached Files Attached Files
    Last edited by Eduardo-; May 19th, 2019 at 02:57 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Re: Form Titlebar Text Centering

    Got it, thanks Eduardo

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,935

    Re: Form Titlebar Text Centering

    Quote Originally Posted by Code Dummy View Post
    It doesn't work on XP
    What are you still doing on XP?

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    461

    Re: Form Titlebar Text Centering

    Why do some people drive 25+ year old cars

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,935

    Re: Form Titlebar Text Centering

    Touche

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,935

    Re: Form Titlebar Text Centering

    But I have an 85 year old car ('34 Plymouth Coupe) that I drive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width