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; } }
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);
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:
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.
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 ...
Open in new window