MFC通過鼠標滾輪上下滾動修改文本框中的變量值大小

功能大體描述:大體就是一個Spin控件,一個Edit控件。點擊Spin上下可以修改Edit中的值,點擊Edit控件(獲取到輸入光標後),滾動滾輪修改Edit中的值


1.這裏使用OnMouseWheel消息(WM_MOUSEWHEEL)響應函數

官方函數:afx_msg BOOL OnMouseWheel( UINT nFlags, short zDelta, CPoint pt );

  • 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 screen.

 

2.代碼實現:

void CMouseDlg::OnDeltaposSpinNum(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
    // TODO:  在此添加控件通知處理程序代碼
    if (pNMUpDown->iDelta==1)
    {
        m_dNum -= 0.1;
    }
    else if (pNMUpDown->iDelta == -1)
    {
        m_dNum += 0.1;
    }


    ShowNumInfo();
    *pResult = 0;
}

 

if (GetDlgItem(IDC_EDIT_NUM)->GetSafeHwnd() == ::GetFocus())
    {
        if (nFlags == 0)
        {
            if (zDelta >= 0)
            {
                m_dNum += 0.01;
            }
            else if (zDelta <= 0)
            {
                m_dNum -= 0.01;
            }
        }
        else if(nFlags == MK_SHIFT)    //shift鍵按下
        {
            if (zDelta >= 0)
            {
                m_dNum += 0.1;
            }
            else if (zDelta <= 0)
            {
                m_dNum -= 0.1;
            }
        }
        else if (nFlags == MK_CONTROL)   //Control鍵按下
        {
            if (zDelta >= 0)
            {
                m_dNum += 1;
            }
            else if (zDelta <= 0)
            {
                m_dNum -= 1;
            }
        }
        
        ShowNumInfo();
    }

void CMouseDlg::ShowNumInfo()
{
    CString str;
    str.Format("%.2f",m_dNum);
    SetDlgItemText(IDC_EDIT_NUM, str);

}
效果:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章