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