Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Message Handler win32

Posted on 2010-11-08
9
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

609 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