DEMO7_1.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:15k
源码类别:

GDI/图象编程

开发平台:

Visual C++

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