我正在Visual Studio资源编辑器中设计一个对话框,问题是我不知道如何侦听滑块控件发送的消息,图像下方是一个对话框过程。
我知道我需要听WM_NOTIFY
消息,但是我不知道控件向我发送了什么代码...我在以下过程的WM_NOTIFY
部分中添加了2条注释,以显示是问题...
这是对话框过程:
INT_PTR CALLBACK SettingsSoundProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static MainWindow* main_win = nullptr;
switch (uMsg)
{
case WM_INITDIALOG:
{
main_win = reinterpret_cast<MainWindow*>(lParam);
SetWindowText(GetDlgItem(hDlg, IDC_VOLUME_LABEL), TEXT("Music"));
SendMessage(GetDlgItem(hDlg, IDC_VOLUME_SLIDER), TBM_SETRANGE, (WPARAM)FALSE, MAKELPARAM(0, 100));
SendMessage(GetDlgItem(hDlg, IDC_VOLUME_SLIDER), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)20);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg, 0);
return TRUE;
case IDCANCEL:
EndDialog(hDlg, 0);
return TRUE;
default:
break;
}
case WM_NOTIFY:
{
LPNMHDR nmhdr = (LPNMHDR)lParam;
if (nmhdr->idFrom == IDC_VOLUME_SLIDER)
{
switch (nmhdr->code)
{
// what message do I listen for here?
case TB_THUMBTRACK:
case WM_HSCROLL:
case WM_VSCROLL:
case TRBN_THUMBPOSCHANGING:
case TB_PAGEUP:
case TB_PAGEDOWN:
case TB_THUMBPOSITION:
case TB_TOP:
case TB_BOTTOM:
case TB_ENDTRACK:
// this is never hit
MessageBox(nullptr, TEXT("Message"), TEXT("Info"), MB_OK);
break;
default:
// this is hit!
abort();
}
}
return TRUE;
}
case WM_DESTROY:
EndDialog(hDlg, 0);
return TRUE;
default:
break;
}
return FALSE;
}
您可以看到WM_HSCROLL
或TRBN_THUMBPOSCHANGING
都被击中了,在这里我需要听什么消息?
答案 0 :(得分:2)
WM_HSCROLL
和WM_VSCROLL
是它们自己的消息,它们没有包装在WM_NOTIFY
中。
TB_...
值也不是WM_NOTIFY
通知代码。它们是在NMTRBTHUMBPOSCHANGING
通知的TRBN_THUMBPOSCHANGING
结构中报告的原因代码。
每the documentation,跟踪栏发送的唯一WM_NOTIFY
通知是:
尝试一下:
INT_PTR CALLBACK SettingsSoundProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static MainWindow* main_win = nullptr;
static HWND hSlider = nullptr;
switch (uMsg)
{
case WM_INITDIALOG:
{
main_win = reinterpret_cast<MainWindow*>(lParam);
hSlider = GetDlgItem(hDlg, IDC_VOLUME_SLIDER);
SetWindowText(GetDlgItem(hDlg, IDC_VOLUME_LABEL), TEXT("Music"));
SendMessage(hSlider, TBM_SETRANGE, FALSE, MAKELPARAM(0, 100));
SendMessage(hSlider, TBM_SETPOS, TRUE, 20);
return TRUE;
}
case WM_DESTROY:
{
EndDialog(hDlg, 0);
return TRUE;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg, 0);
return TRUE;
case IDCANCEL:
EndDialog(hDlg, 0);
return TRUE;
}
break;
}
case WM_NOTIFY:
{
LPNMHDR nmhdr = reinterpret_cast<LPNMHDR>(lParam);
if ((nmhdr->code == TRBN_THUMBPOSCHANGING) &&
(nmhdr->hwndFrom == hSlider))
{
NMTRBTHUMBPOSCHANGING *nmtrb = reinterpret_cast<NMTRBTHUMBPOSCHANGING*>(lParam);
switch (nmtrb->nReason)
{
case TB_THUMBTRACK:
case TB_PAGEUP:
case TB_PAGEDOWN:
case TB_THUMBPOSITION:
case TB_TOP:
case TB_BOTTOM:
case TB_ENDTRACK:
MessageBox(nullptr, TEXT("Thumb Pos Changing"), TEXT("Info"), MB_OK);
break;
}
}
break;
}
case WM_HSCROLL:
{
if ((lParam != 0) &&
(reinterpret_cast<HWND>(lParam) == hSlider))
{
switch (LOWORD(wParam))
{
case SB_ENDSCROLL:
case SB_LEFT:
case SB_RIGHT:
case SB_LINELEFT:
case SB_LINERIGHT:
case SB_PAGELEFT:
case SB_PAGERIGHT:
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
MessageBox(nullptr, TEXT("Horz Scroll"), TEXT("Info"), MB_OK);
break;
}
}
break;
}
case WM_VSCROLL:
{
if ((lParam != 0) &&
(reinterpret_cast<HWND>(lParam) == hSlider))
{
switch (LOWORD(wParam))
{
case SB_BOTTOM:
case SB_ENDSCROLL:
case SB_LINEDOWN:
case SB_LINEUP:
case SB_PAGEDOWN:
case SB_PAGEUP:
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
case SB_TOP:
MessageBox(nullptr, TEXT("Vert Scroll"), TEXT("Info"), MB_OK);
break;
}
}
break;
}
}
return FALSE;
}