LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { static COLORREF crPrim[3]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255)}; static HBRUSH hBrush[3],hBrushStatic; static HWND hwndScroll[3],hwndLabel[3],hwndValue[3],hwndRect; static int color[3],cyChar; static RECT rcColor; static TCHAR * szColorLabel[]={TEXT("Red"),TEXT("Green"),TEXT("Blue")}; HINSTANCE hInstance; int i,cxClient,cyClient; TCHAR szBuffer[10]; switch(message) { case WM_CREATE: //获取句柄 hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE); //创建一个静态的矩形 hwndRect=CreateWindow(TEXT("static"),NULL,WS_CHILD|WS_VISIBLE|SS_WHITERECT,0,0,0,0,hwnd,(HMENU)9,hInstance,NULL); //创建三个滚动条控件 for(i=0;i<3;i++) { //创建,注意WS_TABTOP,这个加上后,可以使用tab;SBS_VERT是纵向滚动 hwndScroll[i]=CreateWindow(TEXT("scrollbar"),NULL,WS_CHILD|WS_VISIBLE|WS_TABSTOP|SBS_VERT,0,0,0,0,hwnd,(HMENU)i,hInstance,NULL); //设置 滚动条控件 的范围,FALSE表示不重绘 SetScrollRange(hwndScroll[i],SB_CTL,0,255,FALSE); //设置初始位置 SetScrollPos(hwndScroll[i],SB_CTL,0,FALSE); //创建滚动条上方静态文本框,设置的(HMENU)为唯一id,后面经常用过 hwndLabel[i]=CreateWindow(TEXT("static"),szColorLabel[i],WS_CHILD|WS_VISIBLE|SS_CENTER,0,0,0,0,hwnd,(HMENU)(i+3),hInstance,NULL); //创建滚动条下方显示数值的文本框 hwndValue[i]=CreateWindow(TEXT("static"),TEXT("0"),WS_CHILD|WS_VISIBLE|SS_CENTER,0,0,0,0,hwnd,(HMENU)(i+6),hInstance,NULL); //这个地方应该是调用了回调函数 //GWL_WNDPROC:获得窗口过程的地址,或代表窗口过程的地址的句柄。必须使用CallWindowProc函数调用窗口过程。 //暂时不清楚设置返回值有什么用 OldScroll[i]=(WNDPROC)SetWindowLong(hwndScroll[i],GWL_WNDPROC,(LONG)ScrollProc); //创建三个具有指定颜色的逻辑刷子 hBrush[i]=CreateSolidBrush(crPrim[i]); } hBrushStatic=CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT)); //系统字体的高度 cyChar=HIWORD(GetDialogBaseUnits()); return 0; case WM_SIZE: //客户区长宽 cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); //设置矩形内容,在此是设置颜色 SetRect(&rcColor,cxClient/2,0,cxClient,cyClient); //这个设置左侧放9个子窗口的区域的大小跟位置 MoveWindow(hwndRect,0,0,cxClient/2,cyClient,TRUE); //设置大小,位置 for(i=0;i<3;i++) { MoveWindow(hwndScroll[i],(2*i+1)*cxClient/14,2*cyChar,cxClient/14,cyClient-4*cyChar,TRUE); MoveWindow(hwndLabel[i],(4*i+1)*cxClient/28,cyChar/2,cxClient/7,cyChar,TRUE); MoveWindow(hwndValue[i],(4*i+1)*cxClient/28,cyClient-3*cyChar/2,cxClient/7,cyChar,TRUE); } //把焦点设置到父窗口 SetFocus(hwnd); return 0; case WM_SETFOCUS: //设置焦点 SetFocus(hwndScroll[idFocus]); return 0; case WM_VSCROLL: //获取id i=GetWindowLong((HWND)lParam,GWL_ID); //判断按键 switch(LOWORD(wParam)) { //省略了一些 } //设置位置 SetScrollPos(hwndScroll[i],SB_CTL,color[i],TRUE); wsprintf(szBuffer,TEXT("%i"),color[i]); //设置滚动条下方的数字 SetWindowText(hwndValue[i],szBuffer); //GCL_HBRBACKGROUND:替换与类有关的背景刷子的句柄。 DeleteObject((HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)CreateSolidBrush(RGB(color[0],color[1],color[2])))); //重绘区域,TRUE表示重绘前先清除背景 InvalidateRect(hwnd,&rcColor,TRUE); return 0; //当某个小窗口要重绘时,返回相应的刷子 case WM_CTLCOLORSCROLLBAR: i=GetWindowLong((HWND)lParam,GWL_ID); return (LRESULT)hBrush[i]; //静态文本重绘时,返回相应的刷子 case WM_CTLCOLORSTATIC: i=GetWindowLong((HWND)lParam,GWL_ID); if(i>=3&&i<=8) { SetTextColor((HDC)wParam,crPrim[i%3]); SetBkColor((HDC)wParam,GetSysColor(COLOR_BTNHIGHLIGHT)); return (LRESULT)hBrushStatic; } break; //系统颜色更改时,重新创建hBrushStatic case WM_SYSCOLORCHANGE: DeleteObject(hBrushStatic); hBrushStatic=CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT)); return 0; //清理的一些工作 case WM_DESTROY: DeleteObject((HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(WHITE_BRUSH))); for(i=0;i<3;i++) DeleteObject(hBrush[i]); DeleteObject(hBrushStatic); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } LRESULT CALLBACK ScrollProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { int id=GetWindowLong(hwnd,GWL_ID); switch(message) { //按键操作 case WM_KEYDOWN: //实现tab的功能 if(wParam==VK_TAB) SetFocus(GetDlgItem(GetParent(hwnd),(id+(GetKeyState(VK_SHIFT)<0?2:1))%3)); break; case WM_SETFOCUS: idFocus=id; break; } return CallWindowProc(OldScroll[id],hwnd,message,wParam,lParam); }
里面有很多东西值得学习,滚动条控件的使用,按钮消息与父窗口的消息传递,id的获取,按钮颜色等的设置,整个的排版等,好多好多。
代码是COLORS1的部分,走了,回宿舍,不然就被关外面了。