Thread: Using TextOut() to align positive & negative numbers

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Using TextOut() to align positive & negative numbers

    I'm trying to align a number of floating point values using TextOut() function but not winning because i use it in a loop to display various numbers passed to it as strings i.e ... "35.78". Problem is these floating numbers can either be positive or negative... TextOut uses coordinates meaning the '-' sign forms part of the coordinate x:y, which pushes the number following the sign a little far to the right i.e if i display "-3.56" below 3.56 using the same coordinates, the numbers won't be aligned nicely... With printf() one can use formatted output such as "%-20lf" and so forth...

    Is there a way i can achieve this? I believe i may opt to use a number designated function like SetDlgItemInt().. but need one for floating numbers

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The normal way to deal with "number alignment" is by a left-justification - so you have a "left-hand edge" that is straight, and the right-hand side wherever it needs to be to achieve that.

    So let's say you want your right-hand edge to be at x=200. Then you get the length (in the current font) of the string using GetTextExtentPoint32, and you then start drawing at 200-size.cx - that will make the string END at 200.

    You may also be able to change the alignment of TextOut itself to use "right justified" and set the clipping rectangle to indicate where your text should go.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    The normal way to deal with "number alignment" is by a left-justification - so you have a "left-hand edge" that is straight, and the right-hand side wherever it needs to be to achieve that.

    So let's say you want your right-hand edge to be at x=200. Then you get the length (in the current font) of the string using GetTextExtentPoint32, and you then start drawing at 200-size.cx - that will make the string END at 200.

    You may also be able to change the alignment of TextOut itself to use "right justified" and set the clipping rectangle to indicate where your text should go.

    --
    Mats
    can't i just use _tcslen to get the lenght of the string...? why GetTextExtentPoint32()?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you want to know how many pixels the text is, not how many characters it is. This is for two reasons:
    1. You need to move a number of pixels to the right from your left margin - you need to know that number of pixels.
    2. You may think "well, just multiply the width of a character with a the length of the string". But not all characters are the same width, so you can't just multiply by a fixed number, you have to walk the string and add up the width of each character (e.g. iiii is not as wide as wwww, and in numbers, at least . and - are likely to be different from the width of the 0..9 digits).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Because you want to know how many pixels the text is, not how many characters it is. This is for two reasons:
    1. You need to move a number of pixels to the right from your left margin - you need to know that number of pixels.
    2. You may think "well, just multiply the width of a character with a the length of the string". But not all characters are the same width, so you can't just multiply by a fixed number, you have to walk the string and add up the width of each character (e.g. iiii is not as wide as wwww, and in numbers, at least . and - are likely to be different from the width of the 0..9 digits).

    --
    Mats
    see...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  2. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  3. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  4. negative numbers in a edit box
    By Isometric in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2001, 09:51 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM