CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how auto size a control?

    Quote Originally Posted by VictorN View Post
    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)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how auto size a control?

    Besides, please read the docs about GetDC, particularly the Remark section!
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how auto size a control?

    Quote Originally Posted by VictorN View Post
    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

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how auto size a control?

    Quote Originally Posted by Cambalinho View Post
    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

  7. #7
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: how auto size a control?

    Quote Originally Posted by Cambalinho View Post
    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.)

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    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?

  9. #9
    Join Date
    May 2007
    Posts
    811

    Re: how auto size a control?

    Quote Originally Posted by dc_2000 View Post
    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.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how auto size a control?

    Quote Originally Posted by STLDude View Post
    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)

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how auto size a control?

    Quote Originally Posted by Cambalinho View Post
    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

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how auto size a control?

    Quote Originally Posted by VictorN View Post
    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?

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how auto size a control?

    Quote Originally Posted by VictorN View Post
    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?

    Name:  Autosize problem.gif
Views: 1415
Size:  51.5 KB

    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

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured