-
January 20th, 2014, 08:19 AM
#16
Re: how auto size a control?
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.
Victor Nijegorodov
-
January 20th, 2014, 02:10 PM
#17
Re: how auto size a control?
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?
-
January 20th, 2014, 02:14 PM
#18
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
-
January 20th, 2014, 03:47 PM
#19
Re: [RESOLVED] how auto size a control?
Please, define "use 1 HDC if was release lol".
-
January 20th, 2014, 03:50 PM
#20
Re: [RESOLVED] how auto size a control?
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)
-
January 20th, 2014, 04:09 PM
#21
Re: [RESOLVED] how auto size a control?
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".
Last edited by ovidiucucu; January 20th, 2014 at 04:13 PM.
-
January 21st, 2014, 02:32 AM
#22
Re: how auto size a control?
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.
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
Last edited by VictorN; January 21st, 2014 at 06:59 AM.
Victor Nijegorodov
-
January 21st, 2014, 06:39 AM
#23
Re: how auto size a control?
Originally Posted by VictorN
You still ignore my posts.
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
Last edited by VictorN; January 21st, 2014 at 06:58 AM.
-
January 21st, 2014, 06:48 AM
#24
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);
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?
The code could have been written as
Code:
HDC hdc = GetDC(hwnd);
SelectObject(hdc, (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0));
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 21st, 2014, 06:56 AM
#25
Re: [RESOLVED] how auto size a control?
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?
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
-
January 21st, 2014, 06:56 AM
#26
Re: how auto size a control?
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"!
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);
Victor Nijegorodov
-
January 21st, 2014, 06:59 AM
#27
Re: how auto size a control?
Originally Posted by VictorN
Sorry for "madness"!
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
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
|