-
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
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
|