Solved

Message Handler win32

Posted on 2010-11-08
9
369 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:tango2009
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 250 total points
ID: 34083202
Hi tango2009,

to draw text into a window you need a device-context (DC) associated with that window.

You can retrieve such a DC i.e. using 'GetDC()' passing the handle of the window. Then you can write text using 'TextOut' or 'DrawText'.

You need to release the DC after finishing drawing using 'ReleaseDC'.

BTW you need to decide how your app should work: Just drawing a text into a window means the text will vanish next time the window is redrawn. If you want the text to stay you need to implement the drawing of the text within a WM_PAINT message handler. To do so you will need to store info about the texts generated by mouse clicks so the WM_PAINT message handler knows where to draw the texts.

Hope that helps,

ZOPPO
0
 

Author Comment

by:tango2009
ID: 34083397
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

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34083455
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
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:tango2009
ID: 34083516

Ok thankyou just one more question how can you pass the text out command into WM_PAINT I have just started to use win32.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 34083766
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
0
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 250 total points
ID: 34083773
Hmm: firefox did not add that code snippet...oh well, here it is again:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	static const TCHAR* s_pszMsg = TEXT("Mouse click!");
	static int s_nX = -1, s_nY = -1;
	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		if ( s_nX != -1 && s_nY != -1 )
		{
			RECT r;
			::GetClientRect ( hWnd, &r );
			TextOut ( hdc, s_nX, s_nY, s_pszMsg, _tcslen ( s_pszMsg ) );
		}
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_LBUTTONDOWN:
		{
			s_nX = GET_X_LPARAM(lParam); 
			s_nY = GET_Y_LPARAM(lParam); 
			RECT r;
			::GetClientRect ( hWnd, &r );
			::InvalidateRect(hWnd, &r, TRUE );
		break;
		}
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

Open in new window

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34083805
Sorry, I was out for lunch ... thanks mrwad99 ...
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 34083959
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 :)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34084021
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 ...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question