Link to home
Start Free TrialLog in
Avatar of tango2009
tango2009

asked on

Message Handler win32

I am creating a program where when the user clicks on the window text is printed on the window at that position. I have posted my message handler I am just stuck on how you can find the mouse position and print text to that position.
case WM_LBUTTONDOWN :
		MessageBox(hwnd, "L Mouse Down", "Mouse", MB_OK);
		break;

	case WM_RBUTTONDBLCLK :
		MessageBox(hwnd, "Double Click", "Mouse", MB_OK);
		break;

	case WM_MOUSEMOVE :
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);

			if (wParam & MK_RBUTTON)
			{
				char text[50];
				sprintf(text, "%d %d", x, y);

				MessageBox(hwnd, text, "Mouse", MB_OK);
			}
			
			break;
		}
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tango2009
tango2009

ASKER

I have added hdc = GetDC(hwnd); and the release of the DC at the end so far my code now looks like this. At the minute though the program is printing out the text to a set position how do I change it so that it checks where the mouse is and prints the text to that position.
case WM_MOUSEMOVE :
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);

			if (wParam & MK_RBUTTON)
			{
				char text[50];
				sprintf(text, "%d %d", x, y);

				MessageBox(hwnd, text, "Mouse", MB_OK);
                TextOut(hdc, 100, 200, "Mouse click ", 100);
			}
			
			break;
		}
	
 ReleaseDC(hwnd, hdc);

Open in new window

You simply have to pass 'x' and 'y' to 'TextOut' instead of the hardcoded '100, 200'

Further the last paramter passed to 'TextOut' has to be the number of characters in the string instead of the '100' you pass. So the code should like like this:

> ...
> const char* pszText = "Mouse click ";
> TextOut(hdc, x, y, "Mouse click ", strlen( pszText ) );
> ...

ZOPPO

Ok thankyou just one more question how can you pass the text out command into WM_PAINT I have just started to use win32.
I think ZOPPO has done everything except give complete code, so I will fill in that blank here.  What follows is a rudimentary solution: you remember the location the mouse was clicked, then force a redraw on the window.  Nice and simple.

HTH
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I was out for lunch ... thanks mrwad99 ...
Heh, no problem.  It is a pretty dirty solution though, a far better one would have been to just draw the text again at the original location, except in the same colour as the background, thereby erasing it... but I guess we can leave that as an exercise to the questioner :)
Yes - IMO always a good idea (and a good excercise :o) is to draw into a memory DC, BitBlt it in WM_PAINT handler and override WM_ERASEBKGND handler to eliminate any flickering ...