CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [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 offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
    1,355

    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 offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
    1,355

    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,449

    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

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

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

    Quote Originally Posted by Paul McKenzie View Post
    What did you expect the length of \t to be?

    Regards,

    Paul McKenzie
    on text, show me 5 spaces(i think is that). so it's the string lenght plus the tab spaces. but when i use the GetTextWindow(), maybe, don't gets the tab count
    what you can tell me?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    on text, show me 5 spaces(i think is that).
    Why 5? Why not 6? or 3? or 1? or 10?

    The point is that a /t has no specified length. A /t is whatever the program that uses it wants it to be. It could be 0 spaces, 1 space, 20 spaces, 100 spaces, or just a dot on the screen.

    A tab is an invisible control character. Yes, it is used by some programs to denote "some space", but that is up to the program what it does with it.

    I bet that the text control throws the tab away, thinking it is a junk character. If you want proof, attempt to read back the text in the control. Do you see a tab at the end? If you do, then the system interpreted the /t as 1 character.

    Regards,

    Paul McKenzie

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

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

    Quote Originally Posted by Paul McKenzie View Post
    Why 5? Why not 6? or 3? or 1? or 10?

    The point is that a /t has no specified length. A /t is whatever the program that uses it wants it to be. It could be 0 spaces, 1 space, 20 spaces, 100 spaces, or just a dot on the screen.

    A tab is an invisible control character. Yes, it is used by some programs to denote "some space", but that is up to the program what it does with it.

    I bet that the text control throws the tab away, thinking it is a junk character. If you want proof, attempt to read back the text in the control. Do you see a tab at the end? If you do, then the system interpreted the /t as 1 character.

    Regards,

    Paul McKenzie
    in static text, the tab is showed... but why isn't counted with drawtext() function?

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    in static text, the tab is showed... but why isn't counted with drawtext() function?
    Did you read the DrawText() documentation?

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    What does it say about tabs?

    This is one of many times you haven't read the documentation for various functions you're using -- please read the documentation, as it is important in getting any Windows API program to run properly. This means reading all of the various parameter information, what each parameter does, what the return values are, what you should return back to the OS if you process a message, etc.

    Regards,

    Paul McKenzie

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

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

    "This is one of many times you haven't read the documentation for various functions you're using -- please read the documentation, as it is important in getting any Windows API program to run properly. This means reading all of the various parameter information, what each parameter does, what the return values are, what you should return back to the OS if you process a message, etc."


    sorry, you have right:
    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+2, c.bottom+2,
                SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
                SWP_DRAWFRAME | SWP_FRAMECHANGED);
            }
        }
    now works fine.... thanks.
    i use with DT_EXPANDTABS.
    let me ask anotherthing: why, using the AdjustWindowRectEx() function, i must add 2 for show the strings correctly?
    Last edited by Cambalinho; January 14th, 2014 at 05:39 PM.

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