Theory
Like handling keyboard input, WINDOWS captures mouse movements and sends them to relevant WINDOWS. These activities include left, right-click, move, double-click, and scroll wheel message WM_WHEEL. WINDOWS does not direct all mouse messages to a window with an input focus as it does with keyboard input. Any mouse passing through the window will receive the mouse message, regardless of whether there is any input focus. In addition, the window will receive a message (WM_NCMOVE) that moves the mouse in a non-customer area, but in most cases we will ignore it. Each mouse button has two messages: WM_LBUTTONDOWN and WM_RBUTTONDOWN. There will also be WM_MBUTTONDOWN and WM_MBUTTONUP messages for the three-Key mouse. When the mouse moves in the client area of a window, the window will receive the WM_MOUSEMOVE message. If you want to process WM_LBUTTONDBCLK or WM_RBUTTONDBCLK in a window, its window class must have the CS_DBLCLKS style. Otherwise, it will receive a bunch of ups and downs (WM_XBUTTONDOWN or WM_XBUTTONUP) messages. For all messages, the lParam parameter passed in by the window procedure function contains the cursor position, where the base position is the x coordinate and the high position is the y coordinate, these coordinate values are relative to the values in the upper left corner of the window client area, and wParam contains the state of the mouse button.
Instance
Key code
// The example program demonstrates how to wait for the left-click message to display a string at the position where it is pressed. Tchar fontname [] = _ T (""); tchar szcaptionmain [] = _ T ("My first window! "); Wparam keychar = 0x20; // 0x20 (hexadecimal) is the ASCII code of the space. bool mouseclick = false is displayed normally when no buttons are set in danzhou; point hitpoint; lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; hfont, holdfont; Switch (Message) {Case wm_command: wmid = loword (wparam); wmevent = hiword (wparam); // select switch (wmid) {Case idm_about: dialogbox (hinst, makeintresource (idd_abou Tbox), hwnd, about); break; Case idm_exit: destroywindow (hwnd); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} break; Case wm_paint: HDC = beginpaint (hwnd, & PS); // todo: add any drawing code here... hfont = createfont (24, 16, 0, average, minimum, minimum, default_quality, default_pitch | ff_script, fontname); holdfont = (hfont) SelectObject (HDC, hfont ); settextcolor (HDC, RGB (200, 255); setbkcolor (HDC, RGB (,); textout (HDC, (tchar *) & keychar, 1); If (mouseclick) {textout (HDC, hitpoint. x, hitpoint. y, szcaptionmain, lstrlen (szcaptionmain);} SelectObject (HDC, holdfont); endpaint (hwnd, & PS); break; // case wm_mousemove: Case wm_lbuttondown: // obtain the coordinates of a string (wm_paint message) hitpoint when you press it with the left button. X = loword (lparam); hitpoint. y = hiword (lparam); mouseclick =! Mouseclick; // display invalidaterect (hwnd, null, true) twice; break; Case wm_char: keychar = wparam; invalidaterect (hwnd, null, true); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}
: