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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo3_2   ---  The simple CAD                        */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo3_2.h"
  7.   7 
  8.   8 #define MAXPOINTS       30
  9.   9 #define MaxX          1000
  10.  10 #define MaxY          1000
  11.  11 
  12.  12 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  13.  13 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  14.  14 
  15.  15 
  16.  16 
  17.  17 /****************************************************************/
  18.  18 /*                      WinMain()                               */
  19.  19 /****************************************************************/
  20.  20 
  21.  21 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  22.  22                    LPSTR lpszCmdLine, int nCmdShow)
  23.  23 {
  24.  24    WNDCLASS wclass;
  25.  25    MSG      msg;
  26.  26    HWND     hWnd;
  27.  27    char     szName[] = "Demo3_2";
  28.  28 
  29.  29    if (!hPrevInstance)
  30.  30     {
  31.  31         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  32.  32         wclass.lpfnWndProc   = MainWndProc;
  33.  33         wclass.cbClsExtra    = 0;
  34.  34         wclass.cbWndExtra    = 0;
  35.  35         wclass.hInstance     = hInstance;
  36.  36         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  37.  37         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  38.  38         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  39.  39         wclass.lpszMenuName  = szName;
  40.  40         wclass.lpszClassName = szName;
  41.  41 
  42.  42         if (!RegisterClass (&wclass))
  43.  43            return (FALSE);
  44.  44     }
  45.  45 
  46.  46     hWnd = CreateWindow(
  47.  47                 szName,
  48.  48                 "The simple CAD",
  49.  49                 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  50.  50                 CW_USEDEFAULT,
  51.  51                 CW_USEDEFAULT,
  52.  52                 CW_USEDEFAULT,
  53.  53                 CW_USEDEFAULT,
  54.  54                 NULL,
  55.  55                 NULL,
  56.  56                 hInstance,
  57.  57                 NULL );
  58.  58 
  59.  59     if (!hWnd)
  60.  60         return (FALSE);
  61.  61 
  62.  62     ShowWindow(hWnd, nCmdShow);
  63.  63     UpdateWindow(hWnd);
  64.  64 
  65.  65     while (GetMessage(&msg, NULL, NULL,NULL))
  66.  66        {
  67.  67            TranslateMessage(&msg);
  68.  68            DispatchMessage(&msg);
  69.  69        }
  70.  70     return (msg.wParam);
  71.  71 }
  72.  72 
  73.  73 
  74.  74 
  75.  75 /****************************************************************/
  76.  76 /*                      MainWndProc()                           */
  77.  77 /****************************************************************/
  78.  78 
  79.  79 typedef struct tagSHAPE {
  80.  80            int  x1, y1;
  81.  81            int  x2, y2;
  82.  82            int  ShapeID;
  83.  83 } SHAPE;
  84.  84 
  85.  85 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  86.  86                             WORD wParam, LONG lParam)
  87.  87 {
  88.  88    PAINTSTRUCT   ps;
  89.  89    HDC           hDC;
  90.  90    HMENU         hMenu;
  91.  91    static int    ShapeID = IDM_LINE;
  92.  92    RECT          Rect, dstRect;
  93.  93    int           x1, x2, y1, y2;
  94.  94    static int    CX, CY;
  95.  95    int           i, Temp;
  96.  96    static int    Num = 0;
  97.  97    static SHAPE  Shape[MAXPOINTS];
  98.  98    static int    OX, OY;
  99.  99 
  100. 100    switch (message)
  101. 101     {
  102. 102         case WM_CREATE :
  103. 103                 srand(LOWORD(GetCurrentTime()));
  104. 104                 break;
  105. 105 
  106. 106         case WM_COMMAND :
  107. 107                 switch(wParam)
  108. 108                   {
  109. 109                     case IDM_CLEAR :
  110. 110                          Num = 0;
  111. 111                          InvalidateRect(hWnd, NULL, TRUE);
  112. 112                          UpdateWindow(hWnd);
  113. 113                          break;
  114. 114 
  115. 115                     case IDM_EXIT :
  116. 116                          DestroyWindow (hWnd);
  117. 117                          break;
  118. 118 
  119. 119                     case IDM_LINE :
  120. 120                          if (ShapeID != IDM_LINE)
  121. 121                            {
  122. 122                              hMenu = GetMenu(hWnd);
  123. 123                              CheckMenuItem(hMenu, ShapeID,
  124. 124                                                   MF_UNCHECKED);
  125. 125                              ShapeID = IDM_LINE;
  126. 126                              CheckMenuItem(hMenu, ShapeID,
  127. 127                                                   MF_CHECKED);
  128. 128                            }
  129. 129                          break;
  130. 130 
  131. 131                     case IDM_RECT :
  132. 132                          if (ShapeID != IDM_RECT)
  133. 133                            {
  134. 134                              hMenu = GetMenu(hWnd);
  135. 135                              CheckMenuItem(hMenu, ShapeID,
  136. 136                                                   MF_UNCHECKED);
  137. 137                              ShapeID = IDM_RECT;
  138. 138                              CheckMenuItem(hMenu, ShapeID,
  139. 139                                                   MF_CHECKED);
  140. 140                            }
  141. 141                          break;
  142. 142 
  143. 143                     case IDM_ELLIP :
  144. 144                          if (ShapeID != IDM_ELLIP)
  145. 145                            {
  146. 146                              hMenu = GetMenu(hWnd);
  147. 147                              CheckMenuItem(hMenu, ShapeID,
  148. 148                                                   MF_UNCHECKED);
  149. 149                              ShapeID = IDM_ELLIP;
  150. 150                              CheckMenuItem(hMenu, ShapeID,
  151. 151                                                   MF_CHECKED);
  152. 152                            }
  153. 153                          break;
  154. 154 
  155. 155                     case IDM_DRAW :
  156. 156                          if (Num == MAXPOINTS)
  157. 157                            {
  158. 158                               MessageBox(hWnd,
  159. 159                                 "Memory not enough", "Warning",
  160. 160                                 MB_OK | MB_ICONEXCLAMATION);
  161. 161                               break;
  162. 162                            }
  163. 163 
  164. 164                          hDC = GetDC(hWnd);
  165. 165 
  166. 166                          x1 = rand() % CX;
  167. 167                          y1 = rand() % CY;
  168. 168                          x2 = rand() % CX;
  169. 169                          y2 = rand() % CY;
  170. 170 
  171. 171                          switch (ShapeID)
  172. 172                            {
  173. 173                             case IDM_LINE :
  174. 174                                  MoveTo(hDC, x1, y1);
  175. 175                                  LineTo(hDC, x2, y2);
  176. 176                                  break;
  177. 177 
  178. 178                             case IDM_RECT :
  179. 179                                  Rectangle(hDC, x1, y1, x2, y2);
  180. 180                                  break;
  181. 181 
  182. 182                             case IDM_ELLIP :
  183. 183                                  Ellipse(hDC, x1, y1, x2, y2);
  184. 184                                  break;
  185. 185                            }
  186. 186 
  187. 187                          ReleaseDC(hWnd, hDC);
  188. 188 
  189. 189                          Shape[Num].x1 = OX + x1;
  190. 190                          Shape[Num].y1 = OY + y1;
  191. 191                          Shape[Num].x2 = OX + x2;
  192. 192                          Shape[Num].y2 = OY + y2;
  193. 193                          Shape[Num].ShapeID = ShapeID;
  194. 194                          Num ++;
  195. 195                          break;
  196. 196                   }
  197. 197                 break;
  198. 198 
  199. 199       case WM_PAINT :
  200. 200             hDC = BeginPaint(hWnd, &ps);
  201. 201             for (i=0; i<Num; i++)
  202. 202               {
  203. 203                 Rect.left  = min(Shape[i].x1-OX, Shape[i].x2-OX);
  204. 204                 Rect.right = max(Shape[i].x1-OX, Shape[i].x2-OX);
  205. 205                 Rect.top   = min(Shape[i].y1-OY, Shape[i].y2-OY);
  206. 206                 Rect.bottom= max(Shape[i].y1-OY, Shape[i].y2-OY);
  207. 207                 IntersectRect(&dstRect, &Rect, &(ps.rcPaint));
  208. 208                 if (IsRectEmpty(&dstRect))
  209. 209                     continue;
  210. 210 
  211. 211                 SetWindowOrg(hDC, OX, OY);
  212. 212                 switch (Shape[i].ShapeID)
  213. 213                  {
  214. 214                    case IDM_LINE :
  215. 215                      MoveTo(hDC, Shape[i].x1, Shape[i].y1);
  216. 216                      LineTo(hDC, Shape[i].x2, Shape[i].y2);
  217. 217                      break;
  218. 218 
  219. 219                    case IDM_RECT :
  220. 220                      Rectangle(hDC, Shape[i].x1, Shape[i].y1,
  221. 221                                     Shape[i].x2, Shape[i].y2);
  222. 222                      break;
  223. 223 
  224. 224                    case IDM_ELLIP :
  225. 225                      Ellipse(hDC, Shape[i].x1, Shape[i].y1,
  226. 226                                   Shape[i].x2, Shape[i].y2);
  227. 227                      break;
  228. 228                  }
  229. 229               }
  230. 230             EndPaint(hWnd, &ps);
  231. 231             break;
  232. 232 
  233. 233       case WM_SIZE :
  234. 234 
  235. 235                 CX = LOWORD(lParam);
  236. 236                 CY = HIWORD(lParam);
  237. 237                 SetScrollRange(hWnd, SB_HORZ, 0, MaxX-CX, TRUE);
  238. 238                 SetScrollRange(hWnd, SB_VERT, 0, MaxY-CY, TRUE);
  239. 239                 OX = min(OX, MaxX-CX);
  240. 240                 OY = min(OY, MaxY-CY);
  241. 241                 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
  242. 242                 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
  243. 243                 break;
  244. 244 
  245. 245       case WM_KEYDOWN :
  246. 246                 switch (wParam)
  247. 247                   {
  248. 248                     case VK_HOME :
  249. 249                         SendMessage(hWnd, WM_HSCROLL,
  250. 250                                     SB_TOP, 0L);
  251. 251                         break;
  252. 252 
  253. 253                     case VK_END :
  254. 254                         SendMessage(hWnd, WM_HSCROLL,
  255. 255                                     SB_BOTTOM, 0L);
  256. 256                         break;
  257. 257 
  258. 258                     case VK_PRIOR :
  259. 259                         SendMessage(hWnd, WM_VSCROLL,
  260. 260                                     SB_TOP, 0L);
  261. 261                         break;
  262. 262 
  263. 263                     case VK_NEXT :
  264. 264                         SendMessage(hWnd, WM_VSCROLL,
  265. 265                                     SB_BOTTOM, 0L);
  266. 266                         break;
  267. 267 
  268. 268                     case VK_LEFT :
  269. 269                         SendMessage(hWnd, WM_HSCROLL,
  270. 270                                     SB_LINEUP, 0L);
  271. 271                         break;
  272. 272 
  273. 273                     case VK_RIGHT :
  274. 274                         SendMessage(hWnd, WM_HSCROLL,
  275. 275                                     SB_LINEDOWN, 0L);
  276. 276                         break;
  277. 277 
  278. 278                     case VK_UP :
  279. 279                         SendMessage(hWnd, WM_VSCROLL,
  280. 280                                     SB_LINEUP, 0L);
  281. 281                         break;
  282. 282 
  283. 283                     case VK_DOWN :
  284. 284                         SendMessage(hWnd, WM_VSCROLL,
  285. 285                                     SB_LINEDOWN, 0L);
  286. 286                         break;
  287. 287                   }
  288. 288                 break;
  289. 289 
  290. 290       case WM_HSCROLL :
  291. 291                 switch (wParam)
  292. 292                   {
  293. 293                     case SB_TOP :
  294. 294                         Temp = 0;
  295. 295                         break;
  296. 296 
  297. 297                     case SB_BOTTOM :
  298. 298                         Temp = MaxX - CX;
  299. 299                         break;
  300. 300 
  301. 301                     case SB_LINEUP :
  302. 302                     case SB_PAGEUP :
  303. 303                         Temp = OX - CX/4;
  304. 304                         break;
  305. 305 
  306. 306                     case SB_LINEDOWN :
  307. 307                     case SB_PAGEDOWN :
  308. 308                         Temp = OX + CX/4;
  309. 309                         break;
  310. 310 
  311. 311                     case SB_THUMBPOSITION :
  312. 312                         Temp = LOWORD(lParam);
  313. 313                         break;
  314. 314 
  315. 315                     default :
  316. 316                         return (NULL);
  317. 317                   }
  318. 318                 Temp = min(max(0, Temp), MaxX-CX);
  319. 319                 if (Temp == OX) break;
  320. 320 
  321. 321                 OX = Temp;
  322. 322                 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
  323. 323                 InvalidateRect(hWnd, NULL, TRUE);
  324. 324                 break;
  325. 325 
  326. 326       case WM_VSCROLL :
  327. 327                 switch (wParam)
  328. 328                   {
  329. 329                     case SB_TOP :
  330. 330                         Temp = 0;
  331. 331                         break;
  332. 332 
  333. 333                     case SB_BOTTOM :
  334. 334                         Temp = MaxY - CY;
  335. 335                         break;
  336. 336 
  337. 337                     case SB_LINEUP :
  338. 338                     case SB_PAGEUP :
  339. 339                         Temp = OY - CY/4;
  340. 340                         break;
  341. 341 
  342. 342                     case SB_LINEDOWN :
  343. 343                     case SB_PAGEDOWN :
  344. 344                         Temp = OY + CY/4;
  345. 345                         break;
  346. 346 
  347. 347                     case SB_THUMBPOSITION :
  348. 348                         Temp = LOWORD(lParam);
  349. 349                         break;
  350. 350 
  351. 351                     default :
  352. 352                         return (NULL);
  353. 353                   }
  354. 354                 Temp = min(max(0, Temp), MaxY-CY);
  355. 355                 if (Temp == OY) break;
  356. 356 
  357. 357                 OY = Temp;
  358. 358                 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
  359. 359                 InvalidateRect(hWnd, NULL, TRUE);
  360. 360                 break;
  361. 361 
  362. 362       case WM_DESTROY :
  363. 363                 PostQuitMessage(0);
  364. 364                 break ;
  365. 365 
  366. 366       default :
  367. 367                 return (DefWindowProc(hWnd, message, wParam, lParam));
  368. 368     }
  369. 369    return (NULL);
  370. 370 }