C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2008, 05:08 AM   #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
csonx_p is offline   Reply With Quote
Old 05-27-2008, 05:37 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-27-2008, 06:51 AM   #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()?
csonx_p is offline   Reply With Quote
Old 05-27-2008, 06:57 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-27-2008, 07:12 AM   #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...
csonx_p is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
the definition of a mathematical "average" or "mean" DavidP A Brief History of Cprogramming.com 7 12-03-2002 11:15 AM
Homework help Jigsaw C++ Programming 2 03-06-2002 05:56 PM
A (complex) question on numbers Unregistered C++ Programming 8 02-03-2002 06:38 PM
negative numbers in a edit box Isometric Windows Programming 2 12-19-2001 09:51 PM
can anyone help me give a code about a program that will input positive numbers... vigen00 C Programming 1 10-01-2001 10:39 AM


All times are GMT -6. The time now is 03:18 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22