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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo3_4   ---  The test of ISOTROPIC MapMode         */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include <math.h>
  7.   7 #include "demo3_4.h"
  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 void DrawGraph(HDC, int, int, POINT);
  13.  13 void DrawResult(HDC, int, int, int, POINT);
  14.  14 
  15.  15 
  16.  16 /****************************************************************/
  17.  17 /*                      WinMain()                               */
  18.  18 /****************************************************************/
  19.  19 
  20.  20 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  21.  21                    LPSTR lpszCmdLine, int nCmdShow)
  22.  22 {
  23.  23    WNDCLASS wclass;
  24.  24    MSG      msg;
  25.  25    HWND     hWnd;
  26.  26    char     szName[] = "Demo3_4";
  27.  27 
  28.  28    if (!hPrevInstance)
  29.  29     {
  30.  30         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  31.  31         wclass.lpfnWndProc   = MainWndProc;
  32.  32         wclass.cbClsExtra    = 0;
  33.  33         wclass.cbWndExtra    = 0;
  34.  34         wclass.hInstance     = hInstance;
  35.  35         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  36.  36         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  37.  37         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  38.  38         wclass.lpszMenuName  = szName;
  39.  39         wclass.lpszClassName = szName;
  40.  40 
  41.  41         if (!RegisterClass (&wclass))
  42.  42            return (FALSE);
  43.  43     }
  44.  44 
  45.  45     hWnd = CreateWindow(
  46.  46                 szName,
  47.  47                 "Curve Operation" ,
  48.  48                 WS_OVERLAPPEDWINDOW,
  49.  49                 CW_USEDEFAULT,
  50.  50                 CW_USEDEFAULT,
  51.  51                 CW_USEDEFAULT,
  52.  52                 CW_USEDEFAULT,
  53.  53                 NULL,
  54.  54                 NULL,
  55.  55                 hInstance,
  56.  56                 NULL );
  57.  57 
  58.  58     if (!hWnd)
  59.  59         return (FALSE);
  60.  60 
  61.  61     ShowWindow(hWnd, nCmdShow);
  62.  62     UpdateWindow(hWnd);
  63.  63 
  64.  64     while (GetMessage(&msg, NULL, NULL,NULL))
  65.  65        {
  66.  66            TranslateMessage(&msg);
  67.  67            DispatchMessage(&msg);
  68.  68        }
  69.  69     return (msg.wParam);
  70.  70 }
  71.  71 
  72.  72 
  73.  73 
  74.  74 /****************************************************************/
  75.  75 /*                      MainWndProc()                           */
  76.  76 /****************************************************************/
  77.  77 
  78.  78 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  79.  79                             WORD wParam, LONG lParam)
  80.  80 {
  81.  81    HDC           hDC;
  82.  82    HMENU         hMenu;
  83.  83    PAINTSTRUCT   ps;
  84.  84    static int    GraphID_1 = IDM_SIN1;
  85.  85    static int    GraphID_2 = IDM_COS2;
  86.  86    static int    OpID = IDM_ADD;
  87.  87    RECT          Rect;
  88.  88    static RECT   Rect1, Rect2, Rect3;
  89.  89    static POINT  Client;
  90.  90    BOOL          GraphChange  = FALSE;
  91.  91    BOOL          ResultChange = FALSE;
  92.  92 
  93.  93    switch (message)
  94.  94     {
  95.  95       case WM_CREATE :
  96.  96                 hMenu = GetMenu(hWnd);
  97.  97                 CheckMenuItem(hMenu, IDM_SIN1, MF_CHECKED);
  98.  98                 CheckMenuItem(hMenu, IDM_COS2, MF_CHECKED);
  99.  99                 CheckMenuItem(hMenu, IDM_ADD,  MF_CHECKED);
  100. 100                 return (0);
  101. 101 
  102. 102       case WM_COMMAND :
  103. 103                 hMenu = GetMenu(hWnd);
  104. 104                 switch (wParam)
  105. 105                   {
  106. 106                     case IDM_SIN1 :
  107. 107                     case IDM_COS1 :
  108. 108 
  109. 109                          if (GraphID_1 == wParam)
  110. 110                             return (0);
  111. 111 
  112. 112                          CheckMenuItem(hMenu, GraphID_1,
  113. 113                                         MF_UNCHECKED);
  114. 114                          GraphID_1 = wParam;
  115. 115                          CheckMenuItem(hMenu, GraphID_1,
  116. 116                                         MF_CHECKED);
  117. 117 
  118. 118                          InvalidateRect(hWnd, &Rect1, TRUE);
  119. 119                          break;
  120. 120 
  121. 121                     case IDM_SIN2 :
  122. 122                     case IDM_COS2 :
  123. 123 
  124. 124                          if (GraphID_2 == wParam)
  125. 125                             return (0);
  126. 126 
  127. 127                          CheckMenuItem(hMenu, GraphID_2,
  128. 128                                         MF_UNCHECKED);
  129. 129                          GraphID_2 = wParam;
  130. 130                          CheckMenuItem(hMenu, GraphID_2,
  131. 131                                         MF_CHECKED);
  132. 132 
  133. 133                          InvalidateRect(hWnd, &Rect2, TRUE);
  134. 134                          break;
  135. 135 
  136. 136                     case IDM_ADD  :
  137. 137                     case IDM_SUB1 :
  138. 138                     case IDM_SUB2 :
  139. 139 
  140. 140                          if (OpID == wParam)
  141. 141                             return (0);
  142. 142 
  143. 143                          CheckMenuItem(hMenu, OpID,
  144. 144                                         MF_UNCHECKED);
  145. 145                          OpID = wParam;
  146. 146                          CheckMenuItem(hMenu, OpID,
  147. 147                                         MF_CHECKED);
  148. 148 
  149. 149                          InvalidateRect(hWnd, &Rect3, TRUE);
  150. 150                          break;
  151. 151                   }
  152. 152                 return (0);
  153. 153 
  154. 154       case WM_SIZE :
  155. 155 
  156. 156                 Client.x = LOWORD(lParam);
  157. 157                 Client.y = HIWORD(lParam);
  158. 158 
  159. 159                 Rect1.left = 0;
  160. 160                 Rect1.top  = 0;
  161. 161                 Rect1.right  = Client.x/2;
  162. 162                 Rect1.bottom = Client.y/2;
  163. 163 
  164. 164                 Rect2.left = 0;
  165. 165                 Rect2.top  = Client.y/2;
  166. 166                 Rect2.right  = Client.x/2;
  167. 167                 Rect2.bottom = Client.y;
  168. 168 
  169. 169                 Rect3.left = Client.x/2;
  170. 170                 Rect3.top  = 0;
  171. 171                 Rect3.right  = Client.x;
  172. 172                 Rect3.bottom = Client.y;
  173. 173 
  174. 174                 return (0);
  175. 175 
  176. 176       case WM_PAINT :
  177. 177                 hDC = BeginPaint(hWnd, &ps);
  178. 178 
  179. 179                 SetMapMode(hDC, MM_ISOTROPIC);
  180. 180 
  181. 181                 SetWindowExt(hDC, 628, 628);
  182. 182                 SetViewportExt(hDC, Client.x, -Client.y);
  183. 183 
  184. 184                 /*      Draw the Graph 1      */
  185. 185                 IntersectRect(&Rect, &Rect1, &ps.rcPaint);
  186. 186                 if (! IsRectEmpty(&Rect))
  187. 187                    {
  188. 188                       DrawGraph(hDC, 1, GraphID_1, Client);
  189. 189                       GraphChange = TRUE;
  190. 190                    }
  191. 191 
  192. 192                 /*      Draw the Graph 2      */
  193. 193                 IntersectRect(&Rect, &Rect2, &ps.rcPaint);
  194. 194                 if (! IsRectEmpty(&Rect))
  195. 195                    {
  196. 196                       DrawGraph(hDC, 2, GraphID_2, Client);
  197. 197                       GraphChange = TRUE;
  198. 198                    }
  199. 199 
  200. 200                 /*      Draw the result of    */
  201. 201                 /*       Graph1 op Graph2     */
  202. 202                 IntersectRect(&Rect, &Rect3, &ps.rcPaint);
  203. 203                 if (! IsRectEmpty(&Rect))
  204. 204                    {  DrawResult(hDC, GraphID_1, GraphID_2,
  205. 205                                  OpID, Client);
  206. 206                       ResultChange = TRUE;
  207. 207                    }
  208. 208 
  209. 209                 EndPaint(hWnd, &ps);
  210. 210                 if (GraphChange && !ResultChange)
  211. 211                    InvalidateRect(hWnd, &Rect3, TRUE);
  212. 212                 return (0);
  213. 213 
  214. 214       case WM_DESTROY :
  215. 215                 PostQuitMessage(0);
  216. 216                 return (0);
  217. 217 
  218. 218       default :
  219. 219                 return(DefWindowProc(hWnd, message, wParam, lParam));
  220. 220     }
  221. 221 }
  222. 222 
  223. 223 
  224. 224 
  225. 225 void DrawGraph(HDC hDC, int Index, int GraphID, POINT Client)
  226. 226 {
  227. 227    int    i;
  228. 228    DWORD  rgbColor;
  229. 229    double Rad;
  230. 230    int    Val;
  231. 231    int    Width;
  232. 232 
  233. 233    SaveDC(hDC);
  234. 234 
  235. 235    if (Client.x < Client.y)
  236. 236       Width = Client.x;
  237. 237    else
  238. 238       Width = Client.y;
  239. 239 
  240. 240    if (Index == 1)
  241. 241       SetViewportOrg(hDC, Client.x/2-Width/4, Client.y/2-Width/4);
  242. 242    else
  243. 243       SetViewportOrg(hDC, Client.x/2-Width/4, Client.y/2+Width/4);
  244. 244 
  245. 245    MoveTo(hDC, -628/6, 0);
  246. 246    LineTo(hDC,  628/6, 0);
  247. 247    MoveTo(hDC, 0, -628/6);
  248. 248    LineTo(hDC, 0,  628/6);
  249. 249 
  250. 250    rgbColor = RGB(0, 0, 0);
  251. 251    if (GraphID == IDM_SIN1 || GraphID == IDM_SIN2)
  252. 252      {
  253. 253        for (i=-628; i<=628; i++)
  254. 254         {
  255. 255           Rad = (double) i/100;
  256. 256           Val = sin(Rad)*100;
  257. 257           SetPixel(hDC, i/6, Val, rgbColor);
  258. 258         }
  259. 259      }
  260. 260    else
  261. 261      {
  262. 262        for (i=-628; i<=628; i++)
  263. 263         {
  264. 264           Rad = (double) i/100;
  265. 265           Val = cos(Rad)*100;
  266. 266           SetPixel(hDC, i/6, Val, rgbColor);
  267. 267         }
  268. 268      }
  269. 269 
  270. 270    RestoreDC(hDC, -1);
  271. 271 }
  272. 272 
  273. 273 
  274. 274 
  275. 275 void DrawResult(HDC hDC, int GraphID_1, int GraphID_2,
  276. 276                 int OpID, POINT Client)
  277. 277 {
  278. 278    int    i;
  279. 279    DWORD  rgbColor;
  280. 280    double Rad;
  281. 281    int    Val;
  282. 282    double (*Func1)(double), (*Func2)(double);
  283. 283    int    Index1, Index2;
  284. 284    int    Width;
  285. 285 
  286. 286    SaveDC(hDC);
  287. 287 
  288. 288    if (Client.x < Client.y)
  289. 289       Width = Client.x;
  290. 290    else
  291. 291       Width = Client.y;
  292. 292 
  293. 293    SetViewportOrg(hDC, Client.x/2+Width/4, Client.y/2);
  294. 294 
  295. 295    MoveTo(hDC, -628/6, 0);
  296. 296    LineTo(hDC,  628/6, 0);
  297. 297    MoveTo(hDC, 0, -628/3);
  298. 298    LineTo(hDC, 0,  628/3);
  299. 299 
  300. 300    if (GraphID_1 == IDM_SIN1)
  301. 301       Func1 = sin;
  302. 302    else
  303. 303       Func1 = cos;
  304. 304 
  305. 305    if (GraphID_2 == IDM_SIN2)
  306. 306       Func2 = sin;
  307. 307    else
  308. 308       Func2 = cos;
  309. 309 
  310. 310    rgbColor = RGB(0, 0, 0);
  311. 311 
  312. 312    switch (OpID)
  313. 313      {
  314. 314        case IDM_ADD :
  315. 315               for (i=-628; i<=628; i++)
  316. 316                 {
  317. 317                   Rad = (double) i/100;
  318. 318                   Val = (Func1(Rad) + Func2(Rad)) * 100;
  319. 319                   SetPixel(hDC, i/6, Val, rgbColor);
  320. 320                 }
  321. 321               break;
  322. 322 
  323. 323        case IDM_SUB1 :
  324. 324               for (i=-628; i<=628; i++)
  325. 325                 {
  326. 326                   Rad = (double) i/100;
  327. 327                   Val = (Func1(Rad) - Func2(Rad)) * 100;
  328. 328                   SetPixel(hDC, i/6, Val, rgbColor);
  329. 329                 }
  330. 330               break;
  331. 331 
  332. 332        case IDM_SUB2 :
  333. 333               for (i=-628; i<=628; i++)
  334. 334                 {
  335. 335                   Rad = (double) i/100;
  336. 336                   Val = (Func2(Rad) - Func1(Rad)) * 100;
  337. 337                   SetPixel(hDC, i/6, Val, rgbColor);
  338. 338                 }
  339. 339               break;
  340. 340      }
  341. 341 
  342. 342    RestoreDC(hDC, -1);
  343. 343 }
  344. 344