Solved

Message Handler win32

Posted on 2010-11-08
9
368 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

832 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