DEMO1_1.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:7k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo1_1 --- The simple example */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo1_1.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11
- 12
- 13 /****************************************************************/
- 14 /* WinMain() */
- 15 /****************************************************************/
- 16
- 17 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 18 LPSTR lpszCmdLine, int nCmdShow)
- 19 {
- 20 WNDCLASS wclass;
- 21 MSG msg;
- 22 HWND hWnd;
- 23 char szName[] = "Demo1_1";
- 24
- 25 if (!hPrevInstance)
- 26 {
- 27 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 28 wclass.lpfnWndProc = MainWndProc;
- 29 wclass.cbClsExtra = 0;
- 30 wclass.cbWndExtra = 0;
- 31 wclass.hInstance = hInstance;
- 32 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 33 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 34 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 35 wclass.lpszMenuName = szName;
- 36 wclass.lpszClassName = szName;
- 37
- 38 if (!RegisterClass (&wclass))
- 39 return (FALSE);
- 40 }
- 41
- 42 hWnd = CreateWindow(
- 43 szName,
- 44 "The simple example",
- 45 WS_OVERLAPPEDWINDOW,
- 46 CW_USEDEFAULT,
- 47 CW_USEDEFAULT,
- 48 CW_USEDEFAULT,
- 49 CW_USEDEFAULT,
- 50 NULL,
- 51 NULL,
- 52 hInstance,
- 53 NULL );
- 54
- 55 if (!hWnd)
- 56 return (FALSE);
- 57
- 58 ShowWindow(hWnd, nCmdShow);
- 59 UpdateWindow(hWnd);
- 60
- 61 while (GetMessage(&msg, NULL, NULL,NULL))
- 62 {
- 63 TranslateMessage(&msg);
- 64 DispatchMessage(&msg);
- 65 }
- 66 return (msg.wParam);
- 67 }
- 68
- 69
- 70
- 71 /****************************************************************/
- 72 /* MainWndProc() */
- 73 /****************************************************************/
- 74
- 75 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 76 WORD wParam, LONG lParam)
- 77 {
- 78 HDC hDC;
- 79 HMENU hMenu;
- 80 static int ShapeID = IDM_LINE;
- 81 RECT Rect;
- 82 int x1, x2, y1, y2;
- 83 static int CX, CY;
- 84
- 85 switch (message)
- 86 {
- 87 case WM_CREATE :
- 88 srand(LOWORD(GetCurrentTime()));
- 89 break;
- 90
- 91 case WM_COMMAND :
- 92 switch(wParam)
- 93 {
- 94 case IDM_CLEAR :
- 95 InvalidateRect(hWnd, NULL, TRUE);
- 96 UpdateWindow(hWnd);
- 97 break;
- 98
- 99 case IDM_EXIT :
- 100 DestroyWindow (hWnd);
- 101 break;
- 102
- 103 case IDM_LINE :
- 104 if (ShapeID != IDM_LINE)
- 105 {
- 106 hMenu = GetMenu(hWnd);
- 107 CheckMenuItem(hMenu, ShapeID,
- 108 MF_UNCHECKED);
- 109 ShapeID = IDM_LINE;
- 110 CheckMenuItem(hMenu, ShapeID,
- 111 MF_CHECKED);
- 112 }
- 113 break;
- 114
- 115 case IDM_RECT :
- 116 if (ShapeID != IDM_RECT)
- 117 {
- 118 hMenu = GetMenu(hWnd);
- 119 CheckMenuItem(hMenu, ShapeID,
- 120 MF_UNCHECKED);
- 121 ShapeID = IDM_RECT;
- 122 CheckMenuItem(hMenu, ShapeID,
- 123 MF_CHECKED);
- 124 }
- 125 break;
- 126
- 127 case IDM_ELLIP :
- 128 if (ShapeID != IDM_ELLIP)
- 129 {
- 130 hMenu = GetMenu(hWnd);
- 131 CheckMenuItem(hMenu, ShapeID,
- 132 MF_UNCHECKED);
- 133 ShapeID = IDM_ELLIP;
- 134 CheckMenuItem(hMenu, ShapeID,
- 135 MF_CHECKED);
- 136 }
- 137 break;
- 138
- 139 case IDM_DRAW :
- 140 hDC = GetDC(hWnd);
- 141
- 142 x1 = rand() % CX;
- 143 y1 = rand() % CY;
- 144 x2 = rand() % CX;
- 145 y2 = rand() % CY;
- 146
- 147 switch (ShapeID)
- 148 {
- 149 case IDM_LINE :
- 150 MoveTo(hDC, x1, y1);
- 151 LineTo(hDC, x2, y2);
- 152 break;
- 153
- 154 case IDM_RECT :
- 155 Rectangle(hDC, x1, y1, x2, y2);
- 156 break;
- 157
- 158 case IDM_ELLIP :
- 159 Ellipse(hDC, x1, y1, x2, y2);
- 160 break;
- 161 }
- 162
- 163 ReleaseDC(hWnd, hDC);
- 164 break;
- 165 }
- 166 break;
- 167
- 168 case WM_SIZE :
- 169 GetClientRect(hWnd, &Rect);
- 170 CX = Rect.right - Rect.left;
- 171 CY = Rect.bottom - Rect.top;
- 172 break;
- 173
- 174 case WM_DESTROY :
- 175 PostQuitMessage(0);
- 176 break ;
- 177
- 178 default :
- 179 return (DefWindowProc(hWnd, message, wParam, lParam));
- 180 }
- 181 return (NULL);
- 182 }