Win32 Slider Control的用法
2.1 获得控件的窗口句柄。
slider control也是一个窗口,是对话框的子窗口,因此,要用GetDlgItem来实现:
backlight_wnd = GetDlgItem(hDlg, IDC_SLIDER_BACKLIGHT); //注意下面示例中蓝色标出的区别
2.2 设置slider control的区间,是给slider control发TBM_SETRANGE消息,比如我设置0到100就是:
SendMessage(backlight_wnd,TBM_SETRANGE, (WPARAM)1,(LPARAM)MAKELONG(0,100));
2.3 设置滑块的初始位置是发送TBM_SETPOS消息来实现的:
SendMessage(backlight_wnd,TBM_SETPOS, (WPARAM)1,(LPARAM)BacklightPercentage);
2.4 slider control和用户的交互,则是通过WM_HSCROLL消息来实现的,需要在WM_HSCROLL的响应函数里面发送TBM_GETPOS消息得到当前滑块的位置:
BacklightPercentage = (unsigned char)SendMessage(backlight_wnd,TBM_GETPOS,0,0);
2.5 旁边的一个当前刻度的数字的说明,需要先设定一个Static text文本控件,然后动态给该控件发送WM_SETTEXT消息来实现动态的更新数字:
SendMessage(GetDlgItem(hDlg, IDC_STATIC_BACKLIGHT_VALUE), WM_SETTEXT, 0, LPARAM(szBacklightPercentage));
最后面的那个参数,szBacklightPercentage是一个字符串。
INT_PTR WINAPI DlgProcTouMingSet(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdcAbout;
int EscKeyID=1000;
int ReturnID=1001;
HWND backlight_wnd;
switch (uMsg)
{
case WM_INITDIALOG:
{
RegisterHotKey(hwnd,EscKeyID,0,VK_ESCAPE);
RegisterHotKey(hwnd,ReturnID,0,VK_RETURN);
backlight_wnd = GetDlgItem(hwnd,IDC_SLIDER_TouMing);
//2.2 设置slider control的区间,是给slider control发TBM_SETRANGE消息,比如我设置0到100就是:
SendMessage(backlight_wnd,TBM_SETRANGE, (WPARAM)1,(LPARAM)MAKELONG(15,255));
int TouMingChuShi= GetPrivateProfileInt(_T("SkinChoose"),_T("TouMing"),150,defaultFullFullPath);
SendMessage(backlight_wnd,TBM_SETPOS, (WPARAM)1,TouMingChuShi); //读取初始透明度
}
break;
case WM_HSCROLL:
{
int nScrollCode = (int) LOWORD(wParam); // scroll bar value
//int nPos = (int) HIWORD(wParam); // scroll box position
HWND hwndScrollBar = (HWND) lParam; // handle to scroll bar lParam一般表示窗口句柄
int nPos=200;
nPos=SendMessage(hwndScrollBar,TBM_GETPOS,0,0);
TCHAR TouMing[10];
switch(nScrollCode)
{
case SB_THUMBTRACK: //用户拖动滚动框。此消息被重复发送,直到用户释放鼠标按钮。
{
nPos=SendMessage(hwndScrollBar,TBM_GETPOS,0,0);
_itow_s( nPos, TouMing, 10, 10);
WritePrivateProfileString(_T("SkinChoose"),_T("TouMing"),TouMing,defaultFullFullPath);
SetLayeredWindowAttributes(
hwnd1,
RGB(0,0,0), //表示与此颜色相同的颜色将会透明
nPos, //表示透明度0是全透明,255是全显示
LWA_ALPHA|LWA_COLORKEY //LWA_ALPHA|LWA_COLORKEY:这样crKey的地方将变为全透明,而其它地方根据bAlpha参数确定透明度。
);
}
break;
case SB_ENDSCROLL:
{
nPos=SendMessage(hwndScrollBar,TBM_GETPOS,0,0);
WritePrivateProfileString(_T("SkinChoose"),_T("Skin"),_T("1"),defaultFullFullPath);
SetLayeredWindowAttributes(
hwnd1,
RGB(0,0,0), //表示与此颜色相同的颜色将会透明
nPos, //表示透明度0是全透明,255是全显示
LWA_ALPHA|LWA_COLORKEY //LWA_ALPHA|LWA_COLORKEY:这样crKey的地方将变为全透明,而其它地方根据bAlpha参数确定透明度。
);
break;
}
}
}
break; case WM_HOTKEY:
{
switch (wParam)
{
case 1000:
{
EndDialog(hwnd, LOWORD(wParam));
UnregisterHotKey(hwnd,1000);
}
break;
case 1001:
{
EndDialog(hwnd, LOWORD(wParam));
UnregisterHotKey(hwnd,1001);
}
break;
}
}
break;
case WM_LBUTTONDOWN:
{
EndDialog(hwnd, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
case WM_COMMAND:
{
UINT wmId = LOWORD(wParam);
UINT wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDOK: //获取生日时间并赋值给全局变量
case IDCANCEL:
{
EndDialog(hwnd, LOWORD(wParam));
}
return (INT_PTR)TRUE;
}
}
}
return (INT_PTR)FALSE;
}