MFC小笔记:滑动条与编辑框

需求:
有一个滑动条(IDC_SLIDER1)和一个编辑框(IDC_EDIT1),当滑动条变化时,数值在编辑框中同时显示。

添加NM_CUSTOMDRAW响应事件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
void CCameraDlg::OnCustomdrawSliderShutter(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: Add your control notification handler code here
    *pResult = 0;

    CSliderCtrl *pSlidCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLIDER1);
    CString strTemp;
    strTemp.Format("%d", pSlidCtrl->GetPos());
    SetDlgItemText(IDC_EDIT1, strTemp.GetBuffer());
}

添加NM_RELEASEDCAPTURE响应事件,代码如下:

1
2
3
4
5
6
7
8
9
void CCameraDlg::OnReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
    // TODO: Add your control notification handler code here
    *pResult = 0;

CString strTemp;
GetDlgItemText(IDC_EDIT1, strTemp);
AfxMessageBox(strTemp);
}

响应编辑框的回车事件:

在父窗体中PreTranslateMessage函数中处理,示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if ( pMsg->message == WM_KEYDOWN )
{
switch (pMsg->wParam)
{
case VK_RETURN: //回车
            {
// 方法1
#if 0
                CWnd* pWnd;
                pWnd = GetDlgItem(IDC_EDIT1);
                if (pWnd->GetSafeHwnd() == pMsg->hwnd)
                {
// todo
                }
#endif
// 方法2
                UINT nID = this->GetFocus()->GetDlgCtrlID();
if (nID == IDC_EDIT1)
{
// todo
}
if (nID == IDC_EDIT2)
{
// todo
}
            }
return TRUE;


case VK_ESCAPE: //ESC


return TRUE;
}
}


return __super::PreTranslateMessage(pMsg);
}