[win32] - how can get the text\image rect size from a control?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [win32] - how can get the text\image rect size from a control?

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    734

    [win32] - how can get the text\image rect size from a control?

    on my label i'm trying do the autosize:
    Code:
    void setAutoSize(bool autosize)
        {
            long s=0;
            if (autosize==true)
            {
                char a[256];
                GetWindowText(this->hwnd,a,256);
                SIZE b;
                HDC hdc = GetDC(hwnd);
                HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
                HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
    
                GetTextExtentPoint32(hdc, a, strlen(a) ,&b);
    
                SelectObject(hdc, hOldFont);
                ReleaseDC(hwnd,hdc);
                RECT c;
                GetWindowRect(hwnd,&c);
                c.bottom=b.cy;
                c.right=b.cx+2;
                LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
                LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
                AdjustWindowRectEx (&c,g,FALSE,s );
                SetWindowPos(hwnd, 0, 0, 0, c.right, c.bottom,
                SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
                SWP_DRAWFRAME | SWP_FRAMECHANGED);
            }
        }
    these code works fine, but if i use '\t' or '\n' with string(char*) these are ignored or considered 1 char.
    so is there another way for get the HDC\bitmap\rect of the inside of the control correctly?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: [win32] - how can get the text\image rect size from a control?

    You may want to look at the DrawText function
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    734

    Re: [win32] - how can get the text\image rect size from a control?

    Quote Originally Posted by VictorN View Post
    You may want to look at the DrawText function
    sorry, but the DrawText() is only for draw the text, but how can i get the HDC rectangle size? or the string rectangle?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: [win32] - how can get the text\image rect size from a control?

    Quote Originally Posted by Cambalinho View Post
    sorry, but the DrawText() is only for draw the text, but how can i get the HDC rectangle size? or the string rectangle?
    Did zou read the DrawText documentation before asserting this? Or jasu looked into your crystal ball?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2009
    Posts
    734

    Re: [win32] - how can get the text\image rect size from a control?

    Quote Originally Posted by VictorN View Post
    Did zou read the DrawText documentation before asserting this? Or jasu looked into your crystal ball?
    sorry, you have right.. sorry.
    the DrawText() with DT_CALCRECT is only for give us the text rectangle and don't print\draw the text.
    i did the code:
    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);
                LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
                LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
                AdjustWindowRectEx (&c,g,FALSE,s );
                SetWindowPos(hwnd, 0, 0, 0, c.right+2, c.bottom+2,
                SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
                SWP_DRAWFRAME | SWP_FRAMECHANGED);
            }
        }
    but i see 2 problems:
    1 - why the AdjustWindowRectEx() don't give me the right results with border?(it's way i add 2 pixels more);
    2 - if the DrawText() is for formated text, why the '\t' count\lenght is ignored on width?(maybe these problem happens, too, with '\v')

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: [win32] - how can get the text\image rect size from a control?

    Quote Originally Posted by Cambalinho View Post
    2 - if the DrawText() is for formated text, why the '\t' count\lenght is ignored on width?
    What did you expect the length of \t to be?

    Regards,

    Paul McKenzie

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