DEMO7_1.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:15k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo7_1 --- Enum Fonts */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo7_1.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11 int FAR PASCAL EnumFaces(LPLOGFONT, LPTEXTMETRIC,
- 12 short, LPSTR);
- 13 int FAR PASCAL EnumSizes(LPLOGFONT, LPTEXTMETRIC,
- 14 short, LPSTR);
- 15 void MakeFontMenu(HWND);
- 16 void MakeSizeMenu(HWND);
- 17 void FillStrokeSize();
- 18 void MakeStyleMenu(HMENU);
- 19
- 20 struct {
- 21 int FaceNum;
- 22 char FaceName[MAXFACES][LF_FACESIZE];
- 23 BOOL bRaster[MAXFACES];
- 24 } FontFace;
- 25
- 26 struct {
- 27 int SizeNum;
- 28 LOGFONT lf[MAXSIZES];
- 29 TEXTMETRIC tm[MAXSIZES];
- 30 } FontSize;
- 31
- 32 HANDLE hInst;
- 33
- 34 HBITMAP hBoldBM, hItalicBM;
- 35 HBITMAP hUnderlineBM, hStrikeOutBM;
- 36
- 37 int FaceID;
- 38 int SizeID;
- 39 BOOL bBold, bItalic;
- 40 BOOL bUnderLine, bStrikeOut;
- 41 BOOL bNormal;
- 42
- 43 int nLogPixSx;
- 44 int nLogPixSy;
- 45
- 46 /****************************************************************/
- 47 /* WinMain() */
- 48 /****************************************************************/
- 49
- 50 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 51 LPSTR lpszCmdLine, int nCmdShow)
- 52 {
- 53 WNDCLASS wclass;
- 54 MSG msg;
- 55 HWND hWnd;
- 56 char szName[] = "Demo7_1";
- 57
- 58 if (!hPrevInstance)
- 59 {
- 60 wclass.style = NULL;
- 61 wclass.lpfnWndProc = MainWndProc;
- 62 wclass.cbClsExtra = 0;
- 63 wclass.cbWndExtra = 0;
- 64 wclass.hInstance = hInstance;
- 65 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 66 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 67 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 68 wclass.lpszMenuName = szName;
- 69 wclass.lpszClassName = szName;
- 70
- 71 if (!RegisterClass (&wclass))
- 72 return (FALSE);
- 73 }
- 74
- 75 hWnd = CreateWindow(
- 76 szName,
- 77 "Enum Fonts",
- 78 WS_OVERLAPPEDWINDOW,
- 79 CW_USEDEFAULT,
- 80 CW_USEDEFAULT,
- 81 CW_USEDEFAULT,
- 82 CW_USEDEFAULT,
- 83 NULL,
- 84 NULL,
- 85 hInstance,
- 86 NULL );
- 87
- 88 if (!hWnd)
- 89 return (FALSE);
- 90
- 91 ShowWindow(hWnd, nCmdShow);
- 92 UpdateWindow(hWnd);
- 93
- 94 while (GetMessage(&msg, NULL, NULL,NULL))
- 95 {
- 96 TranslateMessage(&msg);
- 97 DispatchMessage(&msg);
- 98 }
- 99 return (msg.wParam);
- 100 }
- 101
- 102
- 103
- 104 /****************************************************************/
- 105 /* MainWndProc() */
- 106 /****************************************************************/
- 107
- 108 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 109 WORD wParam, LONG lParam)
- 110 {
- 111 PAINTSTRUCT ps;
- 112 HDC hDC;
- 113 HMENU hMenu;
- 114 HFONT hFont, hPreFont;
- 115 TEXTMETRIC tm;
- 116 LOGFONT lf;
- 117 char szStr1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- 118 char szStr2[] = "abcdefghijklmnopqrstuvwxyz";
- 119
- 120 switch (message)
- 121 {
- 122 case WM_CREATE :
- 123 hInst = ((LPCREATESTRUCT)lParam)->hInstance;
- 124
- 125 hMenu = GetMenu(hWnd);
- 126 MakeStyleMenu(hMenu);
- 127 bNormal = FALSE;
- 128 SendMessage(hWnd, WM_COMMAND, IDM_NORM, 0L);
- 129
- 130 hDC = GetDC(hWnd);
- 131 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
- 132 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
- 133 ReleaseDC(hWnd, hDC);
- 134
- 135 MakeFontMenu(hWnd);
- 136 return (0);
- 137
- 138 case WM_COMMAND :
- 139 hMenu = GetMenu(hWnd);
- 140
- 141 if (wParam>=IDM_FONT &&
- 142 wParam<IDM_FONT+MAXFACES)
- 143 {
- 144 if (wParam != FaceID)
- 145 {
- 146 CheckMenuItem(hMenu, FaceID,
- 147 MF_UNCHECKED);
- 148 FaceID = wParam;
- 149 CheckMenuItem(hMenu, FaceID,
- 150 MF_CHECKED);
- 151
- 152 MakeSizeMenu(hWnd);
- 153 }
- 154 return (0);
- 155 }
- 156 else
- 157 if (wParam>=IDM_SIZE &&
- 158 wParam<IDM_SIZE+MAXSIZES)
- 159 {
- 160 if (wParam != SizeID)
- 161 {
- 162 CheckMenuItem(hMenu, SizeID,
- 163 MF_UNCHECKED);
- 164 SizeID = wParam;
- 165 CheckMenuItem(hMenu, SizeID,
- 166 MF_CHECKED);
- 167 }
- 168 }
- 169 else
- 170 switch (wParam)
- 171 {
- 172 case IDM_BOLD :
- 173 if (bNormal)
- 174 {
- 175 bNormal = FALSE;
- 176 CheckMenuItem(hMenu, IDM_NORM,
- 177 MF_UNCHECKED);
- 178 }
- 179 if (bBold)
- 180 CheckMenuItem(hMenu, wParam,
- 181 MF_UNCHECKED);
- 182 else
- 183 CheckMenuItem(hMenu, wParam,
- 184 MF_CHECKED);
- 185
- 186 bBold = ! bBold;
- 187 break;
- 188
- 189 case IDM_ITALIC :
- 190 if (bNormal)
- 191 {
- 192 bNormal = FALSE;
- 193 CheckMenuItem(hMenu, IDM_NORM,
- 194 MF_UNCHECKED);
- 195 }
- 196 if (bItalic)
- 197 CheckMenuItem(hMenu, wParam,
- 198 MF_UNCHECKED);
- 199 else
- 200 CheckMenuItem(hMenu, wParam,
- 201 MF_CHECKED);
- 202
- 203 bItalic = ! bItalic;
- 204 break;
- 205
- 206 case IDM_UNDERLINE :
- 207 if (bNormal)
- 208 {
- 209 bNormal = FALSE;
- 210 CheckMenuItem(hMenu, IDM_NORM,
- 211 MF_UNCHECKED);
- 212 }
- 213 if (bUnderLine)
- 214 CheckMenuItem(hMenu, wParam,
- 215 MF_UNCHECKED);
- 216 else
- 217 CheckMenuItem(hMenu, wParam,
- 218 MF_CHECKED);
- 219
- 220 bUnderLine = ! bUnderLine;
- 221 break;
- 222
- 223 case IDM_STRIKEOUT :
- 224 if (bNormal)
- 225 {
- 226 bNormal = FALSE;
- 227 CheckMenuItem(hMenu, IDM_NORM,
- 228 MF_UNCHECKED);
- 229 }
- 230 if (bStrikeOut)
- 231 CheckMenuItem(hMenu, wParam,
- 232 MF_UNCHECKED);
- 233 else
- 234 CheckMenuItem(hMenu, wParam,
- 235 MF_CHECKED);
- 236
- 237 bStrikeOut = ! bStrikeOut;
- 238 break;
- 239
- 240 case IDM_NORM :
- 241 if (bNormal)
- 242 break;
- 243
- 244 bNormal = TRUE;
- 245 CheckMenuItem(hMenu, IDM_NORM,
- 246 MF_CHECKED);
- 247
- 248 bBold = bItalic = FALSE;
- 249 bUnderLine = bStrikeOut = FALSE;
- 250 CheckMenuItem(hMenu, IDM_BOLD,
- 251 MF_UNCHECKED);
- 252 CheckMenuItem(hMenu, IDM_ITALIC,
- 253 MF_UNCHECKED);
- 254 CheckMenuItem(hMenu, IDM_UNDERLINE,
- 255 MF_UNCHECKED);
- 256 CheckMenuItem(hMenu, IDM_STRIKEOUT,
- 257 MF_UNCHECKED);
- 258
- 259 break;
- 260 }
- 261
- 262 InvalidateRect(hWnd, NULL, TRUE);
- 263 return (0);
- 264
- 265 case WM_PAINT :
- 266 hDC = BeginPaint(hWnd, &ps);
- 267
- 268 lf = FontSize.lf[SizeID-IDM_SIZE];
- 269
- 270 lf.lfWeight = bBold ? 700 : 400;
- 271 lf.lfItalic = bItalic;
- 272 lf.lfUnderline = bUnderLine;
- 273 lf.lfStrikeOut = bStrikeOut;
- 274
- 275 hFont = CreateFontIndirect(&lf);
- 276 hPreFont = SelectObject(hDC, hFont);
- 277
- 278 GetTextMetrics(hDC, &tm);
- 279 TextOut(hDC, 10, 10,
- 280 szStr1, lstrlen(szStr1));
- 281 TextOut(hDC, 10, 10+tm.tmHeight,
- 282 szStr2, lstrlen(szStr2));
- 283
- 284 SelectObject(hDC, hPreFont);
- 285 DeleteObject(hFont);
- 286
- 287 EndPaint(hWnd, &ps);
- 288 return (0);
- 289
- 290 case WM_DESTROY :
- 291 DeleteObject(hBoldBM);
- 292 DeleteObject(hItalicBM);
- 293 DeleteObject(hUnderlineBM);
- 294 DeleteObject(hStrikeOutBM);
- 295 PostQuitMessage(0);
- 296 return (0) ;
- 297
- 298 default :
- 299 return (DefWindowProc(hWnd, message, wParam, lParam));
- 300 }
- 301 }
- 302
- 303
- 304
- 305 int FAR PASCAL EnumFaces(LPLOGFONT lpLF, LPTEXTMETRIC lpTM,
- 306 short nType, LPSTR lpData)
- 307 {
- 308 lstrcpy(FontFace.FaceName[FontFace.FaceNum],
- 309 lpLF->lfFaceName);
- 310 FontFace.bRaster[FontFace.FaceNum] = nType & 1;
- 311
- 312 FontFace.FaceNum++;
- 313
- 314 if (FontFace.FaceNum >= MAXFACES)
- 315 return (0);
- 316 return (1);
- 317 }
- 318
- 319
- 320 int FAR PASCAL EnumSizes(LPLOGFONT lpLF, LPTEXTMETRIC lpTM,
- 321 short nType, LPSTR lpData)
- 322 {
- 323 if (nLogPixSx != lpTM->tmDigitizedAspectX ||
- 324 nLogPixSy != lpTM->tmDigitizedAspectY)
- 325 return (1);
- 326
- 327 FontSize.lf[FontSize.SizeNum] = *lpLF;
- 328 FontSize.tm[FontSize.SizeNum] = *lpTM;
- 329
- 330 FontSize.SizeNum++;
- 331
- 332 if (FontSize.SizeNum >= MAXSIZES)
- 333 return (0);
- 334 return (1);
- 335 }
- 336
- 337
- 338 void MakeFontMenu(HWND hWnd)
- 339 {
- 340 int i;
- 341 HDC hDC;
- 342 HMENU hMenu, hPopMenu;
- 343 FARPROC lpEnumFaces;
- 344
- 345 FontFace.FaceNum = 0;
- 346 lpEnumFaces = MakeProcInstance(EnumFaces, hInst);
- 347
- 348 hDC = GetDC(hWnd);
- 349 EnumFonts(hDC, NULL, lpEnumFaces, NULL);
- 350 ReleaseDC(hWnd, hDC);
- 351
- 352 hMenu = GetMenu(hWnd);
- 353 hPopMenu = GetSubMenu(hMenu, FONTMENU);
- 354
- 355 DeleteMenu(hPopMenu, 0, MF_BYPOSITION);
- 356
- 357 for (i=0; i<FontFace.FaceNum; i++)
- 358 AppendMenu(hPopMenu, 0, IDM_FONT+i,
- 359 FontFace.FaceName[i]);
- 360
- 361 FaceID = -1;
- 362 SendMessage(hWnd, WM_COMMAND, IDM_FONT, 0L);
- 363 }
- 364
- 365
- 366 void MakeSizeMenu(HWND hWnd)
- 367 {
- 368 int i;
- 369 HDC hDC;
- 370 HMENU hMenu, hPopMenu;
- 371 FARPROC lpEnumSizes;
- 372 char szStr[10];
- 373 static LOGFONT DefLF;
- 374
- 375 FontSize.SizeNum = 0;
- 376 lpEnumSizes = MakeProcInstance(EnumSizes, hInst);
- 377
- 378 if (FontFace.bRaster[FaceID-IDM_FONT])
- 379 {
- 380 hDC = GetDC(hWnd);
- 381 EnumFonts(hDC, FontFace.FaceName[FaceID-IDM_FONT],
- 382 lpEnumSizes, NULL);
- 383 ReleaseDC(hWnd, hDC);
- 384 }
- 385 else
- 386 FillStrokeSize();
- 387
- 388 hMenu = GetMenu(hWnd);
- 389 hPopMenu = GetSubMenu(hMenu, SIZEMENU);
- 390
- 391 while (GetMenuItemCount(hPopMenu) > 0)
- 392 DeleteMenu(hPopMenu, 0, MF_BYPOSITION);
- 393
- 394 if (FontSize.SizeNum)
- 395 for (i=0; i<FontSize.SizeNum; i++)
- 396 {
- 397 sprintf(szStr, "%2d",
- 398 ((FontSize.tm[i].tmHeight -
- 399 FontSize.tm[i].tmInternalLeading)*72
- 400 +nLogPixSy/2) / nLogPixSy) ;
- 401 AppendMenu(hPopMenu, 0, IDM_SIZE+i, szStr);
- 402 }
- 403 else
- 404 {
- 405 FontSize.lf[0] = DefLF;
- 406 lstrcpy(FontSize.lf[0].lfFaceName,
- 407 FontFace.FaceName[FaceID-IDM_FONT]);
- 408 AppendMenu(hPopMenu, 0, IDM_SIZE, "Default");
- 409 }
- 410
- 411 SizeID = -1;
- 412 SendMessage(hWnd, WM_COMMAND, IDM_SIZE, 0L);
- 413 }
- 414
- 415
- 416 void FillStrokeSize()
- 417 {
- 418 int i;
- 419 static LOGFONT DefLF;
- 420 int StrokeSize[] = { 8, 10, 12, 14, 16,
- 421 18, 20, 22, 24, 26,
- 422 28, 30, 32, 34, 36 };
- 423
- 424 FontSize.SizeNum = sizeof(StrokeSize)/sizeof(int);
- 425
- 426 for (i=0; i<FontSize.SizeNum; i++)
- 427 {
- 428 FontSize.lf[i] = DefLF;
- 429
- 430 lstrcpy(FontSize.lf[i].lfFaceName,
- 431 FontFace.FaceName[FaceID-IDM_FONT]);
- 432 FontSize.lf[i].lfCharSet = OEM_CHARSET;
- 433 FontSize.lf[i].lfHeight = StrokeSize[i]*nLogPixSy/72;
- 434
- 435 FontSize.tm[i].tmHeight = StrokeSize[i]*nLogPixSy/72;
- 436 FontSize.tm[i].tmInternalLeading = 0;
- 437 }
- 438 }
- 439
- 440
- 441 void MakeStyleMenu(HMENU hMenu)
- 442 {
- 443 HMENU hStyleMenu;
- 444
- 445 hBoldBM = LoadBitmap(hInst, "Bold");
- 446 hItalicBM = LoadBitmap(hInst, "Italic");
- 447 hUnderlineBM = LoadBitmap(hInst, "Underline");
- 448 hStrikeOutBM = LoadBitmap(hInst, "StrikeOut");
- 449
- 450 hStyleMenu = GetSubMenu(hMenu, STYLEMENU);
- 451 AppendMenu(hStyleMenu, MF_BITMAP, IDM_BOLD,
- 452 (LPSTR)(LONG)hBoldBM);
- 453 AppendMenu(hStyleMenu, MF_BITMAP, IDM_ITALIC,
- 454 (LPSTR)(LONG)hItalicBM);
- 455 AppendMenu(hStyleMenu, MF_BITMAP, IDM_UNDERLINE,
- 456 (LPSTR)(LONG)hUnderlineBM);
- 457 AppendMenu(hStyleMenu, MF_BITMAP, IDM_STRIKEOUT,
- 458 (LPSTR)(LONG)hStrikeOutBM);
- 459 }