对话框(非模式)调色板③
本帖最后由 qq1242009750 于 2018-9-10 20:18 编辑本贴来实现以下功能:
4.应为原色的范围是 0 - 255,所以滚动条的范围要和原色一致(0 - 255) 可以用SetScrollInfo函数来设置
5.滚动条背景可以显示当前原色的颜色,当原色色号为0时显示黑色,色号用全局变量保存
6.改变滚动条的背景颜色可以用WM_CTLCOLORSCROLLBAR消息来改变,此消息要返回一个画刷句柄,此句柄就是滚动条背景色的句柄
7.当滚动条被拖动或两边按键被点击时,要能正确相应消息,滚动条被拖动或两边按键会发送WM_VSCROLL下的SB_THUMTRACK(拖动)、SB_LINEUP、SB_LINEDOWN消息
8.滚动条滚动完毕后也要改变对应的色号,并在滚动条的下方显示出来
好我们来接着写:
接下来我们主要的是对话框的操作,所以只要对对话框过程函数进行修改就好了.
在这里做个ID号的说明:
IDC_SCROLLBAR4 对应的是红色滚动条
IDC_SCROLLBAR5 对应的是绿色滚动条
IDC_SCROLLBAR6 对应的是蓝色滚动条
IDC_STATIC1 对应的是红色色号
IDC_STATIC2 对应的是绿色色号
IDC_STATIC3 对应的是蓝色色号
下面给出代码,其实代码主要动的地方就无非是多了3个全局变量和修改了对话框处理过程
#include <tchar.h>
#include <windows.h>
#include "resource.h"
#define CLASSNAME TEXT("Name")
void CfiWndClass(HINSTANCE hInstance);
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR _stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
HWND DlghWnd;
int R, G, B; //全局色号
void CfiWndClass(HINSTANCE hInstance)
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.hCursor = LoadCursor(hInstance, IDC_ARROW);
wcx.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcx.hIconSm = NULL;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WndProc;
wcx.lpszClassName = CLASSNAME;
wcx.lpszMenuName = NULL;
wcx.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wcx);
}
int _stdcall _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
CfiWndClass(hInstance);
HWND hWnd = CreateWindow(CLASSNAME, TEXT("GsssD"), WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hInstance, 0);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (DlghWnd == 0 || !IsDialogMessage(DlghWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
DlghWnd = CreateDialog((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc);
ShowWindow(DlghWnd, SW_SHOW);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR _stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL Ret = FALSE;
int i = 0;
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
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_VSCROLL:
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; //保存红色色号
break;
case IDC_SCROLLBAR5: //保存绿色色号
G = si.nPos;
break;
case IDC_SCROLLBAR6: //保存蓝色色号
B = si.nPos;
break;
}
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;
}
完成以上代码后我们的滚动条可以正常运行了HH,那我们接着实现 色号和滚动条背景颜色的功能吧
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;
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);
//SetScrollInfo(hDlg, SB_VERT, &si, TRUE);
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_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(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;
}
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]