偵測滑鼠滾輪動作

Posted: 五月 8, 2008 in 技術相關
標籤:,

在 Windows 視窗程式設計中, 有時除了偵測鍵盤動作外

還會另外需要去偵測滑鼠動作 … 簡易的 滑鼠單/雙擊左右鍵 或是捲動滾輪

其實都有相對應的 Message 可以去呼叫 …

下面介紹的是 判斷滑鼠滾輪向上/向下之判斷 …

下面舉例來說 …


BOOL CDrawView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    // TODO: Add your message handler code here and/or call default
    if(zDelta > 0){
        // wheel up...
    }   
    else{
        // wheel down...
    }

    return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}

其中所帶入之參數

  • nFlags – 表示在滾動捲軸時, 還相對應的按下了哪些鍵, 在 MSDN 中定義了 ctrl, shift, 滑鼠左右中建等組合
  • zDelta – 則傳達了滾輪所帶出的偏移量, 預設值是 120 …正負號則可以用以判別 捲軸是向上或是向下捲動
  • pt – 至於 pt 則是顯示滑鼠所在位置之(x, y)座標

因此如上例 則可利用 zDelta 來判斷卷軸之捲動方向 … 借以作 類似 ZoomIn/ZoomOut 的功能 ….

完整的 MSDN 定義節錄如下

afx_msg BOOL OnMouseWheel( UINT nFlags, short zDelta, CPoint pt );

Return Value

Nonzero if mouse wheel scrolling is enabled; otherwise 0.

Parameters

nFlags

Indicates whether various virtual keys are down. This parameter can be any combination of the following values:

  • MK_CONTROL Set if the CTRL key is down.
  • MK_LBUTTON Set if the left mouse button is down.
  • MK_MBUTTON Set if the middle mouse button is down.
  • MK_RBUTTON Set if the right mouse button is down.
  • MK_SHIFT Set if the SHIFT key is down.

zDelta

Indicates distance rotated. The zDelta value is expressed in multiples or divisions of WHEEL_DELTA, which is 120. A value less than zero indicates rotating back (toward the user) while a value greater than zero indicates rotating forward (away from the user). The user can reverse this response by changing the Wheel setting in the mouse software. See the Remarks for more information about this parameter.

pt

Specifies the x- and y-coordinate of the cursor. These coordinates are always relative to the upper-left corner of the window.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s