-
January 8th, 2014, 03:26 PM
#1
[RESOLVED] how auto size a control?
how can i auto size a control?
i'm trying these code without sucess
Code:
void setAutoSize(bool autosize)
{
long s=0;
if (autosize==true)
{
char a[256];
GetWindowText(hwnd,a,256);
SIZE b;
GetTextExtentPoint32(GetDC(hwnd),a,strlen(a),&b);
RECT c;
GetWindowRect(hwnd,&c);
c.bottom=c.top+b.cy;
c.right=c.left+b.cx;
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
AdjustWindowRectEx (&c,g,FALSE,s );
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED| SWP_NOSIZE);
}
}
i even try the ControlSpyV6(a program for test and combine the styles and extended styles) without sucess
can anyone advice me?
-
January 8th, 2014, 03:49 PM
#2
Re: how auto size a control?
1. Define "auto size a control".
2. You call SetWindowPos with the flag SWP_NOSIZE - it means you do NOT change the size of the control!
Victor Nijegorodov
-
January 8th, 2014, 03:56 PM
#3
Re: how auto size a control?
Originally Posted by VictorN
1. Define "auto size a control".
2. You call SetWindowPos with the flag SWP_NOSIZE - it means you do NOT change the size of the control!
i mean resize for show us the text or resize until the text(and don't forget the borders styles)
-
January 8th, 2014, 03:59 PM
#4
Re: how auto size a control?
Besides, please read the docs about GetDC, particularly the Remark section!
Victor Nijegorodov
-
January 8th, 2014, 04:17 PM
#5
Re: how auto size a control?
Originally Posted by VictorN
Besides, please read the docs about GetDC, particularly the Remark section!
correct me these: verytime that i use the GetDC() i must use ReleaseDC() for 'clean' the HDC variable?
so, to be correctly, i must do these:
1 - obtain the font width and height from the text;
2 - resize the control.
i'm be back... thanks
-
January 8th, 2014, 04:43 PM
#6
Re: how auto size a control?
Originally Posted by Cambalinho
correct me these: verytime that i use the GetDC() i must use ReleaseDC() for 'clean' the HDC variable?
so, to be correctly, i must do these:
1 - obtain the font width and height from the text;
2 - resize the control.
i'm be back... thanks
Again, read the docs!
Remarks
The GetDC function retrieves a common, class, or private DC depending on the class style of the specified window. For class and private DCs, GetDC leaves the previously assigned attributes unchanged. However, for common DCs, GetDC assigns default attributes to the DC each time it is retrieved. For example, the default font is System, which is a bitmap font. Because of this, the handle to a common DC returned by GetDC does not tell you what font, color, or brush was used when the window was drawn. To determine the font, call GetTextFace.
Note that the handle to the DC can only be used by a single thread at any one time.
After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
So, you must do:
1 - obtain the font set in your control and select it in the dc used to get text extent
2 - obtain the width and height from the text;
3 - resize the control.
See also:
http://forums.codeguru.com/showthrea...+gettextextent
http://forums.codeguru.com/showthrea...+gettextextent
Victor Nijegorodov
-
January 16th, 2014, 08:04 PM
#7
Re: how auto size a control?
Originally Posted by Cambalinho
i mean resize for show us the text or resize until the text(and don't forget the borders styles)
Just want to correct you about something. You realize that by using WinAPIs & GDI in particular, you're going back to the technology of the early 90's, if not earlier. So there's really no auto-resizing there. All this stuff comes in a very low-level form. If you are expecting Xcode Interface Builder like controls, or even Visual Studio 2013 modern UI style controls, then you should reconsider your IDE. Possibly C# is a better approach (if you want to stick with Windows.)
-
January 17th, 2014, 03:17 PM
#8
Re: how auto size a control?
now i can resize the control to text:
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+3, c.bottom+3,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
i have 1 question: if AdjustWindowRectEx() give me the size with border(or not border), why i must add '+3' for get the more precise size?
-
January 17th, 2014, 07:13 PM
#9
Re: how auto size a control?
Originally Posted by dc_2000
Just want to correct you about something. You realize that by using WinAPIs & GDI in particular, you're going back to the technology of the early 90's, if not earlier. So there's really no auto-resizing there. All this stuff comes in a very low-level form. If you are expecting Xcode Interface Builder like controls, or even Visual Studio 2013 modern UI style controls, then you should reconsider your IDE (I think you ment languages). Possibly C# is a better approach (if you want to stick with Windows.)
I just wanted to emphasize this, since it's right on the money.
Last edited by STLDude; January 17th, 2014 at 07:17 PM.
-
January 18th, 2014, 04:07 AM
#10
Re: how auto size a control?
Originally Posted by STLDude
I just wanted to emphasize this, since it's right on the money.
...but look at the fun Cambalinho's having doing it like this!
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 18th, 2014, 04:35 AM
#11
Re: how auto size a control?
Originally Posted by Cambalinho
now i can resize the control to text:
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);
...
}
}
i have 1 question: if AdjustWindowRectEx() give me the size with border(or not border), why i must add '+3' for get the more precise size?
I don't see where you set the font to device context.
Victor Nijegorodov
-
January 18th, 2014, 05:32 PM
#12
Re: how auto size a control?
Originally Posted by VictorN
I don't see where you set the font to device context.
yes... you have right:
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);
SelectObject(hdc, hOldFont);
//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);
//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+3, c.bottom+3,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED);
}
}
but why i must add 3 pixels for show the text correctly??
i belive that i realy can't use the '+3', because of the border variants or otherthings... so how can i correct these?
-
January 20th, 2014, 04:23 AM
#13
Re: how auto size a control?
Your code is wrong!
Why do you select the hOldFont again right after secting the correct one?
You must:
1. select the correct font
2. call DrawText
3. reselect the old font
Victor Nijegorodov
-
January 20th, 2014, 07:18 AM
#14
Re: how auto size a control?
Originally Posted by VictorN
Your code is wrong!
Why do you select the hOldFont again right after secting the correct one?
You must:
1. select the correct font
2. call DrawText
3. reselect the old font
thanks for that... but my problem continues, because the begining border starts very close to last letter, is these normal?
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
-
January 20th, 2014, 08:06 AM
#15
Re: how auto size a control?
the begining border starts very close to last letter, is these normal
The size returned using DT_CALCRECT is the minimum size required to hold the text. If you want a small space around the text then I suspect you'll need to add this 'extra' to the width. Same applies for the height if you want a small space below the characters.
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)
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
|