Re: how auto size a control?
Quote:
Originally Posted by
Cambalinho
like you see the letter 'i' is very close to the border.. i can add more '+2', but i think that isn't correct in these way :(
I usually use GetTextMetrics to get the TEXTMETRIC data and then add (tmAveCharWidth / 2) to the width calculated with DratText or GetTextExtentPoint32.
Re: how auto size a control?
Quote:
Originally Posted by
VictorN
I usually use GetTextMetrics to get the TEXTMETRIC data and then add (tmAveCharWidth / 2) to the width calculated with DratText or GetTextExtentPoint32.
i'm doing something wrong, because i'm getting big results:
Code:
void setAutoSize(bool autosize)
{
if (autosize==true)
{
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
//Getting the
RECT c = { 0, 0, 0, 0 };
//Getting the text rectangle from actual caption
char a[256];
GetWindowText(this->hwnd,a,256);
DrawText(hdc, a, strlen(a), &c, DT_CALCRECT | DT_EXPANDTABS);
//Clean the Font DC
ReleaseDC(hwnd,hdc);
//add 2 to the size
TEXTMETRIC v;
GetTextMetrics (hdc,&v);
c.right=c.right+(v.tmAveCharWidth / 2);
//Getting the actual styles
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
//change the rectangle size for borders
AdjustWindowRectEx (&c,g,FALSE,s );
//Update the control
SetWindowPos(hwnd, 0, 0, 0, c.right, c.bottom,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
what i'm doing wrong?
Re: how auto size a control?
i found my error lol
how i can use 1 HDC if was release lol
Code:
void setAutoSize(bool autosize)
{
if (autosize==true)
{
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
//Getting the
RECT c = { 0, 0, 0, 0 };
//Getting the text rectangle from actual caption
char a[256];
GetWindowText(this->hwnd,a,256);
DrawText(hdc, a, strlen(a), &c, DT_CALCRECT | DT_EXPANDTABS);
//add 2 to the size
TEXTMETRIC v;
GetTextMetrics (hdc,&v);
c.right=c.right+(v.tmAveCharWidth / 2);
c.bottom=c.bottom+(v.tmAveCharWidth / 2);
//Clean the Font DC
ReleaseDC(hwnd,hdc);
//Getting the actual styles
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
//change the rectangle size for borders
AdjustWindowRectEx (&c,g,FALSE,s );
//Update the control
SetWindowPos(hwnd, 0, 0, 0, c.right, c.bottom,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
thanks for all
Re: [RESOLVED] how auto size a control?
Please, define "use 1 HDC if was release lol".
Re: [RESOLVED] how auto size a control?
Quote:
Originally Posted by
ovidiucucu
Please, define "use 1 HDC if was release lol".
sorry my english: i can't use a HDC from GetDC() after ReleaseDC().
(lol is just a smile)
Re: [RESOLVED] how auto size a control?
Quote:
Originally Posted by
Cambalinho
sorry my english: i can't use a HDC from GetDC() after ReleaseDC().
(lol is just a smile)
You can but you may not. :)
Anyway that's a little bit better. And please note: I know what's "lol".
Re: how auto size a control?
Quote:
Originally Posted by
Cambalinho
i found my error lol
how i can use 1 HDC if was release lol
Code:
void setAutoSize(bool autosize)
{
if (autosize==true)
{
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
...
}
}
You still ignore my posts. :mad:
Again:
Your code is wrong!
Why don't you select the hOldFont before releasing the DC?
You must:
1. select the correct font
2. call DrawText
3. reselect the old font
Re: how auto size a control?
Quote:
Originally Posted by
VictorN
You still ignore my posts. :mad:
Again:
Your code is wrong!
Why don't you select the hOldFont before releasing the DC?
You must:
1. select the correct font
2. call DrawText
3. reselect the old font
sorry... i was understand anotherthing :(
how i can select the correct font?
from SendMessage() i get the actual font, please explain better
realy don't be mad with me i was thinking on anotherthing and i didn't understand what you said... sorry
Re: [RESOLVED] how auto size a control?
Code:
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
Quote:
Why do you select the hOldFont again right after secting the correct one?
I don't see where the hOldFont is being selected again? SelectObject is selecting the correct font into the dc and storing the previous font into hOldFont. Where is this code selecting the hOldFont? :confused:
The code could have been written as
Code:
HDC hdc = GetDC(hwnd);
SelectObject(hdc, (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0));
Re: [RESOLVED] how auto size a control?
Quote:
Originally Posted by
2kaud
Code:
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
I don't see where the hOldFont is being selected again? SelectObject is selecting the correct font into the dc and storing the previous font into hOldFont. Where is this code selecting the hOldFont? :confused:
The code could have been written as
Code:
HDC hdc = GetDC(hwnd);
SelectObject(hdc, (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0));
thanks for all to all
Re: how auto size a control?
Quote:
Originally Posted by
Cambalinho
sorry... i was understand anotherthing :(
how i can select the correct font?
from SendMessage() i get the actual font, please explain better
realy don't be mad with me i was thinking on anotherthing and i didn't understand what you said... sorry
Sorry for "madness"! :cry:
Your code should look like
Code:
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
...
DrawText(hdc, a, strlen(a), &c, DT_CALCRECT | DT_EXPANDTABS);
...
ReleaseDC(hwnd,hdc);
Re: how auto size a control?
Quote:
Originally Posted by
VictorN
Sorry for "madness"! :cry:
Your code should look like
Code:
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
...
DrawText(hdc, a, strlen(a), &c, DT_CALCRECT | DT_EXPANDTABS);
...
ReleaseDC(hwnd,hdc);
corrected ;)