home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / fonts / creatfon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  10.2 KB  |  380 lines

  1. /*
  2.  *
  3.  * Function (s) demonstrated in this program: CreateFont, CreateFontIndirect,
  4.  *      CreateBitmapIndirect, CreateCompatibleBitmap, SetBitmapBits,
  5.  *      GetBitmapBits, SetBitmapBits.
  6.  *
  7.  * Description:
  8.  * This application demonstrates the GetBitmapBits, SetBitmapBits,
  9.  * CreateBitmapIndirect, CreateFont, CreateCompatibleBitmap,
  10.  * CreateFontIndirect, and SetBitmapBits Windows functions.
  11.  *
  12.  */
  13.  
  14. #include <windows.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include "CreatFon.h"
  18.  
  19. long    FAR PASCAL WndProc  (HWND, unsigned, WORD, LONG);
  20. HBITMAP StretchBitmap (HBITMAP, int);
  21. HBITMAP GetBitmapFont (int);
  22.  
  23. long    dwCount;
  24. HBITMAP  hBitmapScottie;
  25. PBITMAP  pBitmap;
  26. char    nBits[500] = "ABC";
  27. HBITMAP  hBitmapPopFont;
  28. HBITMAP  hBitmapAbout;
  29. HANDLE   hInstMain;
  30. char    szAppName [] = "CreatFon";
  31. char    szOutputBuffer1 [70];
  32. char    szOutputBuffer2 [500];
  33.  
  34. struct {
  35.   char    *szMessage;
  36. } Messages [] =   {
  37.   "About\0",
  38.   "  This application demonstrates the GetBitmapBits, \n\
  39. SetBitmapBits, CreateBitmapIndirect, CreateFont, \n\
  40. CreateCompatibleBitmap, CreateFontIndirect, and\n\
  41. SetBitmapBits Windows functions.",
  42.  
  43.       "Help Message",
  44.   "     This program demonstrates the use of many\n\
  45. Windows functions.  Use the menu to test these\n\
  46. functions.",
  47.  
  48.   "CreateFont",
  49.   "     The menu has a new item the font for which\n\
  50. was created using the CreateFont Windows function.",
  51.  
  52.   "CreateFontIndirect",
  53.   "     The menu has a new item the font for which\n\
  54. was created using the CreateFontIndirect Windows\n\
  55. function.",
  56.  
  57.   "CreateBitmapIndirect",
  58.   "     The menu has a new item the bitmap for which\n\
  59. was created using the CreateBitmapIndirect Windows\n\
  60. function.",
  61.  
  62.       "CreateCompatibleBitmap",
  63.   "     The menu has a new item the bitmap for which\n\
  64. was created using the CreateCompatibleBitmap Windows\n\
  65. function.",
  66.  
  67.       "GetBitmapBits",
  68.   "    Bitmap bits are stored in a buffer.",
  69.  
  70.   "SetBitmapBits",
  71.   "     The menu has a new item the bitmap for which\n\
  72. was created using the SetBitmapBits Windows\n\
  73. function.",
  74.  
  75.   "SetBitmapBits",
  76.   "     You must Get the Bits first!",
  77.  
  78. };
  79.  
  80.  
  81. /****************************************************************************/
  82.  
  83. void ProcessMessage (HWND, int);
  84.  
  85. void ProcessMessage (hWnd, MessageNumber)  /*  For outputting messages  */
  86. HWND     hWnd;
  87. int    MessageNumber;
  88. {
  89.   sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  90.   sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  91.   MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  92. }
  93.  
  94.  
  95. /****************************************************************************/
  96.  
  97. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  98. HANDLE   hInstance, hPrevInstance;
  99. LPSTR    lpszCmdLine;
  100. int    nCmdShow;
  101. {
  102.   HMENU    hMenu, hMenuPopup;
  103.   HWND     hWnd;
  104.   MSG      msg;
  105.   int    i;
  106.   WNDCLASS wndclass;
  107.  
  108.   if (!hPrevInstance)
  109.   {
  110.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  111.     wndclass.lpfnWndProc   = WndProc;
  112.     wndclass.cbClsExtra    = 0;
  113.     wndclass.cbWndExtra    = 0;
  114.     wndclass.hInstance     = hInstance;
  115.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  116.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  117.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  118.     wndclass.lpszMenuName  = szAppName;
  119.     wndclass.lpszClassName = szAppName;
  120.  
  121.     if (!RegisterClass (&wndclass))
  122.       return FALSE;
  123.   }
  124.  
  125.   hInstMain = hInstance;
  126.  
  127.   hWnd = CreateWindow (szAppName, "Bitmap Menu Demonstration",
  128.       WS_OVERLAPPEDWINDOW,
  129.       CW_USEDEFAULT, 0,
  130.       CW_USEDEFAULT, 0,
  131.       NULL, NULL, hInstance, NULL);
  132.  
  133.   hMenu = GetSystemMenu (hWnd, FALSE);
  134.   hBitmapAbout = StretchBitmap (LoadBitmap (hInstance, "BitmapAbout"), 1);
  135.   ChangeMenu (hMenu, NULL, NULL, 0, MF_APPEND | MF_SEPARATOR);
  136.   ChangeMenu (hMenu, NULL, (PSTR) hBitmapAbout, IDM_ABOUT,
  137.       MF_APPEND | MF_BITMAP);
  138.  
  139.   ShowWindow (hWnd, nCmdShow);
  140.   UpdateWindow (hWnd);
  141.  
  142.   while (GetMessage (&msg, NULL, 0, 0))
  143.   {
  144.     TranslateMessage (&msg);
  145.     DispatchMessage (&msg);
  146.   }
  147.  
  148.   DeleteObject (hBitmapPopFont);  /*  Free up memory taken by bitmaps  */
  149.   DeleteObject (hBitmapAbout);
  150.   DeleteObject (hBitmapScottie);
  151.  
  152.   return msg.wParam;
  153. }
  154.  
  155.  
  156. /****************************************************************************/
  157.  
  158. HBITMAP StretchBitmap (hBitmap1, I)
  159. HBITMAP    hBitmap1;
  160. int    I;
  161. {
  162.   BITMAP     bm1, bm2;
  163.   HBITMAP    hBitmap2;
  164.   HDC        hDC, hMemDC1, hMemDC2;
  165.   TEXTMETRIC tm;
  166.  
  167.   hDC = CreateIC ("DISPLAY", NULL, NULL, NULL);
  168.   GetTextMetrics (hDC, &tm);
  169.   hMemDC1 = CreateCompatibleDC (hDC);
  170.   hMemDC2 = CreateCompatibleDC (hDC);
  171.   DeleteDC (hDC);
  172.  
  173.   GetObject (hBitmap1, sizeof (BITMAP), (LPSTR) & bm1);
  174.  
  175.   bm2 = bm1;
  176.   bm2.bmWidth      = (tm.tmAveCharWidth * bm2.bmWidth)  / 4;
  177.   bm2.bmHeight     = (tm.tmHeight       * bm2.bmHeight) / 8;
  178.   bm2.bmWidthBytes = ( (bm2.bmWidth + 15) / 16) * 2;
  179.  
  180.   if (I == 1)
  181.   {
  182.     hBitmap2 = CreateBitmapIndirect (&bm2);
  183.     SelectObject (hMemDC1, hBitmap1);
  184.     SelectObject (hMemDC2, hBitmap2);
  185.   }
  186.   else    
  187.   {
  188.     SelectObject (hMemDC1, hBitmap1);
  189.     hBitmap2 = CreateCompatibleBitmap (hMemDC1, bm2.bmWidth, bm2.bmHeight);
  190.     SelectObject (hMemDC2, hBitmap2);
  191.   }
  192.  
  193.   StretchBlt (hMemDC2, 0, 0, bm2.bmWidth, bm2.bmHeight,
  194.       hMemDC1, 0, 0, bm1.bmWidth, bm1.bmHeight, SRCCOPY);
  195.  
  196.   DeleteDC (hMemDC1);
  197.   DeleteDC (hMemDC2);
  198.   DeleteObject (hBitmap1);
  199.  
  200.   return hBitmap2;
  201. }
  202.  
  203.  
  204. /****************************************************************************/
  205.  
  206. HBITMAP GetBitmapFont (i)
  207. int    i;
  208. {
  209.   static struct  
  210.   {
  211.     BYTE lfPitchAndFamily;
  212.     BYTE lfFaceName [LF_FACESIZE];
  213.     char    *szMenuText;
  214.   } lfSet [2] = 
  215.   {
  216.     VARIABLE_PITCH | FF_ROMAN,  "Tms Rmn",   "Times Roman",
  217.     VARIABLE_PITCH | FF_SWISS,  "Helvetica", "Helvetica"
  218.   };
  219.   DWORD   dwSize;
  220.   HBITMAP hBitmap;
  221.   HDC     hDC, hMemDC;
  222.   HFONT   hFont, hNewFont;
  223.   LOGFONT lf;
  224.  
  225.   hFont = GetStockObject (SYSTEM_FONT);
  226.   GetObject (hFont, sizeof (LOGFONT), (LPSTR) & lf);
  227.  
  228.   lf.lfHeight *= 2;
  229.   lf.lfWidth  *= 2;
  230.   lf.lfPitchAndFamily = lfSet[i].lfPitchAndFamily;
  231.   strcpy (lf.lfFaceName, lfSet[i].lfFaceName);
  232.  
  233.   hDC = CreateIC ("DISPLAY", NULL, NULL, NULL);
  234.   hMemDC = CreateCompatibleDC (hDC);
  235.  
  236.   if (i == 0)
  237.   {
  238.     hNewFont = CreateFont (lf.lfHeight, lf.lfWidth, lf.lfEscapement,
  239.         lf.lfOrientation, lf.lfWeight, lf.lfItalic,
  240.         lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
  241.         lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
  242.         lf.lfPitchAndFamily, lf.lfFaceName);
  243.     SelectObject (hMemDC, hNewFont);
  244.   }
  245.   else
  246.     SelectObject (hMemDC, CreateFontIndirect (&lf));
  247.  
  248.   dwSize = GetTextExtent (hMemDC, lfSet[i].szMenuText,
  249.       strlen (lfSet[i].szMenuText));
  250.  
  251.   hBitmap = CreateBitmap (LOWORD (dwSize), HIWORD (dwSize), 1, 1, NULL);
  252.   SelectObject (hMemDC, hBitmap);
  253.   TextOut (hMemDC, 0, 0, lfSet[i].szMenuText, strlen (lfSet[i].szMenuText));
  254.  
  255.   DeleteObject (SelectObject (hMemDC, hFont));
  256.   DeleteDC (hMemDC);
  257.   DeleteDC (hDC);
  258.  
  259.   return hBitmap;
  260. }
  261.  
  262.  
  263. /****************************************************************************/
  264.  
  265. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  266. HWND     hWnd;
  267. unsigned    iMessage;
  268. WORD     wParam;
  269. LONG     lParam;
  270. {
  271.   HMENU    hMenu;
  272.   static short    nCurrentFont = IDM_TMSRMN;
  273.  
  274.   switch (iMessage)
  275.   {
  276.   case WM_SYSCOMMAND:
  277.     switch (wParam)
  278.     {
  279.     case IDM_ABOUT:
  280.       ProcessMessage (hWnd, 0);
  281.       break;
  282.     default:
  283.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  284.     }
  285.     break;
  286.  
  287.   case WM_COMMAND:
  288.     switch (wParam)
  289.     {
  290.     case IDM_CREATEFONT:
  291.       hMenu = GetSubMenu (GetMenu (hWnd), 0);
  292.       hBitmapPopFont = GetBitmapFont (0);
  293.       ChangeMenu (hMenu, NULL, NULL, 0,
  294.           MF_APPEND | MF_SEPARATOR);
  295.       ChangeMenu (hMenu, NULL, (PSTR) hBitmapPopFont,
  296.           IDM_TMSRMN, MF_APPEND | MF_BITMAP);
  297.       ProcessMessage (hWnd, 4);
  298.       break;
  299.  
  300.     case IDM_CREATEFONTINDIRECT:
  301.       hMenu = GetSubMenu (GetMenu (hWnd), 0);
  302.       hBitmapPopFont = GetBitmapFont (1);
  303.       ChangeMenu (hMenu, NULL, NULL, 0,
  304.           MF_APPEND | MF_SEPARATOR);
  305.       ChangeMenu (hMenu, NULL, (PSTR) hBitmapPopFont,
  306.           IDM_TMSRMN, MF_APPEND | MF_BITMAP);
  307.       ProcessMessage (hWnd, 6);
  308.       break;
  309.  
  310.     case IDM_CREATEBMPINDIRECT:
  311.       hMenu = GetSubMenu (GetMenu (hWnd), 0);
  312.       hBitmapAbout = StretchBitmap (LoadBitmap (hInstMain,
  313.           "BitmapAbout"), 1);
  314.       ChangeMenu (hMenu, NULL, NULL, 0,
  315.           MF_APPEND | MF_SEPARATOR);
  316.       ChangeMenu (hMenu, NULL, (PSTR) hBitmapAbout,
  317.           IDM_ABOUT, MF_APPEND | MF_BITMAP);
  318.       ProcessMessage (hWnd, 8);
  319.       break;
  320.  
  321.     case IDM_CREATECOMPATIBLEBMP:
  322.       hMenu = GetSubMenu (GetMenu (hWnd), 0);
  323.       hBitmapAbout = StretchBitmap (LoadBitmap (hInstMain,
  324.           "BitmapAbout"), 2);
  325.       ChangeMenu (hMenu, NULL, NULL, 0,
  326.           MF_APPEND | MF_SEPARATOR);
  327.       ChangeMenu (hMenu, NULL, (PSTR) hBitmapAbout,
  328.           IDM_ABOUT, MF_APPEND | MF_BITMAP);
  329.       ProcessMessage (hWnd, 10);
  330.       break;
  331.  
  332.     case IDM_GETBITMAPBITS:
  333.       hBitmapScottie = LoadBitmap (hInstMain,
  334.           "BitmapScottie");
  335.       pBitmap = (PBITMAP)LocalAlloc (LMEM_MOVEABLE,
  336.           sizeof (BITMAP));
  337.       dwCount = GetObject (hBitmapScottie,
  338.           (long)sizeof (BITMAP), (LPSTR) pBitmap);
  339.       if (GetBitmapBits (hBitmapScottie, dwCount,
  340.           (LPSTR)nBits))
  341.     ProcessMessage (hWnd, 12);
  342.       else
  343.     MessageBox (hWnd, "Bitmap did not copy!",
  344.         "GetBitmapBits", MB_OK);
  345.       break;
  346.  
  347.     case IDM_SETBITMAPBITS:
  348.       if (nBits [0] == 'A')
  349.     ProcessMessage (hWnd, 16);
  350.       else
  351.       {
  352.     SetBitmapBits (hBitmapScottie, dwCount, nBits);
  353.     hMenu = GetSubMenu (GetMenu (hWnd), 0);
  354.     hBitmapScottie = StretchBitmap (hBitmapScottie, 1);
  355.     ChangeMenu (hMenu, NULL, NULL, 0,
  356.         MF_APPEND | MF_SEPARATOR);
  357.     ChangeMenu (hMenu, NULL, (PSTR) hBitmapScottie,
  358.         IDM_SCOTTIE, MF_APPEND | MF_BITMAP);
  359.     ProcessMessage (hWnd, 14);
  360.       }
  361.       break;
  362.  
  363.     case IDM_HELP:
  364.       ProcessMessage (hWnd, 2);
  365.       break;
  366.     }
  367.     break;
  368.  
  369.   case WM_DESTROY:
  370.     PostQuitMessage (0);
  371.     break;
  372.  
  373.   default:
  374.     return DefWindowProc (hWnd, iMessage, wParam, lParam);
  375.   }
  376.   return 0L;
  377. }
  378.  
  379.  
  380.