CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2007
    Posts
    19

    TextOut question

    I have a question regarding the TextOut function. I have a window with an edit control. When text is entered in the edit control it displays the length of the text in the parent window. Here is the code...

    Code:
    case WM_COMMAND:
                     switch( HIWORD(wParam) ){
                           
                             //what to do if edit control is updated:
                             case EN_UPDATE:
                                  int length;
                                  std::stringstream s;
                                  char StringOne[] = "Text Length:  ";
    
                                  length = SendMessage( (HWND)lParam, WM_GETTEXTLENGTH, 0, 0 );
                                  s << length;//convert int to string
                                  strcat( StringOne, s.str().c_str() );//concat strings together
                                  hdc = GetDC( hwnd );
                                  DrawText( hdc, StringOne, strlen(StringOne), &rect, DT_LEFT);
                                  ReleaseDC( hwnd, hdc );
                             break;
                     }
    break;


    It works fine until I hit backspace and go from single digit to double digit numbers. For example, if I have 9 characters in the edit control it says...

    Text Length: 9

    If I enter one more character, it says...

    Text Length: 10

    But if I hit backspace, it says...

    Text Length: 90

    When the string gets shorter it leaves that 0 remaining in the window. How do I set TextOut() so that when the output string gets shorter it erases the end of the last string?

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: TextOut question

    DrawText from the EN_UPDATE handler is not a good place to do this. Try this: enter the text, any no. of characters. Assume , it shows the right text in your window. Now, switch to another application so it covers your window and now switch back to your application. Is the text still there ?

  3. #3
    Join Date
    Jan 2007
    Posts
    19

    Re: TextOut question

    No, the text isn't there. I guess that means I need to do this in the WM_PAINT message switch case. When I use WM_PAINT, my code looks like this...

    Code:
    //declared at the start of WndProc
    static int length;   
    char StringOne[] = "Text Length:  ";
    
    case WM_COMMAND:
                     switch( HIWORD(wParam) ){
                           
                             //what to do if edit control is updated:
                             case EN_UPDATE:
                                  length = SendMessage( (HWND)lParam, WM_GETTEXTLENGTH, 0, 0 );
                             break;
                     }
    break;
               
    case WM_PAINT:
             hdc = BeginPaint( hwnd, &ps );
             s << length;//convert int to string
             strcat( StringOne, s.str().c_str() );//concat strings together
             DrawText( hdc, StringOne, strlen(StringOne), &rect, DT_LEFT);
             EndPaint( hwnd, &ps );
    break;

    Now the string is there when I minimize or cover the window and refocus on it. However, when I enter text into the edit control the text length isn't updated in real time. So while the window is in focus, if I enter text in the edit control, it doesn't update the "Text Length: " until I minimize and maximize again. I'm not sure what to do next....
    Last edited by vironic; February 7th, 2007 at 06:58 AM.

  4. #4
    Join Date
    Aug 2004
    Posts
    294

    Re: TextOut question

    Why not simply add a static control and update its text whenever the length of the text in the edit changes?
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  5. #5
    Join Date
    Jan 2007
    Posts
    19

    Re: TextOut question

    Do you mean make another control (window) within the parent window? Wouldn't I just get the same problem?

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: TextOut question

    Quote Originally Posted by vironic
    I guess that means I need to do this in the WM_PAINT message switch case.
    You got it.
    Now the string is there when I minimize or cover the window and refocus on it. However, when I enter text into the edit control the text length isn't updated in real time. So while the window is in focus, if I enter text in the edit control, it doesn't update the "Text Length: " until I minimize and maximize again. I'm not sure what to do next....
    Traditionally, this is done by forcing a paint, which is done by doing a InvalidateRect followed by UpdateWindow. These 2 calls will queue up a WM_PAINT message.

    Or, as Boris K K says, you can have another child control of your main window( similar to the edit control you have ) of class STATIC and in the EN_UPDATE handler, do a GetDlgItem(id of static item) and then using that HWND, do a SetWindowText. This will cause the static to paint itself.

  7. #7
    Join Date
    Aug 2004
    Posts
    294

    Re: TextOut question

    Quote Originally Posted by vironic
    Do you mean make another control (window) within the parent window? Wouldn't I just get the same problem?
    Yes, this is the most common and the most convenient way to do it. And no, you will not get the same problem because the static controls "knows" how to redraw itself and how to cause a redraw when its text has been changed (as kirants has already noticed).
    Last edited by Boris K K; February 8th, 2007 at 04:10 AM.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

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