Solved

Message Handler win32

Posted on 2010-11-08
9
366 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 30

Accepted Solution

by:
Zoppo earned 250 total points
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
 

Author Comment

by:tango2009
Comment Utility

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
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
Comment Utility
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 30

Expert Comment

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

Expert Comment

by:mrwad99
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now