PAINT_FN.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:15k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 #include "paint.h"
- 2 #include <windows.h>
- 3
- 4 int FAR PASCAL EnumFaces(LPLOGFONT, LPTEXTMETRIC,
- 5 short, LPSTR);
- 6 int FAR PASCAL EnumSizes(LPLOGFONT, LPTEXTMETRIC,
- 7 short, LPSTR);
- 8 void MakeFontMenu(HWND);
- 9 void MakeSizeMenu(HWND);
- 10 void FillStrokeSize();
- 11 void ChangeFont(HWND);
- 12
- 13 struct {
- 14 int FaceNum;
- 15 char FaceName[MAXFACES][LF_FACESIZE];
- 16 BOOL bRaster[MAXFACES];
- 17 } FontFace;
- 18
- 19 struct {
- 20 int SizeNum;
- 21 LOGFONT lf[MAXSIZES];
- 22 TEXTMETRIC tm[MAXSIZES];
- 23 } FontSize;
- 24
- 25 extern HANDLE hInst;
- 26
- 27 extern HDC hMemDC;
- 28 extern int OX, OY;
- 29 extern int CX, CY;
- 30
- 31 extern int nLogPixSx;
- 32 extern int nLogPixSy;
- 33
- 34 extern BOOL CanUndo;
- 35
- 36 int FaceID;
- 37 int SizeID;
- 38 BOOL bBold, bItalic;
- 39 BOOL bUnderLine, bStrikeOut;
- 40 BOOL bNormal;
- 41
- 42 HBITMAP hBoldBM, hItalicBM;
- 43 HBITMAP hUnderlineBM, hStrikeOutBM;
- 44
- 45 HFONT hCurFont;
- 46 BOOL bFontExist = FALSE;
- 47
- 48 BOOL bTextWorking = FALSE;
- 49 int CharX, CharY;
- 50 int CharPosX, CharPosY;
- 51 BOOL bCaret = FALSE;
- 52
- 53 char Buffer[160];
- 54 POINT Start;
- 55 int nChar;
- 56
- 57 TEXTMETRIC CurTM;
- 58
- 59 void ChooseFontMenu(HWND hWnd, WORD wParam)
- 60 {
- 61 HMENU hMenu;
- 62
- 63 hMenu = GetMenu(hWnd);
- 64
- 65 if (wParam>=IDM_FONT && wParam<IDM_FONT+MAXFACES)
- 66 {
- 67 if (wParam != FaceID)
- 68 {
- 69 CheckMenuItem(hMenu, FaceID, MF_UNCHECKED);
- 70 FaceID = wParam;
- 71 CheckMenuItem(hMenu, FaceID, MF_CHECKED);
- 72
- 73 MakeSizeMenu(hWnd);
- 74 }
- 75 return;
- 76 }
- 77 else
- 78 if (wParam>=IDM_SIZE && wParam<IDM_SIZE+MAXSIZES)
- 79 {
- 80 if (wParam != SizeID)
- 81 {
- 82 CheckMenuItem(hMenu, SizeID, MF_UNCHECKED);
- 83 SizeID = wParam;
- 84 CheckMenuItem(hMenu, SizeID, MF_CHECKED);
- 85 }
- 86 }
- 87 else
- 88 switch (wParam)
- 89 {
- 90 case IDM_BOLD :
- 91 if (bNormal)
- 92 {
- 93 bNormal = FALSE;
- 94 CheckMenuItem(hMenu, IDM_NORM,
- 95 MF_UNCHECKED);
- 96 }
- 97 if (bBold)
- 98 CheckMenuItem(hMenu, wParam,
- 99 MF_UNCHECKED);
- 100 else
- 101 CheckMenuItem(hMenu, wParam,
- 102 MF_CHECKED);
- 103
- 104 bBold = ! bBold;
- 105 break;
- 106
- 107 case IDM_ITALIC :
- 108 if (bNormal)
- 109 {
- 110 bNormal = FALSE;
- 111 CheckMenuItem(hMenu, IDM_NORM,
- 112 MF_UNCHECKED);
- 113 }
- 114 if (bItalic)
- 115 CheckMenuItem(hMenu, wParam,
- 116 MF_UNCHECKED);
- 117 else
- 118 CheckMenuItem(hMenu, wParam,
- 119 MF_CHECKED);
- 120
- 121 bItalic = ! bItalic;
- 122 break;
- 123
- 124 case IDM_UNDERLINE :
- 125 if (bNormal)
- 126 {
- 127 bNormal = FALSE;
- 128 CheckMenuItem(hMenu, IDM_NORM,
- 129 MF_UNCHECKED);
- 130 }
- 131 if (bUnderLine)
- 132 CheckMenuItem(hMenu, wParam,
- 133 MF_UNCHECKED);
- 134 else
- 135 CheckMenuItem(hMenu, wParam,
- 136 MF_CHECKED);
- 137
- 138 bUnderLine = ! bUnderLine;
- 139 break;
- 140
- 141 case IDM_STRIKEOUT :
- 142 if (bNormal)
- 143 {
- 144 bNormal = FALSE;
- 145 CheckMenuItem(hMenu, IDM_NORM,
- 146 MF_UNCHECKED);
- 147 }
- 148 if (bStrikeOut)
- 149 CheckMenuItem(hMenu, wParam,
- 150 MF_UNCHECKED);
- 151 else
- 152 CheckMenuItem(hMenu, wParam,
- 153 MF_CHECKED);
- 154
- 155 bStrikeOut = ! bStrikeOut;
- 156 break;
- 157
- 158 case IDM_NORM :
- 159 if (bNormal)
- 160 break;
- 161
- 162 bNormal = TRUE;
- 163 CheckMenuItem(hMenu, IDM_NORM,
- 164 MF_CHECKED);
- 165
- 166 bBold = bItalic = FALSE;
- 167 bUnderLine = bStrikeOut = FALSE;
- 168 CheckMenuItem(hMenu, IDM_BOLD,
- 169 MF_UNCHECKED);
- 170 CheckMenuItem(hMenu, IDM_ITALIC,
- 171 MF_UNCHECKED);
- 172 CheckMenuItem(hMenu, IDM_UNDERLINE,
- 173 MF_UNCHECKED);
- 174 CheckMenuItem(hMenu, IDM_STRIKEOUT,
- 175 MF_UNCHECKED);
- 176
- 177 break;
- 178 }
- 179
- 180 if (bTextWorking)
- 181 ChangeFont(hWnd);
- 182 }
- 183
- 184
- 185
- 186 int FAR PASCAL EnumFaces(LPLOGFONT lpLF, LPTEXTMETRIC lpTM,
- 187 short nType, LPSTR lpData)
- 188 {
- 189 lstrcpy(FontFace.FaceName[FontFace.FaceNum],
- 190 lpLF->lfFaceName);
- 191 FontFace.bRaster[FontFace.FaceNum] = nType & 1;
- 192
- 193 FontFace.FaceNum++;
- 194
- 195 if (FontFace.FaceNum >= MAXFACES)
- 196 return (0);
- 197 return (1);
- 198 }
- 199
- 200
- 201 int FAR PASCAL EnumSizes(LPLOGFONT lpLF, LPTEXTMETRIC lpTM,
- 202 short nType, LPSTR lpData)
- 203 {
- 204 if (nLogPixSx != lpTM->tmDigitizedAspectX ||
- 205 nLogPixSy != lpTM->tmDigitizedAspectY)
- 206 return (1);
- 207
- 208 FontSize.lf[FontSize.SizeNum] = *lpLF;
- 209 FontSize.tm[FontSize.SizeNum] = *lpTM;
- 210
- 211 FontSize.SizeNum++;
- 212
- 213 if (FontSize.SizeNum >= MAXSIZES)
- 214 return (0);
- 215 return (1);
- 216 }
- 217
- 218
- 219 void MakeFontMenu(HWND hWnd)
- 220 {
- 221 int i;
- 222 HDC hDC;
- 223 HMENU hMenu, hPopMenu;
- 224 FARPROC lpEnumFaces;
- 225
- 226 FontFace.FaceNum = 0;
- 227 lpEnumFaces = MakeProcInstance(EnumFaces, hInst);
- 228
- 229 hDC = GetDC(hWnd);
- 230 EnumFonts(hDC, NULL, lpEnumFaces, NULL);
- 231 ReleaseDC(hWnd, hDC);
- 232
- 233 hMenu = GetMenu(hWnd);
- 234 hPopMenu = GetSubMenu(hMenu, FONTMENU);
- 235
- 236 DeleteMenu(hPopMenu, 0, MF_BYPOSITION);
- 237
- 238 for (i=0; i<FontFace.FaceNum; i++)
- 239 AppendMenu(hPopMenu, 0, IDM_FONT+i,
- 240 FontFace.FaceName[i]);
- 241
- 242 FaceID = -1;
- 243 SendMessage(hWnd, WM_COMMAND, IDM_FONT, 0L);
- 244
- 245 MakeSizeMenu(hWnd);
- 246 }
- 247
- 248
- 249 void MakeSizeMenu(HWND hWnd)
- 250 {
- 251 int i;
- 252 HDC hDC;
- 253 HMENU hMenu, hPopMenu;
- 254 FARPROC lpEnumSizes;
- 255 char szStr[10];
- 256 static LOGFONT DefLF;
- 257
- 258 FontSize.SizeNum = 0;
- 259 lpEnumSizes = MakeProcInstance(EnumSizes, hInst);
- 260
- 261 if (FontFace.bRaster[FaceID-IDM_FONT])
- 262 {
- 263 hDC = GetDC(hWnd);
- 264 EnumFonts(hDC, FontFace.FaceName[FaceID-IDM_FONT],
- 265 lpEnumSizes, NULL);
- 266 ReleaseDC(hWnd, hDC);
- 267 }
- 268 else
- 269 FillStrokeSize();
- 270
- 271 hMenu = GetMenu(hWnd);
- 272 hPopMenu = GetSubMenu(hMenu, SIZEMENU);
- 273
- 274 while (GetMenuItemCount(hPopMenu) > 0)
- 275 DeleteMenu(hPopMenu, 0, MF_BYPOSITION);
- 276
- 277 if (FontSize.SizeNum)
- 278 for (i=0; i<FontSize.SizeNum; i++)
- 279 {
- 280 sprintf(szStr, "%2d",
- 281 ((FontSize.tm[i].tmHeight -
- 282 FontSize.tm[i].tmInternalLeading)*72
- 283 +nLogPixSy/2) / nLogPixSy) ;
- 284 AppendMenu(hPopMenu, 0, IDM_SIZE+i, szStr);
- 285 }
- 286 else
- 287 {
- 288 FontSize.lf[0] = DefLF;
- 289 lstrcpy(FontSize.lf[0].lfFaceName,
- 290 FontFace.FaceName[FaceID-IDM_FONT]);
- 291 AppendMenu(hPopMenu, 0, IDM_SIZE, "Default");
- 292 }
- 293
- 294 SizeID = -1;
- 295 SendMessage(hWnd, WM_COMMAND, IDM_SIZE, 0L);
- 296 }
- 297
- 298
- 299 void FillStrokeSize()
- 300 {
- 301 int i;
- 302 static LOGFONT DefLF;
- 303 int StrokeSize[] = { 8, 10, 12, 14, 16,
- 304 18, 20, 22, 24, 26,
- 305 28, 30, 32, 34, 36 };
- 306
- 307 FontSize.SizeNum = sizeof(StrokeSize)/sizeof(int);
- 308
- 309 for (i=0; i<FontSize.SizeNum; i++)
- 310 {
- 311 FontSize.lf[i] = DefLF;
- 312
- 313 lstrcpy(FontSize.lf[i].lfFaceName,
- 314 FontFace.FaceName[FaceID-IDM_FONT]);
- 315 FontSize.lf[i].lfCharSet = OEM_CHARSET;
- 316 FontSize.lf[i].lfHeight = StrokeSize[i]*nLogPixSy/72;
- 317
- 318 FontSize.tm[i].tmHeight = StrokeSize[i]*nLogPixSy/72;
- 319 FontSize.tm[i].tmInternalLeading = 0;
- 320 }
- 321 }
- 322
- 323
- 324 void MakeStyleMenu(HMENU hMenu)
- 325 {
- 326 HMENU hStyleMenu;
- 327
- 328 hBoldBM = LoadBitmap(hInst, "Bold");
- 329 hItalicBM = LoadBitmap(hInst, "Italic");
- 330 hUnderlineBM = LoadBitmap(hInst, "Underline");
- 331 hStrikeOutBM = LoadBitmap(hInst, "StrikeOut");
- 332
- 333 hStyleMenu = GetSubMenu(hMenu, STYLEMENU);
- 334 AppendMenu(hStyleMenu, MF_BITMAP, IDM_BOLD,
- 335 (LPSTR)(LONG)hBoldBM);
- 336 AppendMenu(hStyleMenu, MF_BITMAP, IDM_ITALIC,
- 337 (LPSTR)(LONG)hItalicBM);
- 338 AppendMenu(hStyleMenu, MF_BITMAP, IDM_UNDERLINE,
- 339 (LPSTR)(LONG)hUnderlineBM);
- 340 AppendMenu(hStyleMenu, MF_BITMAP, IDM_STRIKEOUT,
- 341 (LPSTR)(LONG)hStrikeOutBM);
- 342 }
- 343
- 344
- 345 void DeleteStyleMenu()
- 346 {
- 347 DeleteObject(hBoldBM);
- 348 DeleteObject(hItalicBM);
- 349 DeleteObject(hUnderlineBM);
- 350 DeleteObject(hStrikeOutBM);
- 351 }
- 352
- 353
- 354 HFONT CreateCurFont()
- 355 {
- 356 LOGFONT lf;
- 357 HFONT hFont;
- 358
- 359 lf = FontSize.lf[SizeID-IDM_SIZE];
- 360
- 361 lf.lfWeight = bBold ? 700 : 400;
- 362 lf.lfItalic = bItalic;
- 363 lf.lfUnderline = bUnderLine;
- 364 lf.lfStrikeOut = bStrikeOut;
- 365
- 366 hFont = CreateFontIndirect(&lf);
- 367 return (hFont);
- 368 }
- 369
- 370
- 371 void BeginWrite(HWND hWnd, int X, int Y)
- 372 {
- 373 HDC hDC;
- 374
- 375 bTextWorking = TRUE;
- 376 CharPosX = X;
- 377 CharPosY = Y;
- 378
- 379 if (bFontExist)
- 380 DeleteObject(hCurFont);
- 381
- 382 hCurFont = CreateCurFont();
- 383 bFontExist = TRUE;
- 384
- 385 hDC = GetDC(hWnd);
- 386 SelectObject(hDC, hCurFont);
- 387 GetTextMetrics(hDC, &CurTM);
- 388
- 389 CharX = CurTM.tmAveCharWidth;
- 390 CharY = CurTM.tmHeight;
- 391
- 392 if (bCaret)
- 393 {
- 394 HideCaret(hWnd);
- 395 DestroyCaret();
- 396 }
- 397
- 398 BackUpGraph(hDC, GetMenu(hWnd), OX, OY, CX, CY, 0, 0);
- 399
- 400 CreateCaret(hWnd, NULL, 1, CharY);
- 401 SetCaretPos(CharPosX, CharPosY);
- 402 ShowCaret(hWnd);
- 403 bCaret = TRUE;
- 404
- 405 nChar = 0;
- 406 Start.x = CharPosX;
- 407 Start.y = CharPosY;
- 408
- 409 ReleaseDC(hWnd, hDC);
- 410 }
- 411
- 412
- 413 void EndWrite(HWND hWnd)
- 414 {
- 415 bTextWorking = FALSE;
- 416
- 417 if (bFontExist)
- 418 {
- 419 DeleteObject(hCurFont);
- 420 bFontExist = FALSE;
- 421 }
- 422
- 423 if (bCaret)
- 424 {
- 425 HideCaret(hWnd);
- 426 DestroyCaret();
- 427 bCaret = FALSE;
- 428 }
- 429 }
- 430
- 431
- 432 void ChangeFont(HWND hWnd)
- 433 {
- 434 HDC hDC;
- 435 DWORD dwLen;
- 436 int StrW, StrH;
- 437
- 438 HideCaret(hWnd);
- 439 DestroyCaret();
- 440
- 441 hDC = GetDC(hWnd);
- 442
- 443 SelectObject(hDC, hCurFont);
- 444 StrW = LOWORD(GetTextExtent(hDC, Buffer, nChar));
- 445 StrH = CurTM.tmHeight;
- 446
- 447 DeleteObject(hCurFont);
- 448
- 449 hCurFont = CreateCurFont();
- 450 SelectObject(hDC, hCurFont);
- 451 GetTextMetrics(hDC, &CurTM);
- 452
- 453 if (nChar > 0)
- 454 {
- 455 BitBlt(hDC, Start.x-20, Start.y, StrW+21, StrH,
- 456 hMemDC, OX+Start.x-20, OY+Start.y,
- 457 SRCCOPY);
- 458
- 459 TextOut(hDC, Start.x, Start.y, Buffer, nChar);
- 460 CharPosX = Start.x - CurTM.tmOverhang +
- 461 LOWORD(GetTextExtent(hDC, Buffer, nChar));
- 462 }
- 463
- 464 CharX = CurTM.tmAveCharWidth;
- 465 CharY = CurTM.tmHeight;
- 466
- 467 CreateCaret(hWnd, NULL, 1, CharY);
- 468 SetCaretPos(CharPosX, CharPosY);
- 469 ShowCaret(hWnd);
- 470
- 471 ReleaseDC(hWnd, hDC);
- 472 }
- 473
- 474 void ProcessChar(HWND hWnd, WORD ch)
- 475 {
- 476 HDC hDC;
- 477 int CharWidth;
- 478
- 479 hDC = GetDC(hWnd);
- 480
- 481 switch (ch)
- 482 {
- 483 case 'r' : Start.y += CurTM.tmHeight;
- 484 CharPosX = Start.x;
- 485 CharPosY = Start.y;
- 486 SetCaretPos(CharPosX, CharPosY);
- 487 nChar = 0;
- 488 break;
- 489
- 490 case 'b' : if (nChar == 0) break;
- 491
- 492 HideCaret(hWnd);
- 493 SelectObject(hDC, hCurFont);
- 494
- 495 CharWidth = LOWORD(
- 496 GetTextExtent(hDC, Buffer+nChar-1, 1));
- 497 CharPosX -= (CharWidth-CurTM.tmOverhang);
- 498 BitBlt(hDC,
- 499 CharPosX-20, CharPosY,
- 500 CharWidth+21, CurTM.tmHeight,
- 501 hMemDC,
- 502 OX+CharPosX-20, OY+CharPosY,
- 503 SRCCOPY);
- 504
- 505 nChar--;
- 506 if (nChar > 0)
- 507 TextOut(hDC,
- 508 Start.x, CharPosY,
- 509 Buffer, nChar);
- 510
- 511 SetCaretPos(CharPosX, CharPosY);
- 512 ShowCaret(hWnd);
- 513 break;
- 514
- 515 default : Buffer[nChar] = ch;
- 516 HideCaret(hWnd);
- 517
- 518 SelectObject(hDC, hCurFont);
- 519 TextOut(hDC, CharPosX, CharPosY,
- 520 Buffer+nChar, 1);
- 521
- 522 CharWidth = LOWORD(
- 523 GetTextExtent(hDC, Buffer+nChar, 1))
- 524 - CurTM.tmOverhang;
- 525 CharPosX += CharWidth;
- 526 nChar ++;
- 527
- 528 SetCaretPos(CharPosX, CharPosY);
- 529 ShowCaret(hWnd);
- 530
- 531 if (! CanUndo)
- 532 {
- 533 EnableMenuItem(GetMenu(hWnd),
- 534 IDM_UNDO, MF_ENABLED);
- 535 CanUndo = TRUE;
- 536 }
- 537 break;
- 538 }
- 539
- 540 ReleaseDC(hWnd, hDC);
- 541 }