[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?
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!
Re: how auto size a control?
Quote:
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)
Re: how auto size a control?
Besides, please read the docs about GetDC, particularly the Remark section!
Re: how auto size a control?
Quote:
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
Re: how auto size a control?
Quote:
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!
Quote:
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
Re: how auto size a control?
Quote:
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.)
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?
Re: how auto size a control?
Quote:
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.
Re: how auto size a control?
Quote:
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! :D
Re: how auto size a control?
Quote:
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.
Re: how auto size a control?
Quote:
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?
Re: how auto size a control?
Your code is wrong!
Why do you select the hOldFont again right after secting the correct one? :confused:
You must:
1. select the correct font
2. call DrawText
3. reselect the old font
1 Attachment(s)
Re: how auto size a control?
Quote:
Originally Posted by
VictorN
Your code is wrong!
Why do you select the hOldFont again right after secting the correct one? :confused:
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?
Attachment 32281
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 :(
Re: how auto size a control?
Quote:
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.