|
-
February 6th, 2007, 09:08 PM
#1
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?
-
February 7th, 2007, 01:06 AM
#2
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 ?
-
February 7th, 2007, 06:25 AM
#3
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.
-
February 7th, 2007, 08:09 AM
#4
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/
-
February 7th, 2007, 09:22 AM
#5
Re: TextOut question
Do you mean make another control (window) within the parent window? Wouldn't I just get the same problem?
-
February 7th, 2007, 12:02 PM
#6
Re: TextOut question
 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.
-
February 8th, 2007, 04:03 AM
#7
Re: TextOut question
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|