DEMO6_1.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:9k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo6_1 --- MetaFile Test */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo6_1.h"
- 7
- 8 #define MAXPOINTS 30
- 9
- 10 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 11 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 12
- 13
- 14 /****************************************************************/
- 15 /* WinMain() */
- 16 /****************************************************************/
- 17
- 18 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 19 LPSTR lpszCmdLine, int nCmdShow)
- 20 {
- 21 WNDCLASS wclass;
- 22 MSG msg;
- 23 HWND hWnd;
- 24 char szName[] = "Demo6_1";
- 25
- 26 if (!hPrevInstance)
- 27 {
- 28 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 29 wclass.lpfnWndProc = MainWndProc;
- 30 wclass.cbClsExtra = 0;
- 31 wclass.cbWndExtra = 0;
- 32 wclass.hInstance = hInstance;
- 33 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 34 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 35 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 36 wclass.lpszMenuName = szName;
- 37 wclass.lpszClassName = szName;
- 38
- 39 if (!RegisterClass (&wclass))
- 40 return (FALSE);
- 41 }
- 42
- 43 hWnd = CreateWindow(
- 44 szName,
- 45 "MetaFile Test",
- 46 WS_OVERLAPPEDWINDOW,
- 47 CW_USEDEFAULT,
- 48 CW_USEDEFAULT,
- 49 CW_USEDEFAULT,
- 50 CW_USEDEFAULT,
- 51 NULL,
- 52 NULL,
- 53 hInstance,
- 54 NULL );
- 55
- 56 if (!hWnd)
- 57 return (FALSE);
- 58
- 59 ShowWindow(hWnd, nCmdShow);
- 60 UpdateWindow(hWnd);
- 61
- 62 while (GetMessage(&msg, NULL, NULL,NULL))
- 63 {
- 64 TranslateMessage(&msg);
- 65 DispatchMessage(&msg);
- 66 }
- 67 return (msg.wParam);
- 68 }
- 69
- 70
- 71
- 72 /****************************************************************/
- 73 /* MainWndProc() */
- 74 /****************************************************************/
- 75
- 76 typedef struct tagSHAPE {
- 77 int x1, y1;
- 78 int x2, y2;
- 79 int ShapeID;
- 80 } SHAPE;
- 81
- 82 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 83 WORD wParam, LONG lParam)
- 84 {
- 85 PAINTSTRUCT ps;
- 86 HDC hDC;
- 87 HMENU hMenu;
- 88 static int ShapeID = IDM_LINE;
- 89 RECT Rect;
- 90 int x1, x2, y1, y2;
- 91 static int CX, CY;
- 92 int i;
- 93 static int Num = 0;
- 94 static SHAPE Shape[MAXPOINTS];
- 95 static HDC hMetaDC;
- 96 char Filename[] = "demo6_1.mf";
- 97
- 98 switch (message)
- 99 {
- 100 case WM_CREATE :
- 101 srand(LOWORD(GetCurrentTime()));
- 102 hMetaDC = CreateMetaFile(Filename);
- 103 return (0);
- 104
- 105 case WM_COMMAND :
- 106 switch(wParam)
- 107 {
- 108 case IDM_EXIT :
- 109 DestroyWindow (hWnd);
- 110
- 111 break;
- 112
- 113 case IDM_LINE :
- 114 if (ShapeID != IDM_LINE)
- 115 {
- 116 hMenu = GetMenu(hWnd);
- 117 CheckMenuItem(hMenu, ShapeID,
- 118 MF_UNCHECKED);
- 119 ShapeID = IDM_LINE;
- 120 CheckMenuItem(hMenu, ShapeID,
- 121 MF_CHECKED);
- 122 }
- 123 break;
- 124
- 125 case IDM_RECT :
- 126 if (ShapeID != IDM_RECT)
- 127 {
- 128 hMenu = GetMenu(hWnd);
- 129 CheckMenuItem(hMenu, ShapeID,
- 130 MF_UNCHECKED);
- 131 ShapeID = IDM_RECT;
- 132 CheckMenuItem(hMenu, ShapeID,
- 133 MF_CHECKED);
- 134 }
- 135 break;
- 136
- 137 case IDM_ELLIP :
- 138 if (ShapeID != IDM_ELLIP)
- 139 {
- 140 hMenu = GetMenu(hWnd);
- 141 CheckMenuItem(hMenu, ShapeID,
- 142 MF_UNCHECKED);
- 143 ShapeID = IDM_ELLIP;
- 144 CheckMenuItem(hMenu, ShapeID,
- 145 MF_CHECKED);
- 146 }
- 147 break;
- 148
- 149 case IDM_DRAW :
- 150 if (Num == MAXPOINTS)
- 151 {
- 152 MessageBox(hWnd,
- 153 "Memory not enough", "Warning",
- 154 MB_OK | MB_ICONEXCLAMATION);
- 155 break;
- 156 }
- 157
- 158 hDC = GetDC(hWnd);
- 159
- 160 x1 = rand() % CX;
- 161 y1 = rand() % CY;
- 162 x2 = rand() % CX;
- 163 y2 = rand() % CY;
- 164
- 165 switch (ShapeID)
- 166 {
- 167 case IDM_LINE :
- 168 MoveTo(hDC, x1, y1);
- 169 LineTo(hDC, x2, y2);
- 170 MoveTo(hMetaDC, x1, y1);
- 171 LineTo(hMetaDC, x2, y2);
- 172 break;
- 173
- 174 case IDM_RECT :
- 175 Rectangle(hDC, x1, y1,
- 176 x2, y2);
- 177 Rectangle(hMetaDC, x1, y1,
- 178 x2, y2);
- 179 break;
- 180
- 181 case IDM_ELLIP :
- 182 Ellipse(hDC, x1, y1,
- 183 x2, y2);
- 184 Ellipse(hMetaDC, x1, y1,
- 185 x2, y2);
- 186 break;
- 187 }
- 188
- 189 ReleaseDC(hWnd, hDC);
- 190
- 191 Shape[Num].x1 = x1;
- 192 Shape[Num].y1 = y1;
- 193 Shape[Num].x2 = x2;
- 194 Shape[Num].y2 = y2;
- 195 Shape[Num].ShapeID = ShapeID;
- 196 Num ++;
- 197 break;
- 198 }
- 199 return (0);
- 200
- 201 case WM_PAINT :
- 202 hDC = BeginPaint(hWnd, &ps);
- 203 for (i=0; i<Num; i++)
- 204 {
- 205 switch (Shape[i].ShapeID)
- 206 {
- 207 case IDM_LINE :
- 208 MoveTo(hDC, Shape[i].x1, Shape[i].y1);
- 209 LineTo(hDC, Shape[i].x2, Shape[i].y2);
- 210 break;
- 211
- 212 case IDM_RECT :
- 213 Rectangle(hDC, Shape[i].x1, Shape[i].y1,
- 214 Shape[i].x2, Shape[i].y2);
- 215 break;
- 216
- 217 case IDM_ELLIP :
- 218 Ellipse(hDC, Shape[i].x1, Shape[i].y1,
- 219 Shape[i].x2, Shape[i].y2);
- 220 break;
- 221 }
- 222 }
- 223 EndPaint(hWnd, &ps);
- 224 return (0);
- 225
- 226 case WM_SIZE :
- 227 GetClientRect(hWnd, &Rect);
- 228 CX = Rect.right - Rect.left;
- 229 CY = Rect.bottom - Rect.top;
- 230 return (0);
- 231
- 232 case WM_DESTROY :
- 233 DeleteMetaFile(CloseMetaFile(hMetaDC));
- 234 PostQuitMessage(0);
- 235 return (0) ;
- 236
- 237 default :
- 238 return (DefWindowProc(hWnd, message, wParam, lParam));
- 239 }
- 240 }