Click to See Complete Forum and Search --> : TextOut question


vironic
February 6th, 2007, 08:08 PM
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...

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?

kirants
February 7th, 2007, 12:06 AM
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 ?

vironic
February 7th, 2007, 05:25 AM
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...

//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....

Boris K K
February 7th, 2007, 07:09 AM
Why not simply add a static control and update its text whenever the length of the text in the edit changes?

vironic
February 7th, 2007, 08:22 AM
Do you mean make another control (window) within the parent window? Wouldn't I just get the same problem?

kirants
February 7th, 2007, 11:02 AM
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.

Boris K K
February 8th, 2007, 03:03 AM
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).