鼠标滚轮问题
相关的例子:下载>>> 作者:山原依纲 于2008-7-19上传 

     前几天在CSDN上看到一个VB高手准备收徒弟,面试的题目是“编写通过鼠标滚轮来控制窗口大小的程序”。居然很多人认为这个题目很困难。趁着这个机会,我也研究了一下鼠标滚轮的问题。

 

当鼠标滚轮转动时,WM_MOUSEWHEEL消息将会送至当前聚焦的窗口。

 

WM_MOUSEWHEEL Notification

The WM_MOUSEWHEEL message is sent to the focus window when the mouse wheel is rotated. The DefWindowProc function propagates the message to the window's parent. There should be no internal forwarding of the message, since DefWindowProc propagates it up the parent chain until it finds a window that processes it.

A window receives this message through its WindowProc function.

Syntax

WM_MOUSEWHEEL

    WPARAM wParam

    LPARAM lParam;

   

Parameters

wParam

The high-order word indicates the distance the wheel is rotated, expressed in multiples or divisions of WHEEL_DELTA, which is 120. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user.

word表明滚轮转动的距离,以WHEEL_DELTA120)为单位。正数表明向前转动,方向是远离使用者。负数表示向回转动,转向使用者。

The low-order word indicates whether various virtual keys are down. This parameter can be one or more of the following values.

word表明此时是否有虚拟键按下。这个参数是如下一个或者多个值的组合,

MK_CONTROL

The CTRL key is down.

MK_LBUTTON

The left mouse button is down.

MK_MBUTTON

The middle mouse button is down.

MK_RBUTTON

The right mouse button is down.

MK_SHIFT

The SHIFT key is down.

MK_XBUTTON1

Windows 2000/XP: The first X button is down.

MK_XBUTTON2

Windows 2000/XP: The second X button is down.

lParam

The low-order word specifies the x-coordinate of the pointer, relative to the upper-left corner of the screen.

word表明以左上角为原点的当前鼠标所在的x坐标。

The high-order word specifies the y-coordinate of the pointer, relative to the upper-left corner of the screen.

word表明以左上角为起点的当前鼠标所在的y坐标。

 

Return Value

If an application processes this message, it should return zero.

如果应用程序处理了这个消息,应该返回0.

看上去应用起来应该非常简单,于是编写了简单的Demo

 

;MASMPlus 代码模板 - 普通的 Windows 程序代码

 

.386

.Model Flat, StdCall

Option Casemap :None

 

Include windows.inc

Include user32.inc

Include kernel32.inc

Include gdi32.inc

 

includelib gdi32.lib

IncludeLib user32.lib

IncludeLib kernel32.lib

include macro.asm

  

   WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD

   WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

  

.DATA

   szClassName db "MASMPlus_Class",0

  

.DATA?

   hInstance  dd ?

  

.CODE

START:

 

   invoke GetModuleHandle,NULL

   mov hInstance,eax

   invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT

   invoke ExitProcess,0

 

WinMain proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD

   LOCAL wc    :WNDCLASSEX

   LOCAL msg   :MSG

   local hWnd :HWND

  

   mov wc.cbSize,sizeof WNDCLASSEX

   mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW

   mov wc.lpfnWndProc,offset WndProc

   mov wc.cbClsExtra,NULL

   mov wc.cbWndExtra,NULL

   push hInst

   pop wc.hInstance

   mov wc.hbrBackground,COLOR_BTNFACE+1

   mov wc.lpszMenuName,NULL

   mov wc.lpszClassName,offset szClassName

   invoke LoadIcon,hInst,100

   mov wc.hIcon,eax

   invoke LoadCursor,NULL,IDC_ARROW

   mov wc.hCursor,eax

   mov wc.hIconSm,0

   invoke RegisterClassEx, ADDR wc

   invoke CreateWindowEx,NULL,ADDR szClassName,CTXT("http://www.aogosoft.com"),WS_OVERLAPPEDWINDOW,200,200,400,200,NULL,NULL,hInst,NULL

   mov hWnd,eax

   invoke ShowWindow,hWnd,SW_SHOWNORMAL

   invoke UpdateWindow,hWnd

  

   StartLoop:

       invoke GetMessage,ADDR msg,NULL,0,0

          cmp eax, 0

          je ExitLoop

              invoke TranslateMessage, ADDR msg

              invoke DispatchMessage, ADDR msg

          jmp StartLoop

   ExitLoop:

  

mov eax,msg.wParam

ret

WinMain endp

WndProc proc hWin:DWORD,uMsg:DWORD,wParam :DWORD,lParam :DWORD

   .if uMsg==WM_CREATE

   .elseif uMsg == WM_MOUSEWHEEL  

       mov eax,wParam      ;真正有用的代码只有红色一点点

       shr eax,16

       test   eax,8000h

       jz  wheeldown

       invoke SetWindowText,hWin,CTXT("向下滚!")

       jmp @f

     wheeldown: 

       invoke SetWindowText,hWin,CTXT("向上滚!")

     @@: 

   .elseif uMsg == WM_DESTROY

       invoke PostQuitMessage,NULL

   .else

       invoke DefWindowProc,hWin,uMsg,wParam,lParam

   .endif

   ret

WndProc endp

END START

希望本文能起到抛砖引玉的作用,记得鼠标除了左右键,还有这样一个非常方便的功能。

 



<<<上一篇
欢迎访问AoGo汇编小站:http://www.aogosoft.com
下一篇>>>