小白请教各位大神 如何用三个滑块,分别是红绿蓝,实现改变对话框的背景颜色? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、创建成员变量HBRUSH m_brush; 2、滑块变化时响应函数里m_brush.CreateSolidBrush(RGB(r, g, b);2、重载对话框OnCtlColor,在该函数里面返回画刷句柄m_brush 首先设置三个滑块的范围0~255,填充对话框背景你应该会了。在对话框类头文件中声明变量COLORREF color;及BYTE r; BYTE g; BYTE b;BOOL CTestDlg::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default color = RGB(r,g,b); CRect rect; this->GetClientRect(rect); pDC->FillSolidRect(rect, color); return TRUE; return CDialog::OnEraseBkgnd(pDC);} 把三个滑块的值获取过来赋给r,g,b就OK了 三个滑块的值:COLORREF crShow;crShow=RGB(vsPos[0],vsPos[1],vsPos[2]); 整个程序:#define _WIN32_WINNT 0x400#include <windows.h>#include <stdio.h>//LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASS wndclass; HWND hwnd; MSG msg; TCHAR *szName="COL"; wndclass.cbClsExtra=NULL; wndclass.cbWndExtra=NULL; wndclass.hbrBackground=CreateSolidBrush(0); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WndProc; wndclass.lpszClassName=szName; wndclass.lpszMenuName=NULL; wndclass.style=CS_VREDRAW | CS_HREDRAW; if(!RegisterClass(&wndclass)) { MessageBox(NULL,"This Pro Need Windows NT!", "Error",MB_OK); return 0; } hwnd=CreateWindow(szName,szName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;}WNDPROC OldStatic;//LRESULT CALLBACK StaticProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HWND hparentwnd; PAINTSTRUCT ps; HDC hdc; HBRUSH brsh; char Prompt[40];// int id;static TRACKMOUSEEVENT tme;static DWORD counter=0; switch(message) { case WM_PAINT: //如果处理了此消息,WM_CTLCOLORSTATIC也无效了 // OutputDebugString("StaticProc3\n"); // return CallWindowProc(OldStatic, hwnd, message, wParam, lParam ); hparentwnd=GetParent(hwnd);// id=GetDlgCtrlID(hwnd);// sprintf(Prompt,"%d",id);// OutputDebugString(Prompt); //OutputDebugString("Before BeginPaint\n"); hdc = BeginPaint(hwnd, &ps); //OutputDebugString("After BeginPaint\n"); //!!! brsh=(HBRUSH) SendMessage(hparentwnd,WM_CTLCOLORSTATIC,(WPARAM)hdc,(LPARAM)hwnd); ::FillRect(hdc,&ps.rcPaint,brsh); //!!! TextOut(hdc, 10, 1, "Color Red", 9); EndPaint(hwnd, &ps); return 0; case WM_ERASEBKGND: //OutputDebugString("Inside BeginPaint\n"); //hdc = (HDC) wParam; return 1;//handled case WM_MOUSEMOVE:// TRACKMOUSEEVENT tme;SystemParametersInfo SPI_GETMOUSEHOVERTIME tme.cbSize = sizeof(tme); tme.dwFlags = TME_HOVER|TME_LEAVE; tme.dwHoverTime = 15; tme.hwndTrack = hwnd; TrackMouseEvent(&tme); break; case WM_MOUSEHOVER: counter++; sprintf(Prompt,"WM_MOUSEHOVER: %d\r\n",counter); OutputDebugString(Prompt); TrackMouseEvent(&tme); break;// case WM_KEYDOWN:// if(wParam==VK_TAB)// {// HWND wnd=GetFocus();// int next=GetDlgCtrlID(wnd);// wnd=GetDlgItem(hwnd,next++);// SetFocus(wnd);// }// break; } return CallWindowProc( OldStatic, hwnd, message, wParam, lParam );}// LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ static HBRUSH hStaticBrush,hBrush[3]; static int x,y,vsPos[3],cxClient,cyClient; static TCHAR *szText[3]={"RED","GREEN","BLUE"},szBuffer[10]; static HWND hwndScroll[3],hwndText[3],hwndValue[3],hwndShow; static COLORREF crText[3],crShow; static RECT rt; int i; switch (message) { case WM_CREATE: hwndShow=CreateWindow("static",NULL, WS_CHILD | WS_VISIBLE | SS_WHITERECT, 0,0,0,0, hwnd,(HMENU)9,//ID=9 (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL); for(i=0;i<3;i++) { hwndScroll[i]=CreateWindow("scrollbar",NULL, WS_CHILD | WS_VISIBLE | SBS_VERT, 0,0,0,0, hwnd,(HMENU)i,//ID=0-2 (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL); SetScrollRange(hwndScroll[i],SB_CTL,0,255,TRUE); SetScrollPos(hwndScroll[i],SB_CTL,0,TRUE); hwndText[i]=CreateWindow("static",szText[i],//"RED..." WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_CENTER | SS_NOTIFY, 0,0,0,0, hwnd,(HMENU)(i+3),//ID=3,4,5 (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL); if(hwndText[i]) OldStatic=(WNDPROC)SetWindowLong(hwndText[i],GWL_WNDPROC,(LONG)StaticProc); hwndValue[i]=CreateWindow("static","0", WS_CHILD | WS_VISIBLE | SS_CENTER, 0,0,0,0, hwnd,(HMENU)(i+6),//ID=6,7,8 (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);// crText[2-i]=(255<<(i*4)); vsPos[i]=0;// hBrush[i]=CreateSolidBrush(crText[i]); }// hStaticBrush=CreateSolidBrush(RGB(255,200,100));//GetSysColor(COLOR_BTNHIGHLIGHT)); return 0; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); MoveWindow(hwndShow,0,0,cxClient/2,cyClient,TRUE); for(i=0;i<3;i++) { MoveWindow(hwndText[i], cxClient/36+cxClient*i/6, cyClient/15, cxClient/9, cyClient/15, TRUE); MoveWindow(hwndScroll[i], cxClient/36+cxClient*i/6, cyClient*23/100, cxClient/9, cyClient*27/50, TRUE); MoveWindow(hwndValue[i], cxClient/36+cxClient*i/6, cyClient*13/15, cxClient/9, cyClient/15, TRUE); InvalidateRect(hwndText[i],NULL,TRUE); } SetRect(&rt,cxClient/2,0,cxClient,cyClient); return 0; case WM_VSCROLL: i=GetWindowLong((HWND)lParam,GWL_ID); vsPos[i]=GetScrollPos(hwndScroll[i],SB_CTL); switch(LOWORD(wParam)) { case SB_LINEUP: vsPos[i]-=1; break; case SB_LINEDOWN: vsPos[i]+=1; break; case SB_PAGEUP: vsPos[i]-=10; break; case SB_PAGEDOWN: vsPos[i]+=10; break; case SB_THUMBPOSITION: case SB_THUMBTRACK: vsPos[i]=HIWORD(wParam); break; } vsPos[i]=max(0,min(vsPos[i],255)); SetScrollPos(hwndScroll[i],SB_CTL,vsPos[i],TRUE); wsprintf(szBuffer,"%i",vsPos[i]); SetWindowText(hwndValue[i],szBuffer); crShow=RGB(vsPos[0],vsPos[1],vsPos[2]); SetClassLong(hwnd,GCL_HBRBACKGROUND,(long)CreateSolidBrush(crShow)); crText[0]=RGB(vsPos[0],0,0); crText[1]=RGB(0,vsPos[1],0); crText[2]=RGB(0,0,vsPos[2]); // SetWindowText(hwndText[i],szText[i]); InvalidateRect(hwndText[i],NULL,TRUE); InvalidateRect(hwnd,&rt,TRUE); break;// return 0; case WM_CTLCOLORSCROLLBAR : i = GetWindowLong ((HWND) lParam, GWL_ID) ; hBrush[i]=CreateSolidBrush(crText[i]); return (LRESULT) hBrush[i] ; case WM_CTLCOLORSTATIC: SetBkMode((HDC)wParam,TRANSPARENT);// i=GetWindowLong((HWND)lParam,GWL_ID); switch(i) { case 3: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); // OutputDebugString("WM_CTLCOLORSTATIC3\n"); return (LRESULT) hStaticBrush; break; case 4: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); OutputDebugString("4\n"); return (LRESULT)hStaticBrush; break; case 5: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); OutputDebugString("5\n"); return (LRESULT)hStaticBrush; break;//#if 0 case 6: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); OutputDebugString("6\n"); return (LRESULT)hStaticBrush; break; case 7: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); OutputDebugString("7\n"); return (LRESULT)hStaticBrush; break; case 8: SetTextColor((HDC)wParam,crText[i%3]); SetBkColor((HDC)wParam,RGB(255,255,255)); OutputDebugString("8\n"); return (LRESULT)hStaticBrush; break;//#endif } break; case WM_SYSCOLORCHANGE : DeleteObject (hStaticBrush) ; hStaticBrush = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ; return 0 ; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam);} 您不能建个MFC工程吗?飞地用Win32啊!太牛x了 同一工程下两个文件之间通信 如何在GUI程序里使用Console? 请教! 急求一函数 如何将我的mac地址,通过udp包发出去。 请教高手!关于钩子程序如何控制指定窗口的问题。 MFC 位图加载后颜色失真 Windows XP 什么时候才有啊?听说要在线注册,无法用D版了……哪位高人有高见? 能不能动态的更改基于对话框(MFC)的背景呢? 怎样将LPBYTE所指的bytes数组转换成一个cstring? MFC中Tab控件的子对话框位置设置 VC如何向某个进程或窗口发送关机消息?
2、滑块变化时响应函数里m_brush.CreateSolidBrush(RGB(r, g, b);
2、重载对话框OnCtlColor,在该函数里面返回画刷句柄m_brush
{
// TODO: Add your message handler code here and/or call default
color = RGB(r,g,b);
CRect rect;
this->GetClientRect(rect);
pDC->FillSolidRect(rect, color);
return TRUE;
return CDialog::OnEraseBkgnd(pDC);
}
把三个滑块的值获取过来赋给r,g,b就OK了
COLORREF crShow;
crShow=RGB(vsPos[0],vsPos[1],vsPos[2]);
#include <windows.h>
#include <stdio.h>
//
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
TCHAR *szName="COL"; wndclass.cbClsExtra=NULL;
wndclass.cbWndExtra=NULL;
wndclass.hbrBackground=CreateSolidBrush(0);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_VREDRAW | CS_HREDRAW; if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"This Pro Need Windows NT!",
"Error",MB_OK);
return 0;
}
hwnd=CreateWindow(szName,szName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
WNDPROC OldStatic;
//
LRESULT CALLBACK StaticProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hparentwnd;
PAINTSTRUCT ps;
HDC hdc;
HBRUSH brsh;
char Prompt[40];
// int id;
static TRACKMOUSEEVENT tme;
static DWORD counter=0; switch(message)
{
case WM_PAINT: //如果处理了此消息,WM_CTLCOLORSTATIC也无效了
// OutputDebugString("StaticProc3\n");
// return CallWindowProc(OldStatic, hwnd, message, wParam, lParam );
hparentwnd=GetParent(hwnd);
// id=GetDlgCtrlID(hwnd);
// sprintf(Prompt,"%d",id);
// OutputDebugString(Prompt);
//OutputDebugString("Before BeginPaint\n");
hdc = BeginPaint(hwnd, &ps);
//OutputDebugString("After BeginPaint\n");
//!!!
brsh=(HBRUSH) SendMessage(hparentwnd,WM_CTLCOLORSTATIC,(WPARAM)hdc,(LPARAM)hwnd);
::FillRect(hdc,&ps.rcPaint,brsh);
//!!!
TextOut(hdc, 10, 1, "Color Red", 9);
EndPaint(hwnd, &ps);
return 0;
case WM_ERASEBKGND:
//OutputDebugString("Inside BeginPaint\n");
//hdc = (HDC) wParam;
return 1;//handled
case WM_MOUSEMOVE:
// TRACKMOUSEEVENT tme;SystemParametersInfo SPI_GETMOUSEHOVERTIME
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_HOVER|TME_LEAVE;
tme.dwHoverTime = 15;
tme.hwndTrack = hwnd;
TrackMouseEvent(&tme);
break;
case WM_MOUSEHOVER:
counter++;
sprintf(Prompt,"WM_MOUSEHOVER: %d\r\n",counter);
OutputDebugString(Prompt);
TrackMouseEvent(&tme);
break;
// case WM_KEYDOWN:
// if(wParam==VK_TAB)
// {
// HWND wnd=GetFocus();
// int next=GetDlgCtrlID(wnd);
// wnd=GetDlgItem(hwnd,next++);
// SetFocus(wnd);
// }
// break;
}
return CallWindowProc( OldStatic, hwnd, message, wParam, lParam );
}
//
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static HBRUSH hStaticBrush,hBrush[3];
static int x,y,vsPos[3],cxClient,cyClient;
static TCHAR *szText[3]={"RED","GREEN","BLUE"},szBuffer[10];
static HWND hwndScroll[3],hwndText[3],hwndValue[3],hwndShow;
static COLORREF crText[3],crShow;
static RECT rt;
int i;
switch (message)
{
case WM_CREATE:
hwndShow=CreateWindow("static",NULL,
WS_CHILD | WS_VISIBLE | SS_WHITERECT,
0,0,0,0,
hwnd,(HMENU)9,//ID=9
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL); for(i=0;i<3;i++)
{
hwndScroll[i]=CreateWindow("scrollbar",NULL,
WS_CHILD | WS_VISIBLE | SBS_VERT,
0,0,0,0,
hwnd,(HMENU)i,//ID=0-2
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
SetScrollRange(hwndScroll[i],SB_CTL,0,255,TRUE);
SetScrollPos(hwndScroll[i],SB_CTL,0,TRUE);
hwndText[i]=CreateWindow("static",szText[i],//"RED..."
WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_CENTER | SS_NOTIFY,
0,0,0,0,
hwnd,(HMENU)(i+3),//ID=3,4,5
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
if(hwndText[i]) OldStatic=(WNDPROC)SetWindowLong(hwndText[i],GWL_WNDPROC,(LONG)StaticProc);
hwndValue[i]=CreateWindow("static","0",
WS_CHILD | WS_VISIBLE | SS_CENTER,
0,0,0,0,
hwnd,(HMENU)(i+6),//ID=6,7,8
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
// crText[2-i]=(255<<(i*4));
vsPos[i]=0;
// hBrush[i]=CreateSolidBrush(crText[i]);
}
//
hStaticBrush=CreateSolidBrush(RGB(255,200,100));//GetSysColor(COLOR_BTNHIGHLIGHT));
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
MoveWindow(hwndShow,0,0,cxClient/2,cyClient,TRUE);
for(i=0;i<3;i++)
{
MoveWindow(hwndText[i],
cxClient/36+cxClient*i/6,
cyClient/15,
cxClient/9,
cyClient/15,
TRUE);
MoveWindow(hwndScroll[i],
cxClient/36+cxClient*i/6,
cyClient*23/100,
cxClient/9,
cyClient*27/50,
TRUE);
MoveWindow(hwndValue[i],
cxClient/36+cxClient*i/6,
cyClient*13/15,
cxClient/9,
cyClient/15,
TRUE);
InvalidateRect(hwndText[i],NULL,TRUE);
}
SetRect(&rt,cxClient/2,0,cxClient,cyClient);
return 0;
case WM_VSCROLL:
i=GetWindowLong((HWND)lParam,GWL_ID);
vsPos[i]=GetScrollPos(hwndScroll[i],SB_CTL);
switch(LOWORD(wParam))
{
case SB_LINEUP:
vsPos[i]-=1;
break;
case SB_LINEDOWN:
vsPos[i]+=1;
break;
case SB_PAGEUP:
vsPos[i]-=10;
break;
case SB_PAGEDOWN:
vsPos[i]+=10;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
vsPos[i]=HIWORD(wParam);
break;
}
vsPos[i]=max(0,min(vsPos[i],255));
SetScrollPos(hwndScroll[i],SB_CTL,vsPos[i],TRUE);
wsprintf(szBuffer,"%i",vsPos[i]);
SetWindowText(hwndValue[i],szBuffer);
crShow=RGB(vsPos[0],vsPos[1],vsPos[2]);
SetClassLong(hwnd,GCL_HBRBACKGROUND,(long)CreateSolidBrush(crShow));
crText[0]=RGB(vsPos[0],0,0);
crText[1]=RGB(0,vsPos[1],0);
crText[2]=RGB(0,0,vsPos[2]);
// SetWindowText(hwndText[i],szText[i]);
InvalidateRect(hwndText[i],NULL,TRUE);
InvalidateRect(hwnd,&rt,TRUE);
break;
// return 0;
case WM_CTLCOLORSCROLLBAR :
i = GetWindowLong ((HWND) lParam, GWL_ID) ;
hBrush[i]=CreateSolidBrush(crText[i]);
return (LRESULT) hBrush[i] ;
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam,TRANSPARENT);//
i=GetWindowLong((HWND)lParam,GWL_ID);
switch(i)
{
case 3:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
//
OutputDebugString("WM_CTLCOLORSTATIC3\n");
return (LRESULT) hStaticBrush;
break;
case 4:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
OutputDebugString("4\n");
return (LRESULT)hStaticBrush;
break;
case 5:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
OutputDebugString("5\n");
return (LRESULT)hStaticBrush;
break;
//#if 0
case 6:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
OutputDebugString("6\n");
return (LRESULT)hStaticBrush;
break;
case 7:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
OutputDebugString("7\n");
return (LRESULT)hStaticBrush;
break;
case 8:
SetTextColor((HDC)wParam,crText[i%3]);
SetBkColor((HDC)wParam,RGB(255,255,255));
OutputDebugString("8\n");
return (LRESULT)hStaticBrush;
break;
//#endif
}
break;
case WM_SYSCOLORCHANGE :
DeleteObject (hStaticBrush) ;
hStaticBrush = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}