对话框(非模式)调色板④
本帖最后由 qq1242009750 于 2018-9-10 20:23 编辑本贴来实现以下功能:
9.每当我们有色号发生改变都要改变我们右边色板的颜色
10.因为右边的色板是用ListBox做的,所以要改变ListBox的背景色
11.改变LisBox的背景色可以用WM_CTLCOLORLISTBOX消息来改变,此消息要返回一个画刷句柄,此句柄就是ListBox背景色的句柄
其实这三个功能实现起来很简单,在要在对话框处理函数里加一个WM_CTLCOLORLISTBOX消息就可以了。
每当我们改变了滚动条后,都会把ListBox重绘一次,重绘时会经过WM_CTLCOLORLISTBOX消息,我们只要在WM_CTLCOLORLISTBOX消息里创建一个三原色结合的画刷就好了,并在每一次出发滚动条消息时,删除即可.
下面将放出代码,只要和前面的代码对比一下就知道增加了什么功能了
INT_PTR _stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL Ret = FALSE;
int i = 0;
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
static HBRUSH HVScrollBrush, ListBoxBrush;
switch (message)
{
case WM_INITDIALOG:
si.fMask = SIF_POS | SIF_RANGE; //设置标志位
si.nMin = 0;
si.nMax = 255; //设置滚动条的范围 0 - 255
si.nPos = 0;
for (i = IDC_SCROLLBAR4; i <= IDC_SCROLLBAR6; ++i)
SetScrollInfo(GetDlgItem(hDlg, i), SB_CTL, &si, FALSE);
Ret = TRUE;
break;
case WM_CTLCOLORSTATIC:
{
TCHAR buf;
BOOL Cheak = FALSE; //标记位,只修改IDC_STATIC1 - IDC_STATIC3
switch (GetDlgCtrlID((HWND)lParam))
{
case IDC_STATIC1:
wsprintf(buf, TEXT("%03d"), R);
Cheak = TRUE;
break;
case IDC_STATIC2:
wsprintf(buf, TEXT("%03d"), G);
Cheak = TRUE;
break;
case IDC_STATIC3:
wsprintf(buf, TEXT("%03d"), B);
Cheak = TRUE;
break;
}
if(Cheak)
SetWindowText((HWND)lParam, buf);
Ret = TRUE;
}
break;
case WM_CTLCOLORLISTBOX:
ListBoxBrush = CreateSolidBrush(RGB(R, G, B));
return (LRESULT)ListBoxBrush;
case WM_CTLCOLORSCROLLBAR:
switch (GetDlgCtrlID((HWND)lParam)) //根据ID来判断要更改的滚动条
{
case IDC_SCROLLBAR4:
HVScrollBrush = CreateSolidBrush(RGB(R, 0, 0)); //创建画刷
break;
case IDC_SCROLLBAR5:
HVScrollBrush = CreateSolidBrush(RGB(0, G, 0)); //创建画刷
break;
case IDC_SCROLLBAR6:
HVScrollBrush = CreateSolidBrush(RGB(0, 0, B)); //创建画刷
break;
}
return (LRESULT)HVScrollBrush; //返回画刷句柄
case WM_VSCROLL:
DeleteObject(ListBoxBrush);
DeleteObject(HVScrollBrush); //删除画刷
si.fMask = SIF_POS;
GetScrollInfo((HWND)lParam, SB_CTL, &si);
switch (LOWORD(wParam))
{
case SB_LINEUP: //上箭头
if (si.nPos > 0)
--si.nPos;
break;
case SB_LINEDOWN: //下箭头
if (si.nPos < 255)
++si.nPos;
break;
case SB_THUMBTRACK: //滑块的拖动
si.nPos = HIWORD(wParam);
break;
}
//根据ID来判断滚动条
switch (GetDlgCtrlID((HWND)lParam))
{
case IDC_SCROLLBAR4:
R = si.nPos; //保存红色色号,并重新绘制静态文本框
InvalidateRect(GetDlgItem(hDlg, IDC_STATIC1), NULL, FALSE);
break;
case IDC_SCROLLBAR5: //保存绿色色号,并重新绘制静态文本框
G = si.nPos;
InvalidateRect(GetDlgItem(hDlg, IDC_STATIC2), NULL, FALSE);
break;
case IDC_SCROLLBAR6: //保存蓝色色号,并重新绘制静态文本框
B = si.nPos;
InvalidateRect(GetDlgItem(hDlg, IDC_STATIC3), NULL, FALSE);
break;
}
InvalidateRect(GetDlgItem(hDlg, IDC_LIST1), NULL, FALSE);
SetScrollInfo((HWND)lParam, SB_CTL, &si, TRUE);
Ret = TRUE;
break;
case WM_CLOSE:
DestroyWindow(hDlg);
Ret = TRUE;
break;
case WM_DESTROY:
SendMessage(GetParent(hDlg), WM_DESTROY, 0, 0);
Ret = TRUE;
break;
}
return Ret;
}
大家尽量每个帖子的功能都可以试一下,来感受一下软件一步一步完成的感觉
传送门:对话框(非模式)调色板③
传送门:对话框(非模式)调色板⑤
页:
[1]