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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo4_1   ---  Pencil & Line                         */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo4_1.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 
  12.  12 
  13.  13 void DrawGraph(HDC, BOOL);
  14.  14 void DrawPencil(HDC);
  15.  15 void DrawLine(HDC, BOOL);
  16.  16 
  17.  17 
  18.  18 int   ToolID = IDM_PENCIL;
  19.  19 POINT OrgPoint;
  20.  20 POINT PrePoint;
  21.  21 POINT CurPoint;
  22.  22 
  23.  23 /****************************************************************/
  24.  24 /*                      WinMain()                               */
  25.  25 /****************************************************************/
  26.  26 
  27.  27 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  28.  28                    LPSTR lpszCmdLine, int nCmdShow)
  29.  29 {
  30.  30    WNDCLASS wclass;
  31.  31    MSG      msg;
  32.  32    HWND     hWnd;
  33.  33    char     szName[] = "Demo4_1";
  34.  34 
  35.  35    if (!hPrevInstance)
  36.  36     {
  37.  37         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  38.  38         wclass.lpfnWndProc   = MainWndProc;
  39.  39         wclass.cbClsExtra    = 0;
  40.  40         wclass.cbWndExtra    = 0;
  41.  41         wclass.hInstance     = hInstance;
  42.  42         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  43.  43         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  44.  44         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  45.  45         wclass.lpszMenuName  = szName;
  46.  46         wclass.lpszClassName = szName;
  47.  47 
  48.  48         if (!RegisterClass (&wclass))
  49.  49            return (FALSE);
  50.  50     }
  51.  51 
  52.  52     hWnd = CreateWindow(
  53.  53                 szName,
  54.  54                 "Pencil & Line" ,
  55.  55                 WS_OVERLAPPEDWINDOW,
  56.  56                 CW_USEDEFAULT,
  57.  57                 CW_USEDEFAULT,
  58.  58                 CW_USEDEFAULT,
  59.  59                 CW_USEDEFAULT,
  60.  60                 NULL,
  61.  61                 NULL,
  62.  62                 hInstance,
  63.  63                 NULL );
  64.  64 
  65.  65     if (!hWnd)
  66.  66         return (FALSE);
  67.  67 
  68.  68     ShowWindow(hWnd, nCmdShow);
  69.  69     UpdateWindow(hWnd);
  70.  70 
  71.  71     while (GetMessage(&msg, NULL, NULL,NULL))
  72.  72        {
  73.  73            TranslateMessage(&msg);
  74.  74            DispatchMessage(&msg);
  75.  75        }
  76.  76     return (msg.wParam);
  77.  77 }
  78.  78 
  79.  79 
  80.  80 
  81.  81 /****************************************************************/
  82.  82 /*                      MainWndProc()                           */
  83.  83 /****************************************************************/
  84.  84 
  85.  85 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  86.  86                             WORD wParam, LONG lParam)
  87.  87 {
  88.  88    HDC           hDC;
  89.  89    HMENU         hMenu;
  90.  90    static BOOL   bLBDown;
  91.  91 
  92.  92 
  93.  93    switch (message)
  94.  94     {
  95.  95       case WM_CREATE :
  96.  96                 hMenu = GetMenu(hWnd);
  97.  97                 CheckMenuItem(hMenu, IDM_PENCIL, MF_CHECKED);
  98.  98                 return (0);
  99.  99 
  100. 100       case WM_COMMAND :
  101. 101                 hMenu = GetMenu(hWnd);
  102. 102                 switch (wParam)
  103. 103                   {
  104. 104                     case IDM_PENCIL :
  105. 105                     case IDM_LINE :
  106. 106 
  107. 107                          if (ToolID == wParam)
  108. 108                             return (0);
  109. 109 
  110. 110                          CheckMenuItem(hMenu, ToolID,
  111. 111                                         MF_UNCHECKED);
  112. 112                          ToolID = wParam;
  113. 113                          CheckMenuItem(hMenu, ToolID,
  114. 114                                         MF_CHECKED);
  115. 115                          break;
  116. 116 
  117. 117                     case IDM_CLEAR :
  118. 118                          InvalidateRect(hWnd, NULL, TRUE);
  119. 119                          break;
  120. 120 
  121. 121                     case IDM_QUIT :
  122. 122                          DestroyWindow(hWnd);
  123. 123                          break;
  124. 124                   }
  125. 125                 return (0);
  126. 126 
  127. 127       case WM_LBUTTONDOWN :
  128. 128                 SetCapture(hWnd);
  129. 129                 bLBDown = TRUE;
  130. 130 
  131. 131                 OrgPoint = MAKEPOINT(lParam);
  132. 132                 CurPoint = PrePoint = OrgPoint;
  133. 133 
  134. 134                 return (0);
  135. 135 
  136. 136       case WM_LBUTTONUP :
  137. 137                 bLBDown = FALSE;
  138. 138                 ReleaseCapture();
  139. 139 
  140. 140                 hDC = GetDC(hWnd);
  141. 141                 DrawGraph(hDC, TRUE);
  142. 142                 ReleaseDC(hWnd, hDC);
  143. 143 
  144. 144                 return (0);
  145. 145 
  146. 146       case WM_MOUSEMOVE :
  147. 147                 if (bLBDown)
  148. 148                   {
  149. 149                     PrePoint = CurPoint;
  150. 150                     CurPoint = MAKEPOINT(lParam);
  151. 151 
  152. 152                     hDC = GetDC(hWnd);
  153. 153                     DrawGraph(hDC, FALSE);
  154. 154                     ReleaseDC(hWnd, hDC);
  155. 155                   }
  156. 156                 return (0);
  157. 157 
  158. 158       case WM_DESTROY :
  159. 159                 PostQuitMessage(0);
  160. 160                 return (0);
  161. 161 
  162. 162       default :
  163. 163          return(DefWindowProc(hWnd, message, wParam, lParam));
  164. 164     }
  165. 165 }
  166. 166 
  167. 167 
  168. 168 
  169. 169 void DrawGraph(HDC hDC, BOOL bSure)
  170. 170 {
  171. 171    switch (ToolID)
  172. 172     {
  173. 173       case IDM_PENCIL :
  174. 174               DrawPencil(hDC);
  175. 175               break;
  176. 176 
  177. 177       case IDM_LINE :
  178. 178               DrawLine(hDC, bSure);
  179. 179               break;
  180. 180     }
  181. 181 }
  182. 182 
  183. 183 
  184. 184 
  185. 185 void DrawPencil(HDC hDC)
  186. 186 {
  187. 187    MoveTo(hDC, PrePoint.x, PrePoint.y);
  188. 188    LineTo(hDC, CurPoint.x, CurPoint.y);
  189. 189 }
  190. 190 
  191. 191 
  192. 192 
  193. 193 void DrawLine(HDC hDC, BOOL bSure)
  194. 194 {
  195. 195    int  nDrawMode;
  196. 196 
  197. 197    if (! bSure)
  198. 198      {
  199. 199        nDrawMode = SetROP2(hDC, R2_NOT);
  200. 200 
  201. 201        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  202. 202        LineTo(hDC, PrePoint.x, PrePoint.y);
  203. 203 
  204. 204        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  205. 205        LineTo(hDC, CurPoint.x, CurPoint.y);
  206. 206 
  207. 207        SetROP2(hDC, nDrawMode);
  208. 208      }
  209. 209    else
  210. 210      {
  211. 211        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  212. 212        LineTo(hDC, CurPoint.x, CurPoint.y);
  213. 213      }
  214. 214 }