Thread: Vertically centering text in a (single-line) edit/static control

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Vertically centering text in a (single-line) edit/static control

    Is it possible to vertically center text in controls and if it is, how?
    I've tried searching this forum and the MSDN library with no results. :-(
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Create the edit box with the ES_CENTER style.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    I am, but it's only centering the text horizontally. It's still aligning the text to the top.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I don't know of a standard method to vertically center text but here is a method you could use:
    1. Get the height of the text in the edit box.
    2. Get the height of the edit box using GetClientRect.
    3. Do the calculation to calculate the postion of centered text:
    Code:
    top = (client_height - font_height) / 2
    4. Use this value in a call to EM_SETRECT.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Hm. That's a good idea, but it appears the EM_SETRECT only works for multiline edit controls.
    I guess the easiest solution for the statics would then be to just do the drawing myself, using the text metrics approach you described. Thanks for the help!
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  2. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM
  3. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 04:39 AM