- 公开视频 -> 链接点击跳转公开课程
- 博客首页 -> 链接点击跳转博客主页
目录
滑动条(Trackbar)
使用场景
-
音量控制
-
亮度调节
-
视频播放进度控制
-
任何需要用户在特定范围内选择值的场景
初始控件
- TBM_SETRANGE - 设置范围
- TBM_SETPOS - 设置位置
-
case WM_INITDIALOG: { HWND hTrackBar = GetDlgItem(hDlg, IDC_MYTRACKBAR); SendMessage(hTrackBar, TBM_SETRANGE, TRUE, MAKELPARAM(minRange, maxRange)); // 设置范围 SendMessage(hTrackBar, TBM_SETPOS, TRUE, (LPARAM)initialPosition); // 设置初始位置 return (INT_PTR)TRUE; }
控件消息
TBM_SETPOS
:设置滑块的当前位置。TBM_GETPOS
:获取滑块的当前位置。-
// 设置滑块的位置 SendMessage(hTrackBar, TBM_SETPOS, TRUE, (LPARAM)newPosition); // 获取滑块的位置 int pos = (int)SendMessage(hTrackBar, TBM_GETPOS, 0, 0);
示例代码
case WM_INITDIALOG:
{
// 初始环境
INITCOMMONCONTROLSEX initComm = { 0 };
initComm.dwSize = sizeof(INITCOMMONCONTROLSEX);
initComm.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&initComm);
// 初始控件
HWND hTrackBar = GetDlgItem(hwnd, IDC_SLIDER1);
SendMessage(hTrackBar, TBM_SETRANGE, TRUE, MAKELPARAM(0, 100));
SendMessage(hTrackBar, TBM_SETPOS, TRUE, 20);
return TRUE;
}
case WM_HSCROLL:
{
HWND hTrackBar = GetDlgItem(hwnd, IDC_SLIDER1);
switch (LOWORD(wParam))
{
case TB_THUMBTRACK:
{
if ((HWND)lParam == hTrackBar)
{
TCHAR szBuffer[0xFF] = { 0 };
INT nPos = SendMessage(hTrackBar, TBM_GETPOS, 0, 0);
wsprintf(szBuffer, TEXT("%d"), nPos);
SetWindowText(GetDlgItem(hwnd, IDC_STATIC_TRACK), szBuffer);
}
break;
}
}
return TRUE;
}