[.net] Odd error w/painting

Started by
2 comments, last by SHilbert 19 years, 4 months ago

case ID_ABOUT_HELLOWORLD:
{
	hdc = BeginPaint(hWnd, &ps);
	BeginPaint(hWnd,&ps);
	char szMessage[] = "Omg this is so cool!!!111one";
	TextOut(ps.hdc, 0, 0, szMessage, strlen(szMessage));
      EndPaint(hWnd,&ps);
      break;
}
What i'm trying to do with this is when the HELLOWORLD menu item is clicked it'll print szMessage[] out on the window at 0,0, now i'm new to WinAPI and just started really messing around with VC7 :D, it's fun stuff so far but it confuses me now..it's giving me an error on the TextOut(); line that says "warning C4267:'argument': conversion from 'size_t' to 'int', possible loss of data" Some things I notice that are probable, my code is wrong somehow..or i'm just putting it in the wrong place. The reason I put this in the ID_ABOUT_HELLOWORLD case, was because I figure if I put it in the normal WM_PAINT case i'de just be painting the message on window creation/update, I wouldn't be painting it when the menu item is clicked. The thing I would really need..is for it to paint szMessage[] and keep it there while processing all other messages..not just putting the paint data in a while loop or something and let it sit there..right now it would seem(if my code is right) that it would paint it once when I click the menu item and then it would go about its business, it wouldn't make it stay there. I want it to stay until the window is destroyed.. But in any case, ID_ABOUT_HELLOWORLD is a menu item within IDM_ABOUT, however you say that in winapi terminology..I obviously have no clue ^_^, but it's a menu item that when you hover over IDM_ABOUT it has a black arrow that points to the right, then a new menu appears, that's where ID_ABOUT_HELLOWORLD lies. What is wrong with my code and conceptualization? Am I just coding it wrong or am I putting this in the wrong place? Or is it both? Thank you for your time, ~Mezmirous [Edited by - Mezmirous on December 5, 2004 5:38:02 PM]
Advertisement
First thing, I think this forum is for .NET development, as in using the .NET framework, not just Visual Studio .NET. But anyway...

Quote:Original post by Mezmirous
it's giving me an error on the TextOut(); line that says "warning C4267:'argument': conversion from 'size_t' to 'int', possible loss of data"

That's not a problem, to get rid of that warning just put an explicit cast to int in front of strlen, e.g.
TextOut(ps.hdc, 0, 0, szMessage, (int)strlen(szMessage));

The reason it says that is that size_t (what strlen returns) is 32 bits unsigned, and int is 31 bits of number plus a sign bit, so theoretically for very large numbers there could be issues; however, I doubt you have a 2 billion character long string, so I wouldn't worry about it.

Quote:Original post by Mezmirous
Some things I notice that are probable, my code is wrong somehow..or i'm just putting it in the wrong place. The reason I put this in the ID_ABOUT_HELLOWORLD case, was because I figure if I put it in the normal WM_PAINT case i'de just be painting the message on window creation/update, I wouldn't be painting it when the menu item is clicked.

The thing I would really need..is for it to paint szMessage[] and keep it there while processing all other messages..not just putting the paint data in a while loop or something and let it sit there..right now it would seem(if my code is right) that it would paint it once when I click the menu item and then it would go about its business, it wouldn't make it stay there. I want it to stay until the window is destroyed..

But in any case, ID_ABOUT_HELLOWORLD is a menu item within IDM_ABOUT, however you say that in winapi terminology..I obviously have no clue ^_^, but it's a menu item that when you hover over IDM_ABOUT it has a black arrow that points to the right, then a new menu appears, that's where ID_ABOUT_HELLOWORLD lies.

What is wrong with my code and conceptualization? Am I just coding it wrong or am I putting this in the wrong place? Or is it both?

Thank you for your time,
~Mezmirous

The simplest way to accomplish this would be to store a flag representing whether or not that menu item has been clicked, and in WM_PAINT you only draw the text if the flag is set to true. In your handler for ID_ABOUT_HELLOWORLD, you'd just set the flag and then call
InvalidateRect(hWnd, NULL, TRUE);
to force your window to redraw (and thus invoke your WM_PAINT handler.) That way the message will "stay" on the screen, but also only show up when you click the menu item. If you need any further clarification just ask.
Thank you very much :) Your explanation was excellent.

Is there a better place I should put WinAPI questions?

Thank you for your time,
~Mezmirous
I'm glad I could help.

Maybe try the General Programming forum?

This topic is closed to new replies.

Advertisement