[RESOLVED] how auto size a control? - Page 2
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

Thread: [RESOLVED] how auto size a control?

  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    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
    678

    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
    678

    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
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    Re: [RESOLVED] how auto size a control?

    Please, define "use 1 HDC if was release lol".
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #20
    Join Date
    Apr 2009
    Posts
    678

    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
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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 03:13 PM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  7. #22
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    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 05:59 AM.
    Victor Nijegorodov

  8. #23
    Join Date
    Apr 2009
    Posts
    678

    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 05:58 AM.

  9. #24
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #25
    Join Date
    Apr 2009
    Posts
    678

    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
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    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
    678

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center