-
January 9th, 2014, 04:09 PM
#1
[win32] - how can get the text\image rect size from a control?
on my label i'm trying do the autosize:
Code:
void setAutoSize(bool autosize)
{
long s=0;
if (autosize==true)
{
char a[256];
GetWindowText(this->hwnd,a,256);
SIZE b;
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
GetTextExtentPoint32(hdc, a, strlen(a) ,&b);
SelectObject(hdc, hOldFont);
ReleaseDC(hwnd,hdc);
RECT c;
GetWindowRect(hwnd,&c);
c.bottom=b.cy;
c.right=b.cx+2;
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
AdjustWindowRectEx (&c,g,FALSE,s );
SetWindowPos(hwnd, 0, 0, 0, c.right, c.bottom,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
these code works fine, but if i use '\t' or '\n' with string(char*) these are ignored or considered 1 char.
so is there another way for get the HDC\bitmap\rect of the inside of the control correctly?
-
January 9th, 2014, 05:26 PM
#2
Re: [win32] - how can get the text\image rect size from a control?
You may want to look at the DrawText function
Victor Nijegorodov
-
January 9th, 2014, 05:34 PM
#3
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by VictorN
sorry, but the DrawText() is only for draw the text, but how can i get the HDC rectangle size? or the string rectangle?
-
January 10th, 2014, 04:04 AM
#4
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Cambalinho
sorry, but the DrawText() is only for draw the text, but how can i get the HDC rectangle size? or the string rectangle?
Did zou read the DrawText documentation before asserting this? Or jasu looked into your crystal ball?
Victor Nijegorodov
-
January 14th, 2014, 03:55 PM
#5
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by VictorN
Did zou read the DrawText documentation before asserting this? Or jasu looked into your crystal ball?
sorry, you have right.. sorry.
the DrawText() with DT_CALCRECT is only for give us the text rectangle and don't print\draw the text.
i did the code:
Code:
void setAutoSize(bool autosize)
{
if (autosize==true)
{
char a[256];
GetWindowText(this->hwnd,a,256);
RECT c = { 0, 0, 0, 0 };
DrawText(GetDC(hwnd), a, strlen(a), &c, DT_CALCRECT);
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
AdjustWindowRectEx (&c,g,FALSE,s );
SetWindowPos(hwnd, 0, 0, 0, c.right+2, c.bottom+2,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
but i see 2 problems:
1 - why the AdjustWindowRectEx() don't give me the right results with border?(it's way i add 2 pixels more);
2 - if the DrawText() is for formated text, why the '\t' count\lenght is ignored on width?(maybe these problem happens, too, with '\v')
-
January 14th, 2014, 04:53 PM
#6
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Cambalinho
2 - if the DrawText() is for formated text, why the '\t' count\lenght is ignored on width?
What did you expect the length of \t to be?
Regards,
Paul McKenzie
-
January 14th, 2014, 04:59 PM
#7
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Paul McKenzie
What did you expect the length of \t to be?
Regards,
Paul McKenzie
on text, show me 5 spaces(i think is that). so it's the string lenght plus the tab spaces. but when i use the GetTextWindow(), maybe, don't gets the tab count
what you can tell me?
-
January 14th, 2014, 05:12 PM
#8
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Cambalinho
on text, show me 5 spaces(i think is that).
Why 5? Why not 6? or 3? or 1? or 10?
The point is that a /t has no specified length. A /t is whatever the program that uses it wants it to be. It could be 0 spaces, 1 space, 20 spaces, 100 spaces, or just a dot on the screen.
A tab is an invisible control character. Yes, it is used by some programs to denote "some space", but that is up to the program what it does with it.
I bet that the text control throws the tab away, thinking it is a junk character. If you want proof, attempt to read back the text in the control. Do you see a tab at the end? If you do, then the system interpreted the /t as 1 character.
Regards,
Paul McKenzie
-
January 14th, 2014, 05:19 PM
#9
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Paul McKenzie
Why 5? Why not 6? or 3? or 1? or 10?
The point is that a /t has no specified length. A /t is whatever the program that uses it wants it to be. It could be 0 spaces, 1 space, 20 spaces, 100 spaces, or just a dot on the screen.
A tab is an invisible control character. Yes, it is used by some programs to denote "some space", but that is up to the program what it does with it.
I bet that the text control throws the tab away, thinking it is a junk character. If you want proof, attempt to read back the text in the control. Do you see a tab at the end? If you do, then the system interpreted the /t as 1 character.
Regards,
Paul McKenzie
in static text, the tab is showed... but why isn't counted with drawtext() function?
-
January 14th, 2014, 05:26 PM
#10
Re: [win32] - how can get the text\image rect size from a control?
Originally Posted by Cambalinho
in static text, the tab is showed... but why isn't counted with drawtext() function?
Did you read the DrawText() documentation?
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
What does it say about tabs?
This is one of many times you haven't read the documentation for various functions you're using -- please read the documentation, as it is important in getting any Windows API program to run properly. This means reading all of the various parameter information, what each parameter does, what the return values are, what you should return back to the OS if you process a message, etc.
Regards,
Paul McKenzie
-
January 14th, 2014, 05:37 PM
#11
Re: [win32] - how can get the text\image rect size from a control?
"This is one of many times you haven't read the documentation for various functions you're using -- please read the documentation, as it is important in getting any Windows API program to run properly. This means reading all of the various parameter information, what each parameter does, what the return values are, what you should return back to the OS if you process a message, etc."
sorry, you have right:
Code:
void setAutoSize(bool autosize)
{
if (autosize==true)
{
char a[256];
GetWindowText(this->hwnd,a,256);
RECT c = { 0, 0, 0, 0 };
DrawText(GetDC(hwnd), a, strlen(a), &c, DT_CALCRECT|DT_EXPANDTABS);
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
AdjustWindowRectEx (&c,g,FALSE,s );
SetWindowPos(hwnd, 0, 0, 0, c.right+2, c.bottom+2,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
now works fine.... thanks.
i use with DT_EXPANDTABS.
let me ask anotherthing: why, using the AdjustWindowRectEx() function, i must add 2 for show the strings correctly?
Last edited by Cambalinho; January 14th, 2014 at 05:39 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|