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

📄 paint_dg.c

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

⌨️ 快捷键说明

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