DEMO4_7.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:9k
源码类别:

GDI/图象编程

开发平台:

Visual C++

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