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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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
HTH
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ...
ASKER
Open in new window