ICZelion TUT7

zhaozj2021-02-11  216

Tutorial 7: Mouse INPUT

We will learn how to receive and respond to mouse input in our window procedure. The example program will wait for left mouse clicks and display a text string at the exact clicked spot in the client area.

Theory: As with keyboard input, Windows detects and sends notifications about mouse activities that are relevant to each window Those activities include left and right clicks, mouse cursor movement over window, double clicks Unlike keyboard input which is directed to the window that has.. input focus, mouse messages are sent to any window that the mouse cursor is over, active or not. In addition, there are mouse messages about the non-client area too. But most of the time, we can blissfully ignore them. We can Focus On Those Relating to the Client Area.

There are two messages for each mouse button:. WM_LBUTTONDOWN, WM_RBUTTONDOWN and WM_LBUTTONUP, WM_RBUTTONUP messages For a mouse with three buttons, there are also WM_MBUTTONDOWN and WM_MBUTTONUP When the mouse cursor moves over the client area, Windows sends WM_MOUSEMOVE messages to the window under. The cursor.

A Window Can Receive Double Click Messages, WM_LButtondbClk or WM_RBUTTONDBCLK,

IF AND Only if ITS WINDOW CLASS HAS CS_DBLCLKS Style Flag, Else The Window Will Receive Only A Series of Mouse Button Up and Down Messages.

For all these messages, the value of lParam contains the position of the mouse. The low word is the x-coordinate, and the high word is the y-coordinate relative to upper left corner of the client area of ​​the window. WParam indicates the State of the mouse buttons and shift and ctrl keys.

EXAMPLE:

.386

.Model flat, stdcall

Option CaseMAP: NONE

Winmain Proto: DWORD,: DWORD,: DWORD,: DWORD

include /masm32/include/windows.inc include /masm32/include/user32.inc include /masm32/include/kernel32.inc include /masm32/include/gdi32.inc includelib /masm32/lib/user32.lib includelib / masm32 / lib / kernel32.lib includelib /masm32/lib/gdi32.lib.data classname DB "SimpleWinclass", 0 Appname DB "Our First Window, 0 mouseclick db 0; 0 = no click yet

.DATA? HINSTANCE HINSTANCE? COMMANDLINE LPSTR? HitPoint Point <>

. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Getcommandline Mov Commandline, Eax Invoke Winmain, Hinstance, Null, CommandLine, SW_SHOWDEFAULT INVOKE EXITPROCESS, EAX

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInst pop wc.hInstance mov wc.hbrBackground, COLOR_WINDOW 1 mov wc.lpszMenuName, NULL mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon , eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx, NULL, aDDR ClassName, aDDR AppName, / WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, / CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, / HINST, NULL MOV HWND, EAX INVOKE ShowWindow, Hwnd, SW_SHOWNORMAL INVOKE UPDATEWINDOW, HWND .WHILE TRUE INVOKE GETMESSAGE, Addr MSG, NULL, 0, 0.Break .if (! EAX) Invoke DispatchMessage, Addr Msg .Endw Mov EAX, MSG.WParam Ret Winmain EndpWndProc Proc HWnd: HWnd, UMSG: UINT, WPARAM: WPARAM, LPARAM: LPARAM LOCAL HDC : Hdc local ps: Paintstruct

.IF uMsg == WM_DESTROY invoke PostQuitMessage, NULL .ELSEIF uMsg == WM_LBUTTONDOWN mov eax, lParam and eax, 0FFFFh mov hitpoint.x, eax mov eax, lParam shr eax, 16 mov hitpoint.y, eax mov MouseClick, TRUE invoke InvalidateRect , hWnd, NULL, TRUE .ELSEIF uMsg == WM_PAINT invoke BeginPaint, hWnd, ADDR ps mov hdc, eax .IF MouseClick invoke lstrlen, ADDR AppName invoke TextOut, hdc, hitpoint.x, hitpoint.y, ADDR AppName, eax .ENDIF Invoke Endpaint, Hwnd, Addr Ps .else Invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM RET WNDPRE END, EAX RET WNDPROC Endp End Startanalysis:

.ELSEIF uMSG == WM_LButtondown

Mov Eax, LPARAM

And Eax, 0FFFFH

Mov HitPoint.x, EAX

Mov Eax, LPARAM

SHR EAX, 16

Mov HitPoint.y, EAX

MOV Mouseclick, True

Invoke InvalidateRect, HWND, NULL, TRUE

.. The window procedure waits for left mouse button click When it receives WM_LBUTTONDOWN, lParam contains the coordinate of the mouse cursor in the client area It saves the coordinate in a variable of type POINT which is defined as:

Point Struct X DD? Y DD? Point Ends

And Sets The Flag, MouseClick, To True, Meaning That There's at Least A Left Mouse Button Click In The Client Area.

Mov Eax, LParam and Eax, 0FFFFH MOV Hitpoint.x, EAX

Since X-Coordinate Is The Low Word of Lparam and The Member in Size, We Have To Zero Out The High Word of Eax Prior To Storing It in Hitpoint.x.

SHR EAX, 16 MOV HitPoint.y, EAX

Because y-coordinate is The High Word of LParam, We Must Put in The Low Word of Eax Prior To Storing It in Hitpoint.y. We do this by shifting eax 16 bits to the right. After storing the mouse position, we set the flag, MouseClick, to TRUE in order to let the painting code in WM_PAINT section know that there's at least a click in the client area so it can draw the string at the mouse position. Next we call InvalidateRect function to force the window to repaint ITS Entire Client Area.. IF MouseClick Invoke Lstrlen, Addr Appname Invoke Textout, HDC, Hitpoint.x, Hitpoint.y, Addr Appname, Eax .Endif

The painting code in WM_PAINT section must check if MouseClick is true, since when the window was created, it received a WM_PAINT message which at that time, no mouse click had occurred so it should not draw the string in the client area. We initialize MouseClick to FALSE and change its value to TRUE when an actual mouse click occurs. If at least one mouse click has occurred, it draws the string in the client area at the mouse position. Note that it calls lstrlen to get the length of the string to Display and sends the length of textout function.

转载请注明原文地址:https://www.9cbs.com/read-4665.html

New Post(0)