PAINT_FI.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:13k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 #include <windows.h>
- 2 #include "paint.h"
- 3
- 4 BOOL FAR PASCAL OpenDlgProc(HWND, unsigned, WORD, LONG);
- 5 BOOL FAR PASCAL SaveAsDlgProc(HWND, unsigned, WORD, LONG);
- 6
- 7 FARPROC lpOpenDlgProc;
- 8 FARPROC lpSaveAsDlgProc;
- 9
- 10 int QuerySave(HWND hWnd);
- 11
- 12 OFSTRUCT ofs;
- 13 char szCurPathName[80] = "";
- 14 char szName[] = "My Paint";
- 15
- 16 /* Declared in paint.c */
- 17 extern HANDLE hInst;
- 18 extern HDC hMemDC;
- 19 extern int MemX, MemY;
- 20 extern BOOL CanUndo;
- 21
- 22 void NewFile(HWND hWnd, BOOL bFirst)
- 23 {
- 24 HDC hDC;
- 25 HMENU hMenu;
- 26 char str[100];
- 27
- 28 if (! bFirst)
- 29 {
- 30 if (QuerySave(hWnd) == IDCANCEL)
- 31 return ;
- 32 }
- 33
- 34 hDC = GetDC(hWnd);
- 35 hMenu = GetMenu(hWnd);
- 36 CancelBound(hDC, hMenu);
- 37 ReleaseDC(hWnd, hDC);
- 38
- 39 PatBlt(hMemDC, 0, 0, MemX, MemY, WHITENESS);
- 40
- 41 EnableMenuItem(hMenu, IDM_UNDO, MF_GRAYED);
- 42 CanUndo = FALSE;
- 43
- 44 szCurPathName[0] = 0;
- 45 lstrcpy(str, szName);
- 46 lstrcat(str, " -- (Untitled)");
- 47 SetWindowText (hWnd, str);
- 48 }
- 49
- 50
- 51 void ChangeTitle(HWND hWnd)
- 52 {
- 53 char str[100];
- 54
- 55 lstrcpy(szCurPathName, ofs.szPathName);
- 56 lstrcpy(str, szName);
- 57 lstrcat(str, " -- ");
- 58 lstrcat(str, szCurPathName);
- 59 SetWindowText (hWnd, str);
- 60 }
- 61
- 62
- 63 LPSTR lstrchr (LPSTR str, char ch)
- 64 {
- 65 while (*str)
- 66 if ( *str++ == ch )
- 67 return str;
- 68 return NULL;
- 69 }
- 70
- 71
- 72 BOOL FAR PASCAL OpenDlgProc (HWND hDlg, WORD message,
- 73 WORD wParam, LONG lParam)
- 74 {
- 75 static char szDefExt[5] = ".GPH";
- 76 static char szFileSpec[20] = "*.GPH";
- 77
- 78 char LastChar, szFileName[96];
- 79 WORD EditLen;
- 80
- 81 switch (message)
- 82 {
- 83 case WM_INITDIALOG:
- 84
- 85 SendDlgItemMessage (hDlg, DI_NAME, EM_LIMITTEXT, 80, 0);
- 86 DlgDirList (hDlg, szFileSpec, DI_FILES,
- 87 DI_PATH, FILEATTR);
- 88 DlgDirList (hDlg, szFileSpec, DI_DIRS, 0, DIRATTR);
- 89 SetDlgItemText (hDlg, DI_NAME, szFileSpec);
- 90 return TRUE;
- 91
- 92 case WM_COMMAND:
- 93
- 94 switch (wParam)
- 95 {
- 96 case DI_FILES:
- 97 switch ( HIWORD (lParam) )
- 98 {
- 99 case LBN_SELCHANGE:
- 100
- 101 DlgDirSelect (hDlg, szFileName, DI_FILES);
- 102 SetDlgItemText (hDlg, DI_NAME, szFileName);
- 103 return TRUE;
- 104
- 105 case LBN_DBLCLK:
- 106
- 107 DlgDirSelect (hDlg, szFileName, DI_FILES);
- 108 SetDlgItemText (hDlg, DI_NAME, szFileName);
- 109 SendMessage (hDlg, WM_COMMAND, DI_OPEN, 0);
- 110 return TRUE;
- 111 }
- 112 break;
- 113
- 114 case DI_DIRS:
- 115 switch ( HIWORD (lParam) )
- 116 {
- 117 case LBN_SELCHANGE:
- 118
- 119 DlgDirSelect (hDlg, szFileName, DI_DIRS);
- 120 lstrcat (szFileName, szFileSpec);
- 121 SetDlgItemText (hDlg, DI_NAME, szFileName);
- 122 return TRUE;
- 123
- 124 case LBN_DBLCLK:
- 125
- 126 DlgDirSelect (hDlg, szFileName, DI_DIRS);
- 127 lstrcat (szFileName, szFileSpec);
- 128 DlgDirList (hDlg, szFileName, DI_FILES,
- 129 DI_PATH, FILEATTR);
- 130 DlgDirList (hDlg, szFileName, DI_DIRS,
- 131 0, DIRATTR);
- 132 SetDlgItemText (hDlg, DI_NAME, szFileSpec);
- 133 return TRUE;
- 134 }
- 135 break;
- 136
- 137 case IDOK :
- 138 case DI_OPEN:
- 139
- 140 GetDlgItemText (hDlg, DI_NAME, szFileName, 80);
- 141
- 142 EditLen = lstrlen (szFileName);
- 143 LastChar = szFileName [ EditLen - 1 ];
- 144
- 145 if ( LastChar == '\' || LastChar == ':' )
- 146 lstrcat (szFileName, szFileSpec);
- 147
- 148 if ( lstrchr (szFileName, '*')
- 149 || lstrchr (szFileName, '?') )
- 150 {
- 151 if ( DlgDirList (hDlg, szFileName, DI_DIRS,
- 152 DI_PATH, DIRATTR) )
- 153 {
- 154 DlgDirList (hDlg, szFileName, DI_FILES,
- 155 0, FILEATTR);
- 156 lstrcpy (szFileSpec, szFileName);
- 157 SetDlgItemText (hDlg, DI_NAME, szFileSpec);
- 158 }
- 159 else MessageBeep (0);
- 160
- 161 return TRUE;
- 162 }
- 163
- 164 lstrcat (lstrcat (szFileName, "\"), szFileSpec);
- 165 if ( DlgDirList (hDlg, szFileName, DI_DIRS,
- 166 DI_PATH, DIRATTR) )
- 167 {
- 168 DlgDirList (hDlg, szFileName, DI_FILES,
- 169 0, FILEATTR);
- 170 SetDlgItemText (hDlg, DI_NAME, szFileSpec);
- 171 return TRUE;
- 172 }
- 173
- 174 szFileName [EditLen] = ' ';
- 175 if ( -1 == OpenFile ( szFileName, &ofs,
- 176 OF_EXIST | OF_READ ) )
- 177 {
- 178 lstrcat (szFileName, szDefExt);
- 179 if ( -1 == OpenFile ( szFileName, &ofs,
- 180 OF_EXIST | OF_READ ) )
- 181 {
- 182 MessageBeep (0);
- 183 return TRUE;
- 184 }
- 185 }
- 186 EndDialog (hDlg, TRUE);
- 187 return TRUE;
- 188
- 189 case DI_CANCEL:
- 190 EndDialog (hDlg, FALSE);
- 191 return TRUE;
- 192 }
- 193 break;
- 194 }
- 195 return FALSE;
- 196 }
- 197
- 198
- 199 void ReadFileData(HWND hWnd)
- 200 {
- 201 // Read graph data from file 'szCurPathName',
- 202 // and copy to hMemDC;
- 203 }
- 204
- 205
- 206 void FileOpen(HWND hWnd)
- 207 {
- 208 char str[100];
- 209
- 210 if (QuerySave(hWnd) == IDCANCEL)
- 211 return ;
- 212
- 213 lpOpenDlgProc = MakeProcInstance((FARPROC) OpenDlgProc,
- 214 hInst);
- 215 if (DialogBox (hInst, "OPENDLG",
- 216 hWnd, lpOpenDlgProc) )
- 217 {
- 218 ChangeTitle (hWnd);
- 219 ReadFileData (hWnd);
- 220 }
- 221 FreeProcInstance(lpOpenDlgProc);
- 222 }
- 223
- 224
- 225 BOOL FAR PASCAL SaveAsDlgProc (HWND hDlg, WORD message,
- 226 WORD wParam, LONG lParam)
- 227 {
- 228 static char szDefExt[5] = ".GPH";
- 229 static char szFileSpec[20] = "*.GPH";
- 230
- 231 char LastChar, szFileName[96];
- 232 WORD EditLen;
- 233
- 234 switch (message)
- 235 {
- 236 case WM_INITDIALOG:
- 237
- 238 SendDlgItemMessage (hDlg, DI_NAME2, EM_LIMITTEXT, 80, 0);
- 239 DlgDirList (hDlg, szFileSpec, DI_FILES2,
- 240 DI_PATH2, FILEATTR);
- 241 DlgDirList (hDlg, szFileSpec, DI_DIRS2, 0, DIRATTR);
- 242 SetDlgItemText (hDlg, DI_NAME2, szFileSpec);
- 243 return TRUE;
- 244
- 245 case WM_COMMAND:
- 246
- 247 switch (wParam)
- 248 {
- 249 case DI_FILES2:
- 250 switch ( HIWORD (lParam) )
- 251 {
- 252 case LBN_SELCHANGE:
- 253
- 254 DlgDirSelect (hDlg, szFileName, DI_FILES2);
- 255 SetDlgItemText (hDlg, DI_NAME2, szFileName);
- 256 return TRUE;
- 257
- 258 case LBN_DBLCLK:
- 259
- 260 DlgDirSelect (hDlg, szFileName, DI_FILES2);
- 261 SetDlgItemText (hDlg, DI_NAME2, szFileName);
- 262 SendMessage (hDlg, WM_COMMAND, DI_SAVE, 0);
- 263 return TRUE;
- 264 }
- 265 break;
- 266
- 267 case DI_DIRS2:
- 268 switch ( HIWORD (lParam) )
- 269 {
- 270 case LBN_SELCHANGE:
- 271
- 272 DlgDirSelect (hDlg, szFileName, DI_DIRS2);
- 273 lstrcat (szFileName, szFileSpec);
- 274 SetDlgItemText (hDlg, DI_NAME2, szFileName);
- 275 return TRUE;
- 276
- 277 case LBN_DBLCLK:
- 278
- 279 DlgDirSelect (hDlg, szFileName, DI_DIRS2);
- 280 lstrcat (szFileName, szFileSpec);
- 281 DlgDirList (hDlg, szFileName, DI_FILES2,
- 282 DI_PATH2, FILEATTR);
- 283 DlgDirList (hDlg, szFileName, DI_DIRS2,
- 284 0, DIRATTR);
- 285 SetDlgItemText (hDlg, DI_NAME2, szFileSpec);
- 286 return TRUE;
- 287 }
- 288 break;
- 289
- 290 case IDOK :
- 291 case DI_SAVE:
- 292
- 293 GetDlgItemText (hDlg, DI_NAME2, szFileName, 80);
- 294
- 295 EditLen = lstrlen (szFileName);
- 296 LastChar = szFileName [ EditLen - 1 ];
- 297
- 298 if ( LastChar == '\' || LastChar == ':' )
- 299 lstrcat (szFileName, szFileSpec);
- 300
- 301 if ( lstrchr (szFileName, '*')
- 302 || lstrchr (szFileName, '?') )
- 303 {
- 304 if ( DlgDirList (hDlg, szFileName, DI_DIRS2,
- 305 DI_PATH2, DIRATTR) )
- 306 {
- 307 DlgDirList (hDlg, szFileName, DI_FILES2,
- 308 0, FILEATTR);
- 309 lstrcpy (szFileSpec, szFileName);
- 310 SetDlgItemText (hDlg, DI_NAME2, szFileSpec);
- 311 }
- 312 else MessageBeep (0);
- 313
- 314 return TRUE;
- 315 }
- 316
- 317 lstrcat (lstrcat (szFileName, "\"), szFileSpec);
- 318 if ( DlgDirList (hDlg, szFileName, DI_DIRS2,
- 319 DI_PATH2, DIRATTR) )
- 320 {
- 321 DlgDirList (hDlg, szFileName, DI_FILES2,
- 322 0, FILEATTR);
- 323 SetDlgItemText (hDlg, DI_NAME2, szFileSpec);
- 324 return TRUE;
- 325 }
- 326
- 327 szFileName [EditLen] = ' ';
- 328 if ( -1 == OpenFile ( szFileName, &ofs,
- 329 OF_EXIST | OF_READ ) )
- 330 EndDialog (hDlg, 1);
- 331 else
- 332 EndDialog (hDlg, 2);
- 333 return TRUE;
- 334
- 335 case DI_CANCEL:
- 336 EndDialog (hDlg, FALSE);
- 337 return TRUE;
- 338 }
- 339 break;
- 340 }
- 341 return FALSE;
- 342 }
- 343
- 344
- 345 void WriteFileData(HWND hWnd)
- 346 {
- 347 // Copy the graph data in hMemDC to file 'szCurPathName'
- 348 }
- 349
- 350
- 351 void FileSaveAs(HWND hWnd)
- 352 {
- 353 int Status;
- 354
- 355 lpSaveAsDlgProc = MakeProcInstance((FARPROC) SaveAsDlgProc,
- 356 hInst);
- 357 Status = DialogBox (hInst, "SAVEASDLG",
- 358 hWnd, lpSaveAsDlgProc);
- 359
- 360 if (Status == 1)
- 361 {
- 362 ChangeTitle(hWnd);
- 363 WriteFileData(hWnd);
- 364 }
- 365 else if (Status == 2)
- 366 {
- 367 if (IDYES==MessageBox(hWnd, "File already exist !n"
- 368 " Overwrite it ?",
- 369 "Save File",
- 370 MB_YESNO | MB_ICONQUESTION))
- 371 {
- 372 ChangeTitle(hWnd);
- 373 WriteFileData(hWnd);
- 374 }
- 375 }
- 376 FreeProcInstance(lpSaveAsDlgProc);
- 377 }
- 378
- 379
- 380 void FileSave(HWND hWnd)
- 381 {
- 382 if (szCurPathName[0] == 0)
- 383 FileSaveAs(hWnd);
- 384 else
- 385 WriteFileData(hWnd);
- 386 }
- 387
- 388
- 389 int QuerySave(HWND hWnd)
- 390 {
- 391 int status;
- 392
- 393 status = MessageBox(hWnd, "Save old file ?",
- 394 "Save File",
- 395 MB_YESNOCANCEL | MB_ICONQUESTION);
- 396
- 397 if (status == IDYES)
- 398 FileSave(hWnd);
- 399
- 400 return (status);
- 401 }