Hi all. I'm new. This is fun. I like the tutorials a lot!
Anyway, I am trying to modify Tutorial 7 to respond to right mouse click.
However, the right mouse click only works one time, when you first load the window. After that, only left mouse click text string shows even when
you click right mouse.
Thanks for your help!
Anyway, I am trying to modify Tutorial 7 to respond to right mouse click.
However, the right mouse click only works one time, when you first load the window. After that, only left mouse click text string shows even when
you click right mouse.
Thanks for your help!
.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
RightAppName db "My First RightCLick",0
MouseClick db 0 ; 0=no click yet
RightMouseClick db 0
.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 endp
WndProc 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_RBUTTONDOWN
mov eax, lParam
and eax, 0FFFFh
mov hitpoint.x,eax
mov eax, lParam
shr eax, 16
mov hitpoint.y, eax
mov RightMouseClick,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
.ELSEIF RightMouseClick
invoke lstrlen,ADDR RightAppName
invoke TextOut,hdc,hitpoint.x,hitpoint.y,ADDR RightAppName, eax
.ENDIF
invoke EndPaint,hWnd, ADDR ps
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF
xor eax,eax
ret
WndProc endp
end start
However, the right mouse click only works one time, when you first load the window. After that, only left mouse click text string shows even when you click right mouse.
When you check which message to display with the following code .IF MouseClick
invoke lstrlen,ADDR AppName
invoke TextOut,hdc,hitpoint.x,hitpoint.y,ADDR AppName,eax
.ELSEIF RightMouseClick
invoke lstrlen,ADDR RightAppName
invoke TextOut,hdc,hitpoint.x,hitpoint.y,ADDR RightAppName, eax
.ENDIF
you first look at the value of "MouseClick" which is initialized to FALSE in your data segment. Once you change it to TRUE when you click the left mouse button, it always remains to that value regardless of the mouse button you click, and will therefore always result in the code to display the left mouse button message being processed preferentially.
There are numerous ways to correct this misbehaviour but the simplest one for your snippet would be to add the following instruction in the uMsg==WM_RBUTTONDOWN case:
mov MouseClick,FALSE
Raymond
Thanks!
Your solution works, however, I wonder why I do not have to do the same for WM_LBUTTONDOWN since both MouseClick and RightMouseClick are initialized false in the data segment. ?
Your solution works, however, I wonder why I do not have to do the same for WM_LBUTTONDOWN since both MouseClick and RightMouseClick are initialized false in the data segment. ?
I wonder why I do not have to do the same for WM_LBUTTONDOWN
Simply because, if you click the left mouse button, it gets checked first and its code gets done skipping the code for the right mouse button. It then doesn't matter what the RightMouseClick status is in memory.
As I mentioned, this was a quick fix for your snippet because you simply had only 2 choices. Don't use this as a standard way of programming. In more complex algos, you may need to proceed differently.
Raymond
If its possible to have more than one if statement in the message que, you could seperate left and right mouse clicks.
i.e.
.if leftmouseclick==true
invoke something
.endif
.if rightmouseclick==true
invoke something
.endif
I'm not sure if this would work you can test it though if you feel bothered.
maybe even
.if leftmouseclick==TRUE
invoke something
mov leftmouseclick,FALSE
.endif
.if rightmouseclick==TRUE
invoke something
mov rightmouseclick,FALSE
.endif
and if you wanted to be extreme you could have two procedures. Not sure how you'd do that just yet though.
i.e.
.if leftmouseclick==true
invoke something
.endif
.if rightmouseclick==true
invoke something
.endif
I'm not sure if this would work you can test it though if you feel bothered.
maybe even
.if leftmouseclick==TRUE
invoke something
mov leftmouseclick,FALSE
.endif
.if rightmouseclick==TRUE
invoke something
mov rightmouseclick,FALSE
.endif
and if you wanted to be extreme you could have two procedures. Not sure how you'd do that just yet though.