CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: how auto size a control?

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

  2. #17
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how auto size a control?

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

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

    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

  4. #19
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: [RESOLVED] how auto size a control?

    Please, define "use 1 HDC if was release lol".
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: [RESOLVED] how auto size a control?

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

  6. #21
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: [RESOLVED] how auto size a control?

    Quote Originally Posted by Cambalinho View Post
    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: how auto size a control?

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

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

    Re: how auto size a control?

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

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

    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)

  10. #25
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] how auto size a control?

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

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

    Re: how auto size a control?

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

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

    Re: how auto size a control?

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

Page 2 of 2 FirstFirst 12

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