Paleta de Cores - RGB

Paleta de Cores - RGB
  1 /**
  2      Colors Using Scroll Bars
  3      (C) Charles Petzold, 1998-329p
  4  */
  5  
  6  #include <windows.h>
  7  
  8  LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM);
  9  LRESULT CALLBACK ScrollProc(HWND, UINT, WPARAM, LPARAM);
 10  
 11  int idFocus;
 12  WNDPROC OldScroll[3];
 13  
 14  int WINAPI  WinMain(HINSTANCE hInstance,
 15                      HINSTANCE hPrevInstance,
 16                      PSTR szCmdLine, int iCmdShow) {
 17      static TCHAR szAppName[] = TEXT("Colors1");
 18      HWND     hwnd;
 19      MSG      msg;
 20      WNDCLASS wndclass;
 21  
 22      wndclass.style         = CS_HREDRAW | CS_VREDRAW;
 23      wndclass.lpfnWndProc   = WndProc;
 24      wndclass.cbClsExtra    = 0;
 25      wndclass.cbWndExtra    = 0;
 26      wndclass.hInstance     = hInstance;
 27      wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
 28      wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
 29      wndclass.hbrBackground = GetSysColorBrush(COLOR_INFOBK)/*CreateSolidBrush(0)*/;
 30      wndclass.lpszMenuName  = NULL;
 31      wndclass.lpszClassName = szAppName;
 32  
 33      if(!RegisterClass(&wndclass)) {
 34          MessageBox(NULL, TEXT("Windows NT STD"),
 35                     szAppName, MB_ICONERROR);
 36          return 0;
 37      }
 38  
 39      hwnd = CreateWindow(szAppName, TEXT("Color Scroll"),
 40                          WS_OVERLAPPEDWINDOW,
 41                          CW_USEDEFAULT, CW_USEDEFAULT,
 42                          CW_USEDEFAULT, CW_USEDEFAULT,
 43                          NULL, NULL, hInstance, NULL);
 44  
 45      ShowWindow(hwnd, iCmdShow);
 46      UpdateWindow(hwnd);
 47  
 48      while(GetMessage(&msg, NULL, 0, 0)) {
 49          TranslateMessage(&msg);
 50          DispatchMessage (&msg);
 51      }
 52      return msg.wParam;
 53  }
 54  
 55  LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 56                           WPARAM wParam, LPARAM lParam) {
 57      static COLORREF crPrim[3] = {
 58          RGB(255, 0, 0), RGB(0, 255, 0),
 59          RGB(0, 0, 255)
 60      };
 61      static HBRUSH hBrush[3], hBrushStatic;
 62      static HWND hwndScroll[3],
 63             hwndLabel[3], hwndValue[3], hwndRect;
 64      static int color[3], cyChar;
 65      static RECT rcColor;
 66      static TCHAR *szColorLabel[] = {
 67          TEXT("Red"), TEXT("Green"), TEXT("Blue")
 68      };
 69      HINSTANCE hInstance;
 70      int i, cxClient, cyClient;
 71      TCHAR szBuffer[10];
 72  
 73      switch(message) {
 74          case WM_CREATE:
 75              hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);
 76              /**Create the white-rectangle window against width the*/
 77              /**scroll bars will be positioned. The child window ID is 9*/
 78              hwndRect = CreateWindow(TEXT("static"), NULL,
 79                                      WS_CHILD | WS_VISIBLE | SS_WHITERECT,
 80                                      0, 0, 0, 0,
 81                                      hwnd, (HMENU) 9, hInstance, NULL);
 82              for(i=0; i<3; i++) {
 83                  /**The three scroll bars have IDs 0, 1, and 2, with*/
 84                  /**scroll bar ranges from 0 through 255*/
 85                  hwndScroll[i] = CreateWindow(TEXT("scrollbar"), NULL,
 86                                               WS_CHILD | WS_VISIBLE |
 87                                               WS_TABSTOP | SBS_VERT,
 88                                               0, 0, 0, 0,
 89                                               hwnd, (HMENU) i, hInstance, NULL);
 90                  SetScrollRange(hwndScroll[i], SB_CTL, 0, 255, FALSE);
 91                  SetScrollPos(hwndScroll[i], SB_CTL, 0, FALSE);
 92                  /**The three color-name labels have IDs 3, 4, and 5,*/
 93                  /**and text strings "Red", "Green", and "Blue"*/
 94                  hwndLabel[i] = CreateWindow(TEXT("static"), szColorLabel[i],
 95                                              WS_CHILD | WS_VISIBLE | SS_CENTER,
 96                                              0, 0, 0, 0,
 97                                              hwnd, (HMENU) (i+3),
 98                                              hInstance, NULL);
 99                  /**The three color-value text fields have IDs 6, 7,*/
100                  /**and 8, and initial text strings of "0"*/
101                  hwndValue[i] = CreateWindow(TEXT("static"), TEXT("0"),
102                                              WS_CHILD | WS_VISIBLE | SS_CENTER,
103                                              0, 0, 0, 0,
104                                              hwnd, (HMENU) (i+6),
105                                              hInstance, NULL);
106                  OldScroll[i] = (WNDPROC) SetWindowLong(hwndScroll[i],
107                                                         GWL_WNDPROC,
108                                                         (LONG) ScrollProc);
109                  hBrush[i] = (HBRUSH)/*CreateSolidBrush*/(crPrim[i]);
110              }
111              hBrushStatic = (HBRUSH)/*CreateSolidBrush*/(GetSysColor(COLOR_BTNHIGHLIGHT));
112  
113              cyChar = HIWORD(GetDialogBaseUnits());
114              return 0;
115  
116          case WM_SIZE:
117              cxClient = LOWORD(lParam);
118              cyClient = HIWORD(lParam);
119              SetRect(&rcColor, cxClient/2, 0, cxClient, cyClient);
120              MoveWindow(hwndRect, 0, 0, cxClient/2, cyClient, TRUE);
121  
122              for(i=0; i<3; i++) {
123                  MoveWindow(hwndScroll[i],
124                             (2*i+1)*cxClient/14,2*cyChar,
125                             cxClient/14,cyClient-4*cyChar, TRUE);
126                  MoveWindow(hwndLabel[i],
127                             (4*i+1)*cxClient/28, cyChar/2,
128                             cyClient/7,cyChar, TRUE);
129                  MoveWindow(hwndValue[i],
130                             (4*i+1)*cxClient/28,
131                             cyClient-3*cyChar/2,
132                             cxClient/7, cyChar, TRUE);
133              }
134              SetFocus(hwnd);
135              return 0;
136  
137          case WM_SETFOCUS:
138              SetFocus(hwndScroll[idFocus]);
139              return 0;
140          case WM_VSCROLL:
141              i = GetWindowLong((HWND) lParam, GWL_ID);
142              switch(LOWORD(wParam)) {
143                  case SB_PAGEDOWN:
144                      color[i]+=15;
145                      /**Fall through*/
146                  case SB_LINEDOWN:
147                      color[i]=min(255,color[i]+1);
148                      break;
149                  case SB_PAGEUP:
150                      color[i] -= 15;
151                      /**Fall through*/
152                  case SB_LINEUP:
153                      color[i] = max(0, color[i] - 1);
154                      break;
155                  case SB_TOP:
156                      color[i] = 0;
157                      break;
158                  case SB_BOTTOM:
159                      color[i] = 255;
160                      break;
161                  case SB_THUMBPOSITION:
162                  case SB_THUMBTRACK:
163                      color[i] = HIWORD(wParam);
164                      break;
165                  default:
166                      break;
167              }
168              SetScrollPos(hwndScroll[i], SB_CTL, color[i], TRUE);
169              wsprintf(szBuffer, TEXT("%i"), color[i]);
170              SetWindowText(hwndValue[i], szBuffer);
171              (BOOL)/*DeleteObject*/((HBRUSH) SetClassLong(hwnd, GCL_HBRBACKGROUND,
172                             (LONG) (HBRUSH)/*CreateSolidBrush*/(RGB(color[0], color[1], color[2]))));
173              InvalidateRect(hwnd, &rcColor, TRUE);
174              return 0;
175          case WM_CTLCOLORSCROLLBAR:
176              i = GetWindowLong((HWND) lParam, GWL_ID);
177              return (LRESULT) hBrush[i];
178          case WM_CTLCOLORSTATIC:
179              i = GetWindowLong((HWND) lParam, GWL_ID);
180  
181              if(i>3 && i<=8) /**Static text controls*/ {
182                  (HGDIOBJ)/*SetTextColor*/((HDC) wParam, crPrim[i%3]);
183                  (HGDIOBJ)/*SetBkColor*/((HDC) wParam, GetSysColor(COLOR_BTNHIGHLIGHT));
184                  return (LRESULT) hBrushStatic;
185              }
186              break;
187          case WM_SYSCOLORCHANGE:
188              (BOOL)/*DeleteObject*/(hBrushStatic);
189              hBrushStatic=(HBRUSH)/*CreateSolidBrush*/(GetSysColor(COLOR_BTNHIGHLIGHT));
190              return 0;
191          case WM_DESTROY:
192              (BOOL)/*DeleteObject*/(
193                  (HBRUSH) SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)
194                                        (int)/*GetStockObject*/(WHITE_BRUSH))
195              );
196              for(i=0;i<3;i++)
197                  (BOOL)/*DeleteObject*/(hBrush[i]);
198              (BOOL)/*DeleteObject*/(hBrushStatic);
199              PostQuitMessage(0);
200              return 0;
201      }
202      return DefWindowProc(hwnd, message, wParam, lParam);
203   }
204  
205   LRESULT CALLBACK ScrollProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
206      int id = GetWindowLong(hwnd, GWL_ID);
207      switch(message) {
208          case WM_KEYDOWN:
209              if(wParam==VK_TAB)
210                  SetFocus(GetDlgItem(GetParent(hwnd),
211                                      (id+(GetKeyState(VK_SHIFT)<0?2:1))%3));
212              break;
213          case WM_SETFOCUS:
214              idFocus=id;
215              break;
216      }
217      return CallWindowProc(OldScroll[id], hwnd, message, wParam, lParam);
218   }

Comentários