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

📄 demo4_7.c

📁 WINDOWS图形编程随书光盘
💻 C
字号:
  1 /****************************************************************/
  2 /*         demo4_7   ---  LineDDA test                          */
  3 /****************************************************************/
  4 
  5 #include <windows.h>
  6 #include "demo4_7.h"
  7 
  8 
  9 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
 10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
 11 BOOL FAR PASCAL LineDlgProc(HWND, unsigned, WORD, LONG);
 12 void FAR PASCAL LineDDAProc(short, short, LPSTR);
 13 
 14 void DrawGraph(HDC, BOOL);
 15 void DrawLine(HDC, BOOL);
 16 void DrawRect(HDC, BOOL);
 17 
 18 FARPROC lpLineDlgProc;
 19 FARPROC lpLineDDAProc;
 20 
 21 HANDLE  hInst;
 22 
 23 int     ToolID  = IDM_LINE;
 24 int     StyleID = DI_STYLE1;
 25 
 26 POINT   OrgPoint;
 27 POINT   PrePoint;
 28 POINT   CurPoint;
 29 
 30 extern  COLORREF ColorStyle[3][16];
 31 
 32 /****************************************************************/
 33 /*                      WinMain()                               */
 34 /****************************************************************/
 35 
 36 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
 37                    LPSTR lpszCmdLine, int nCmdShow)
 38 {
 39    WNDCLASS wclass;
 40    MSG      msg;
 41    HWND     hWnd;
 42    char     szName[] = "Demo4_7";
 43 
 44    if (!hPrevInstance)
 45     {
 46         wclass.style         = CS_HREDRAW | CS_VREDRAW;
 47         wclass.lpfnWndProc   = MainWndProc;
 48         wclass.cbClsExtra    = 0;
 49         wclass.cbWndExtra    = 0;
 50         wclass.hInstance     = hInstance;
 51         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
 52         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
 53         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
 54         wclass.lpszMenuName  = szName;
 55         wclass.lpszClassName = szName;
 56 
 57         if (!RegisterClass (&wclass))
 58            return (FALSE);
 59     }
 60 
 61     hWnd = CreateWindow(
 62                 szName,
 63                 "LineDDA test",
 64                 WS_OVERLAPPEDWINDOW,
 65                 CW_USEDEFAULT,
 66                 CW_USEDEFAULT,
 67                 CW_USEDEFAULT,
 68                 CW_USEDEFAULT,
 69                 NULL,
 70                 NULL,
 71                 hInstance,
 72                 NULL );
 73 
 74     if (!hWnd)
 75         return (FALSE);
 76 
 77     ShowWindow(hWnd, nCmdShow);
 78     UpdateWindow(hWnd);
 79 
 80     while (GetMessage(&msg, NULL, NULL,NULL))
 81        {
 82            TranslateMessage(&msg);
 83            DispatchMessage(&msg);
 84        }
 85     return (msg.wParam);
 86 }
 87 
 88 
 89 
 90 /****************************************************************/
 91 /*                      MainWndProc()                           */
 92 /****************************************************************/
 93 
 94 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
 95                             WORD wParam, LONG lParam)
 96 {
 97    HDC           hDC;
 98    HMENU         hMenu;
 99    static BOOL   bLBDown;
100 
101    switch (message)
102     {
103       case WM_CREATE :
104                 hMenu = GetMenu(hWnd);
105                 CheckMenuItem(hMenu, IDM_LINE, MF_CHECKED);
106 
107                 hInst = ((LPCREATESTRUCT) lParam)->hInstance;
108                 return (0);
109 
110       case WM_COMMAND :
111                 hMenu = GetMenu(hWnd);
112                 switch (wParam)
113                   {
114                     case IDM_LINE       :
115                     case IDM_RECT       :
116 
117                          if (ToolID == wParam)
118                             return (0);
119 
120                          CheckMenuItem(hMenu, ToolID,
121                                         MF_UNCHECKED);
122                          ToolID = wParam;
123                          CheckMenuItem(hMenu, ToolID,
124                                         MF_CHECKED);
125                          break;
126 
127                     case IDM_CHOOSE :
128 
129                          lpLineDlgProc = MakeProcInstance(
130                                (FARPROC) LineDlgProc, hInst);
131 
132                          DialogBox(hInst, "CHOOSELINEDIALOG",
133                                    hWnd, lpLineDlgProc);
134 
135                          FreeProcInstance(lpLineDlgProc);
136                          break;
137 
138                     case IDM_CLEAR :
139                          InvalidateRect(hWnd, NULL, TRUE);
140                          break;
141 
142                     case IDM_QUIT :
143                          DestroyWindow(hWnd);
144                          break;
145                   }
146                 return (0);
147 
148       case WM_LBUTTONDOWN :
149                 SetCapture(hWnd);
150                 bLBDown = TRUE;
151 
152                 OrgPoint = MAKEPOINT(lParam);
153                 CurPoint = PrePoint = OrgPoint;
154 
155                 return (0);
156 
157       case WM_LBUTTONUP :
158                 bLBDown = FALSE;
159                 ReleaseCapture();
160 
161                 hDC = GetDC(hWnd);
162                 DrawGraph(hDC, TRUE);
163                 ReleaseDC(hWnd, hDC);
164 
165                 return (0);
166 
167       case WM_MOUSEMOVE :
168                 if (bLBDown)
169                   {
170                     PrePoint = CurPoint;
171                     CurPoint = MAKEPOINT(lParam);
172 
173                     hDC = GetDC(hWnd);
174                     DrawGraph(hDC, FALSE);
175                     ReleaseDC(hWnd, hDC);
176                   }
177                 return (0);
178 
179       case WM_DESTROY :
180                 PostQuitMessage(0);
181                 return (0);
182 
183       default :
184          return(DefWindowProc(hWnd, message, wParam, lParam));
185     }
186 }
187 
188 
189 
190 void DrawGraph(HDC hDC, BOOL bSure)
191 {
192    if (ToolID == IDM_RECT)
193      SelectObject(hDC, GetStockObject(NULL_BRUSH));
194 
195    switch (ToolID)
196      {
197        case IDM_LINE :
198               DrawLine(hDC, bSure);
199               break;
200 
201        case IDM_RECT   :
202               DrawRect(hDC, bSure);
203               break;
204      }
205 }
206 
207 
208 
209 void DrawLine(HDC hDC, BOOL bSure)
210 {
211    int  nDrawMode;
212 
213    if (! bSure)
214      {
215        nDrawMode = SetROP2(hDC, R2_NOT);
216 
217        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
218        LineTo(hDC, PrePoint.x, PrePoint.y);
219 
220        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
221        LineTo(hDC, CurPoint.x, CurPoint.y);
222 
223        SetROP2(hDC, nDrawMode);
224      }
225    else
226      {
227        lpLineDDAProc = MakeProcInstance((FARPROC)LineDDAProc,
228                                         hInst);
229 
230        LineDDA(OrgPoint.x, OrgPoint.y, CurPoint.x, CurPoint.y,
231                lpLineDDAProc, (LPSTR) &hDC);
232 
233        FreeProcInstance(lpLineDDAProc);
234      }
235 }
236 
237 
238 
239 void DrawRect(HDC hDC, BOOL bSure)
240 {
241    int  nDrawMode;
242 
243    if (! bSure)
244      {
245        nDrawMode = SetROP2(hDC, R2_NOT);
246 
247        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
248                       PrePoint.x, PrePoint.y);
249 
250        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
251                       CurPoint.x, CurPoint.y);
252 
253        SetROP2(hDC, nDrawMode);
254      }
255    else
256      {
257        lpLineDDAProc = MakeProcInstance((FARPROC) LineDDAProc,
258                                         hInst);
259 
260        LineDDA(OrgPoint.x, OrgPoint.y, OrgPoint.x, CurPoint.y,
261                lpLineDDAProc, (LPSTR) &hDC);
262        LineDDA(OrgPoint.x, CurPoint.y, CurPoint.x, CurPoint.y,
263                lpLineDDAProc, (LPSTR) &hDC);
264        LineDDA(CurPoint.x, CurPoint.y, CurPoint.x, OrgPoint.y,
265                lpLineDDAProc, (LPSTR) &hDC);
266        LineDDA(CurPoint.x, OrgPoint.y, OrgPoint.x, OrgPoint.y,
267                lpLineDDAProc, (LPSTR) &hDC);
268 
269        FreeProcInstance(lpLineDDAProc);
270      }
271 }
272 
273 
274 
275 void FAR PASCAL LineDDAProc(short x, short y, LPSTR lphDC)
276 {
277    HPEN     hPen;
278    HBRUSH   hBrush, hPreBrush;
279    static   short Num = -1;
280    COLORREF crColor;
281 
282    Num = (Num + 1) % 64;
283    if (Num % 4 != 0) return ;
284 
285    hPen = GetStockObject(NULL_PEN);
286    SelectObject(*(HDC far *)lphDC, hPen);
287 
288    crColor = ColorStyle[StyleID-DI_STYLE1][Num/4];
289    hBrush = CreateSolidBrush(crColor);
290    hPreBrush = SelectObject(*(HDC far *)lphDC, hBrush);
291 
292    Rectangle(*(HDC far *)lphDC, x-2, y-2, x+3, y+3);
293 
294    SelectObject(*(HDC far *)lphDC, hPreBrush);
295    DeleteObject(hBrush);
296 }
297 

⌨️ 快捷键说明

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