⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo4_5d.c

📁 WINDOWS图形编程随书光盘
💻 C
字号:
  1 #include "demo4_5.h"
  2 #include <windows.h>
  3 
  4 HBRUSH MyCreateBrush(int, COLORREF);
  5 
  6 extern HANDLE hInst;
  7 
  8 extern int    nPenColor;
  9 extern int    nPenStyle;
 10 extern int    nPenWidth;
 11 
 12 extern int    nBrushColor;
 13 extern int    nHatch;
 14 
 15 typedef struct tagCOLORSTRUCT {
 16    int  cR;
 17    int  cG;
 18    int  cB;
 19 } COLORSTRUCT;
 20 
 21 #define MKCOLOR(A) (RGB(A.cR, A.cG, A.cB))
 22 
 23 COLORSTRUCT crDefColor[28] =
 24   { {255,255,255}, {255,  0,  0}, {  0,255,  0},
 25     {  0,  0,255}, {255,255,  0}, {  0,255,255},
 26     {255,  0,255},
 27 
 28     {192,192,192}, {128,  0,  0}, {  0,128,  0},
 29     {  0,  0,128}, {128,128,  0}, {  0,128,128},
 30     {128,  0,128},
 31 
 32     {128,128,128}, {255,255,128}, {  0,255,128},
 33     {128,255,255}, {128,128,255}, {255,  0,128},
 34     {255,128, 64},
 35 
 36     {  0,  0,  0}, {128,128, 64}, {  0, 64, 64},
 37     {  0,128,255}, {  0, 64,128}, { 64,  0,128},
 38     {128, 64,  0}
 39   };
 40 
 41 COLORSTRUCT  crPCurColor[28];
 42 COLORSTRUCT  crBCurColor[28];
 43 
 44 int          nChooseColor;
 45 int          nChooseStyle;
 46 int          nChooseWidth;
 47 int          nChooseHatch;
 48 
 49 int          nCurDlgID;
 50 
 51 long FAR PASCAL ChooseCtrlProc(HWND hCtrl, unsigned msg,
 52                                WORD wParam, LONG lParam)
 53 {
 54    PAINTSTRUCT  ps;
 55    HDC          hDC;
 56    HBRUSH       hBrush;
 57    RECT         Client;
 58    HANDLE       nCtrlID;
 59 
 60    switch (msg)
 61      {
 62        case WM_PAINT :
 63           hDC = BeginPaint(hCtrl, &ps);
 64 
 65           GetClientRect(hCtrl, &Client);
 66           nCtrlID = GetWindowWord(hCtrl, GWW_ID);
 67 
 68           if (nCtrlID>=DI_H01 && nCtrlID<=DI_H07)
 69               hBrush = MyCreateBrush(nCtrlID-DI_H02, 0);
 70           else
 71               hBrush = MyCreateBrush(-1,
 72                       (nCurDlgID == IDM_CHOOSEPEN ?
 73                        MKCOLOR(crPCurColor[nCtrlID-DI_PC01]) :
 74                        MKCOLOR(crBCurColor[nCtrlID-DI_BC01])));
 75 
 76           FillRect(hDC, &Client, hBrush);
 77           DeleteObject(hBrush);
 78 
 79           EndPaint(hCtrl, &ps);
 80           return (0);
 81 
 82        case WM_LBUTTONDOWN :
 83           nCtrlID = GetWindowWord(hCtrl, GWW_ID);
 84           SendMessage(GetParent(hCtrl), WM_COMMAND,
 85                         nCtrlID, (LONG) 0);
 86           return (0);
 87      }
 88 
 89    return (DefWindowProc(hCtrl, msg, wParam, lParam));
 90 }
 91 
 92 
 93 
 94 long FAR PASCAL LineWSCtrlProc(HWND hCtrl, unsigned msg,
 95                                WORD wParam, LONG lParam)
 96 {
 97    PAINTSTRUCT  ps;
 98    HDC          hDC;
 99    HPEN         hPen, hPrePen;
100    RECT         Client;
101    HANDLE       nCtrlID;
102    int          i, Item;
103 
104    switch (msg)
105      {
106        case WM_PAINT :
107           hDC = BeginPaint(hCtrl, &ps);
108 
109           GetClientRect(hCtrl, &Client);
110           nCtrlID = GetWindowWord(hCtrl, GWW_ID);
111 
112           if (nCtrlID == DI_WIDTH)
113             {
114               for (i=1; i<10; i+=2)
115                 {
116                   hPen = CreatePen(PS_SOLID, i, 0);
117                   hPrePen = SelectObject(hDC, hPen);
118 
119                   MoveTo(hDC, Client.left,
120                                 Client.bottom*i/10);
121                   LineTo(hDC, Client.right,
122                                 Client.bottom*i/10);
123 
124                   SelectObject(hDC, hPrePen);
125                   DeleteObject(hPrePen);
126                 }
127              }
128            else
129              {
130                for (i=0; i<5; i++)
131                 {
132                   hPen = CreatePen(i, 1, 0);
133                   hPrePen = SelectObject(hDC, hPen);
134                   SetBkMode(hDC, TRANSPARENT);
135 
136                   MoveTo(hDC, Client.left,
137                                 Client.bottom*(1+2*i)/10);
138                   LineTo(hDC, Client.right,
139                                 Client.bottom*(1+2*i)/10);
140 
141                   SelectObject(hDC, hPrePen);
142                   DeleteObject(hPrePen);
143                 }
144              }
145 
146           EndPaint(hCtrl, &ps);
147           return (0);
148 
149        case WM_LBUTTONDOWN :
150           nCtrlID = GetWindowWord(hCtrl, GWW_ID);
151           GetClientRect(hCtrl, &Client);
152 
153           Item = HIWORD(lParam)/(Client.bottom/5);
154           SendMessage(GetParent(hCtrl), WM_COMMAND,
155                       nCtrlID, (LONG) Item);
156           return (0);
157      }
158 
159    return (DefWindowProc(hCtrl, msg, wParam, lParam));
160 }
161 
162 
163 
164 void ReDrawPenGraph(HDC hCtrl)
165 {
166    HWND     hWnd;
167    RECT     Client;
168    HDC      hDC;
169    HPEN     hPen, hPrePen;
170    HBRUSH   hBrush;
171    COLORREF crBkColor;
172 
173    hWnd = GetParent(GetParent(hCtrl));
174    hDC = GetDC(hWnd);
175    crBkColor = GetBkColor(hDC);
176    ReleaseDC(hWnd, hDC);
177 
178    hDC = GetDC(hCtrl);
179    GetClientRect(hCtrl, &Client);
180 
181    hBrush = CreateSolidBrush(crBkColor);
182    FillRect(hDC, &Client, hBrush);
183 
184    hPen = CreatePen(nChooseStyle, nChooseWidth,
185                     MKCOLOR(crPCurColor[nChooseColor]));
186    hPrePen = SelectObject(hDC, hPen);
187    SetBkMode(hDC, TRANSPARENT);
188 
189    MoveTo(hDC, Client.left+5, Client.top+5);
190    LineTo(hDC, Client.right-5, Client.bottom-5);
191 
192    SelectObject(hDC, hPrePen);
193    DeleteObject(hPen);
194 
195    ReleaseDC(hCtrl, hDC);
196 }
197 
198 
199 
200 BOOL FAR PASCAL PenDlgProc(HWND hDlg, unsigned msg,
201                            WORD wParam, LONG lParam)
202 {
203    HANDLE       hCtrl, hCtrlGraph;
204 
205    switch (msg)
206      {
207        case WM_INITDIALOG :
208           nChooseColor =  nPenColor;
209           nChooseStyle  = nPenStyle;
210           nChooseWidth  = nPenWidth;
211           nCurDlgID     = IDM_CHOOSEPEN;
212           return (FALSE);
213 
214        case WM_CLOSE :
215           EndDialog(hDlg, FALSE);
216           return (TRUE);
217 
218        case WM_PAINT :
219           hCtrlGraph = GetDlgItem(hDlg, DI_PGRAPH);
220           ReDrawPenGraph(hCtrlGraph);
221           return (FALSE);
222 
223        case WM_COMMAND :
224           switch (wParam)
225             {
226               case DI_OK :
227                   nPenColor =  nChooseColor;
228                   nPenWidth =  nChooseWidth;
229                   nPenStyle =  nChooseStyle;
230                   EndDialog(hDlg, TRUE);
231                   return (TRUE);
232 
233               case DI_CANCEL :
234                   EndDialog(hDlg, FALSE);
235                   return (TRUE);
236 
237               default :
238                   if (wParam>=DI_PC01 && wParam<=DI_PC28)
239                     {
240                       nChooseColor = wParam-DI_PC01;
241                       hCtrlGraph =
242                          GetDlgItem(hDlg, DI_PGRAPH);
243                       ReDrawPenGraph(hCtrlGraph);
244                     }
245                   else
246                   if (wParam == DI_WIDTH)
247                     {
248                       nChooseWidth = lParam*2 + 1;
249                       hCtrlGraph =
250                          GetDlgItem(hDlg, DI_PGRAPH);
251                       ReDrawPenGraph(hCtrlGraph);
252                     }
253                   else
254                   if (wParam == DI_STYLE)
255                     {
256                       nChooseStyle = lParam;
257                       hCtrlGraph =
258                          GetDlgItem(hDlg, DI_PGRAPH);
259                       ReDrawPenGraph(hCtrlGraph);
260                     }
261             }
262           return (TRUE);
263 
264        default :
265           return (FALSE);
266      }
267 }
268 
269 
270 
271 void ReDrawBrushGraph(HDC hCtrl)
272 {
273    HWND     hWnd;
274    RECT     Client;
275    HDC      hDC;
276    HPEN     hPen, hPrePen;
277    HBRUSH   hBrush, hPreBrush;
278    COLORREF crBkColor, crColor;
279 
280    hWnd = GetParent(GetParent(hCtrl));
281    hDC = GetDC(hWnd);
282    crBkColor = GetBkColor(hDC);
283    ReleaseDC(hWnd, hDC);
284 
285    hDC = GetDC(hCtrl);
286    GetClientRect(hCtrl, &Client);
287 
288    crColor = GetSysColor(COLOR_WINDOW);
289    hBrush = CreateSolidBrush(crColor);
290    FillRect(hDC, &Client, hBrush);
291 
292    hPen = CreatePen(nPenStyle, nPenWidth,
293                     MKCOLOR(crPCurColor[nPenColor]));
294    hBrush = MyCreateBrush(nChooseHatch,
295                     MKCOLOR(crBCurColor[nChooseColor]));
296 
297    hPrePen = SelectObject(hDC, hPen);
298    hPreBrush = SelectObject(hDC, hBrush);
299    SetBkColor(hDC, crBkColor);
300 
301    Rectangle(hDC, Client.left+2, Client.top+2,
302                   Client.right-2, Client.bottom-2);
303 
304    SelectObject(hDC, hPrePen);
305    SelectObject(hDC, hPreBrush);
306    DeleteObject(hPen);
307    DeleteObject(hBrush);
308 
309    ReleaseDC(hCtrl, hDC);
310 }
311 
312 
313 
314 BOOL FAR PASCAL BrushDlgProc(HWND hDlg, unsigned msg,
315                              WORD wParam, LONG lParam)
316 {
317    HANDLE    hCtrl, hCtrlGraph;
318 
319    switch (msg)
320      {
321        case WM_INITDIALOG :
322           nChooseColor  = nBrushColor;
323           nChooseHatch  = nHatch;
324           nCurDlgID     = IDM_CHOOSEBRUSH;
325           return (TRUE);
326 
327        case WM_CLOSE :
328           EndDialog(hDlg, FALSE);
329           return (TRUE);
330 
331        case WM_PAINT :
332           hCtrlGraph = GetDlgItem(hDlg, DI_BGRAPH);
333           ReDrawBrushGraph(hCtrlGraph);
334           return (FALSE);
335 
336        case WM_COMMAND :
337           switch (wParam)
338             {
339               case DI_OK :
340                   nBrushColor  = nChooseColor;
341                   nHatch       = nChooseHatch;
342                   EndDialog(hDlg, TRUE);
343                   return (TRUE);
344 
345               case DI_CANCEL :
346                   EndDialog(hDlg, FALSE);
347                   return (TRUE);
348 
349               default :
350                   if (wParam>=DI_BC01 && wParam<=DI_BC28)
351                     {
352                       nChooseColor = wParam-DI_BC01;
353                       hCtrlGraph =
354                          GetDlgItem(hDlg, DI_BGRAPH);
355                       ReDrawBrushGraph(hCtrlGraph);
356                     }
357                   else if (wParam>=DI_H01 && wParam<=DI_H07)
358                     {
359                       nChooseHatch = wParam-DI_H02;
360                       hCtrlGraph = GetDlgItem(hDlg, DI_BGRAPH);
361                       ReDrawBrushGraph(hCtrlGraph);
362                     }
363             }
364           return (TRUE);
365 
366        default :
367           return (FALSE);
368      }
369 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -