home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / apps / textalgn.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  10KB  |  323 lines

  1. /*
  2.  * This application demonstrates the GetTextAlign() and SetTextAlign()
  3.  * Windows functions. This program is a subset of the SHOWFONT learning
  4.  * guide application. See that program for a more comprehensive example.
  5.  *
  6.  * In this application, a vertical and/or horizontal alignment is
  7.  * selected via the "Options..." menu (or the defaults are used),
  8.  * and the left mouse button is clicked in the client area of this
  9.  * demo application.
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14. #include <string.h>
  15. #include "textalgn.h"
  16.  
  17. /* Function Prototypes */
  18.  
  19. void SetMyDC(HDC);
  20. short StringOut(HDC, short, short, PSTR, HFONT);
  21. void ShowString(HWND);
  22. long FAR PASCAL TextAlgnWndProc(HWND, unsigned, WORD, LONG);
  23.  
  24. HANDLE  hInst;
  25. HFONT   hSFont, hFont;
  26. char    line[4][64];
  27. LOGFONT LogFont;
  28. POINT   ptCurrent = {0, 0};
  29. short   nBkMode = OPAQUE;
  30. DWORD   rgbBkColor = RGB(255, 255, 255);
  31. DWORD   rgbTextColor = RGB(0, 0, 0);
  32. short   nAlignLCR = TA_LEFT;
  33. short   nAlignTBB = TA_TOP; 
  34. WORD    wPrevVAlign = IDM_ALIGNBASE;
  35. WORD    wPrevHAlign = IDM_ALIGNLEFT;
  36. char    AppName[] = "Get/SetTextAlign() Demo";
  37. char    WindowTitle[80];
  38.  
  39.  
  40. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  41. HANDLE hInstance;
  42. HANDLE hPrevInstance;
  43. LPSTR  lpCmdLine;
  44. int    nCmdShow;
  45. {
  46.     HWND hWnd;
  47.     PWNDCLASS pWndClass;
  48.     MSG msg;
  49.  
  50.     if (!hPrevInstance) {
  51.        pWndClass->hCursor       = LoadCursor(NULL, IDC_ARROW);
  52.        pWndClass->hIcon         = NULL;
  53.        pWndClass->lpszMenuName  = (LPSTR) "TextAlgn";
  54.        pWndClass->lpszClassName = (LPSTR) "TextAlgn";
  55.        pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  56.        pWndClass->hInstance     = hInstance;
  57.        pWndClass->style         = NULL;
  58.        pWndClass->lpfnWndProc   = TextAlgnWndProc;
  59.        RegisterClass((LPWNDCLASS) pWndClass);
  60.        }
  61.  
  62.     hInst = hInstance;
  63.  
  64.     hWnd = CreateWindow((LPSTR)"TextAlgn",
  65.                         (LPSTR)"TextAlgn",
  66.                         WS_OVERLAPPEDWINDOW,
  67.                         CW_USEDEFAULT,
  68.                         CW_USEDEFAULT,
  69.                         CW_USEDEFAULT,
  70.                         CW_USEDEFAULT,
  71.                         NULL,
  72.                         NULL,
  73.                         hInstance,
  74.                         NULL);
  75.  
  76.     if (!hWnd)
  77.         return (FALSE);
  78.  
  79.     ShowWindow(hWnd, nCmdShow);
  80.     UpdateWindow(hWnd);
  81.  
  82.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  83.         TranslateMessage(&msg);
  84.         DispatchMessage(&msg);
  85.     }
  86.     return (msg.wParam);
  87. }
  88.  
  89.  
  90. /****************************************************************************
  91.  
  92.     FUNCTION: SetMyDC(HDC)
  93.  
  94.     PURPOSE: Initializes the DC
  95.  
  96. ****************************************************************************/
  97.  
  98. void SetMyDC(hDC) 
  99. HDC hDC;
  100. {
  101.     SetBkMode(hDC, nBkMode);
  102.     SetBkColor(hDC, rgbBkColor);
  103.     SetTextColor(hDC, rgbTextColor);
  104.     SetTextAlign(hDC, nAlignLCR | nAlignTBB);
  105. }
  106.  
  107. /****************************************************************************
  108.  
  109.     FUNCTION: StringOut(HDC, short, short, PSTR, HFONT)
  110.  
  111.     PURPOSE: Outputs a string to application's window
  112.  
  113. ****************************************************************************/
  114.  
  115. short StringOut(hDC, X, Y, pString, hFont)
  116. HDC   hDC;
  117. short X;
  118. short Y;
  119. PSTR  pString;
  120. HFONT hFont;
  121. {
  122.     HFONT hOldFont;
  123.     DWORD dwExtent;
  124.  
  125.     hOldFont = SelectObject(hDC, hFont);
  126.     if (hOldFont != NULL) {
  127.         dwExtent = GetTextExtent(hDC, pString, strlen(pString));
  128.         TextOut(hDC, X, Y, pString, strlen(pString));
  129.         SelectObject(hDC, hOldFont);
  130.     }
  131.     return (LOWORD(dwExtent));
  132. }
  133.  
  134. /****************************************************************************
  135.  
  136.     FUNCTION: ShowString(HWND)
  137.  
  138.     PURPOSE: Show string in current font
  139.  
  140. ****************************************************************************/
  141.  
  142. void ShowString(hWnd)
  143. HWND hWnd;
  144. {
  145.     HFONT hItalicFont;
  146.     HFONT hBoldFont;
  147.     HFONT hUnderlineFont;
  148.     HFONT hStrikeOutFont;
  149.     HDC   hDC;
  150.     short X, tmpX;
  151.     short Y;
  152.     short nAlign;
  153.  
  154.     GetObject(hFont, sizeof(LOGFONT), (LPSTR) &LogFont);
  155.     LogFont.lfItalic    = TRUE;
  156.     hItalicFont         = CreateFontIndirect(&LogFont);
  157.     LogFont.lfItalic    = FALSE;
  158.     LogFont.lfUnderline = TRUE;
  159.     hUnderlineFont      = CreateFontIndirect(&LogFont);
  160.     LogFont.lfUnderline = FALSE;
  161.     LogFont.lfStrikeOut = TRUE;
  162.     hStrikeOutFont      = CreateFontIndirect(&LogFont);
  163.     LogFont.lfStrikeOut = FALSE;
  164.     LogFont.lfWeight    = FW_BOLD;
  165.     hBoldFont           = CreateFontIndirect(&LogFont);
  166.  
  167.     hDC = GetDC(hWnd);
  168.     SetMyDC(hDC);
  169.     X = ptCurrent.x;
  170.     Y = ptCurrent.y;
  171.     nAlign = nAlignLCR | nAlignTBB;                 /* GetTextAlign(hDC); */
  172.     if ((nAlign & TA_CENTER) == TA_CENTER) {
  173.         tmpX = X;
  174.         nAlignLCR = TA_LEFT;
  175.         SetTextAlign(hDC, nAlignLCR | nAlignTBB);
  176.         X += StringOut(hDC, X, Y, ", and ", hFont);
  177.         X += StringOut(hDC, X, Y, "strikeout", hStrikeOutFont);
  178.         X += StringOut(hDC, X, Y, " in a single line.", hFont);
  179.         X = tmpX;
  180.         nAlignLCR = TA_RIGHT;
  181.         SetTextAlign(hDC, nAlignLCR | nAlignTBB);
  182.         X -= StringOut(hDC, X, Y, "underline", hUnderlineFont);
  183.         X -= StringOut(hDC, X, Y, ", ", hFont);
  184.         X -= StringOut(hDC, X, Y, "italic", hItalicFont);
  185.         X -= StringOut(hDC, X, Y, ", ", hFont);
  186.         X -= StringOut(hDC, X, Y, "bold", hBoldFont);
  187.         X -= StringOut(hDC, X, Y, "You can use ", hFont);
  188.         nAlignLCR = TA_CENTER;
  189.     }
  190.     else if ((nAlign & TA_CENTER) == TA_RIGHT) {
  191.         X -= StringOut(hDC, X, Y, " in a single line.", hFont);
  192.         X -= StringOut(hDC, X, Y, "strikeout", hStrikeOutFont);
  193.         X -= StringOut(hDC, X, Y, ", and ", hFont);
  194.         X -= StringOut(hDC, X, Y, "underline", hUnderlineFont);
  195.         X -= StringOut(hDC, X, Y, ", ", hFont);
  196.         X -= StringOut(hDC, X, Y, "italic", hItalicFont);
  197.         X -= StringOut(hDC, X, Y, ", ", hFont);
  198.         X -= StringOut(hDC, X, Y, "bold", hBoldFont);
  199.         X -= StringOut(hDC, X, Y, "You can use ", hFont);
  200.     }
  201.     else  {
  202.         X += StringOut(hDC, X, Y, "You can use ", hFont);
  203.         X += StringOut(hDC, X, Y, "bold", hBoldFont);
  204.         X += StringOut(hDC, X, Y, ", ", hFont);
  205.         X += StringOut(hDC, X, Y, "italic", hItalicFont);
  206.         X += StringOut(hDC, X, Y, ", ", hFont);
  207.         X += StringOut(hDC, X, Y, "underline", hUnderlineFont);
  208.         X += StringOut(hDC, X, Y, ", and ", hFont);
  209.         X += StringOut(hDC, X, Y, "strikeout", hStrikeOutFont);
  210.         X += StringOut(hDC, X, Y, " in a single line.", hFont);
  211.     }
  212.     ReleaseDC(hWnd, hDC);
  213.  
  214.     DeleteObject(hItalicFont);
  215.     DeleteObject(hUnderlineFont);
  216.     DeleteObject(hStrikeOutFont);
  217.     DeleteObject(hBoldFont);
  218. }
  219.  
  220. /****************************************************************************
  221.  
  222.     FUNCTION: TextAlgnWndProc(HWND, unsigned, WORD, LONG)
  223.  
  224.     PURPOSE: Processes messages
  225.  
  226. ****************************************************************************/
  227.  
  228. long FAR PASCAL TextAlgnWndProc(hWnd, message, wParam, lParam)
  229. HWND     hWnd;
  230. unsigned message;
  231. WORD     wParam;
  232. LONG     lParam;
  233. {
  234.     HDC         hDC;
  235.     PAINTSTRUCT ps;
  236.     int         i;
  237.  
  238.     switch(message) {
  239.         case WM_CREATE:
  240.             hSFont = GetStockObject(SYSTEM_FONT);
  241.             hFont  = hSFont;
  242.             GetObject(hFont, sizeof(LOGFONT), (LPSTR) &LogFont);
  243.  
  244.             for (i=0; i<64; i++) {
  245.                 line[0][i] = (char)i;
  246.                 line[1][i] = (char)i+64;
  247.                 line[2][i] = (char)i+128;
  248.                 line[3][i] = (char)i+192;
  249.             }
  250.             break;
  251.  
  252.         case WM_PAINT:
  253.             hDC = BeginPaint(hWnd, &ps);
  254.             SetMyDC(hDC);
  255.             EndPaint(hWnd, &ps);
  256.             break;
  257.  
  258.         case WM_COMMAND:
  259.             switch (wParam) {
  260.                 case IDM_CLEAR:
  261.                     InvalidateRect(hWnd, (LPRECT)NULL, TRUE);
  262.                     break;
  263.  
  264.                 case IDM_ALIGNLEFT:
  265.                     nAlignLCR = TA_LEFT;
  266.                     CheckMenuItem(GetMenu(hWnd), wPrevHAlign, MF_UNCHECKED);
  267.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  268.                     wPrevHAlign = wParam;
  269.                     break;
  270.  
  271.                 case IDM_ALIGNCENTER:
  272.                     nAlignLCR = TA_CENTER;
  273.                     CheckMenuItem(GetMenu(hWnd), wPrevHAlign, MF_UNCHECKED);
  274.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  275.                     wPrevHAlign = wParam;
  276.                     break;
  277.  
  278.                 case IDM_ALIGNRIGHT:
  279.                     nAlignLCR = TA_RIGHT;
  280.                     CheckMenuItem(GetMenu(hWnd), wPrevHAlign, MF_UNCHECKED);
  281.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  282.                     wPrevHAlign = wParam;
  283.                     break;
  284.  
  285.                 case IDM_ALIGNTOP:
  286.                     nAlignTBB = TA_TOP;
  287.                     CheckMenuItem(GetMenu(hWnd), wPrevVAlign, MF_UNCHECKED);
  288.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  289.                     wPrevVAlign = wParam;
  290.                     break;
  291.  
  292.                 case IDM_ALIGNBASE:
  293.                     nAlignTBB = TA_BASELINE;
  294.                     CheckMenuItem(GetMenu(hWnd), wPrevVAlign, MF_UNCHECKED);
  295.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  296.                     wPrevVAlign = wParam;
  297.                     break;
  298.  
  299.                 case IDM_ALIGNBOTTOM:
  300.                     nAlignTBB = TA_BOTTOM;
  301.                     CheckMenuItem(GetMenu(hWnd), wPrevVAlign, MF_UNCHECKED);
  302.                     CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  303.                     wPrevVAlign = wParam;
  304.                     break;
  305.             }
  306.             break;
  307.  
  308.         case WM_LBUTTONUP:
  309.             ptCurrent.x = LOWORD(lParam);
  310.             ptCurrent.y = HIWORD(lParam);
  311.             ShowString(hWnd);
  312.             break;
  313.  
  314.         case WM_DESTROY:
  315.             PostQuitMessage(0);
  316.             break;
  317.  
  318.         default:
  319.             return (DefWindowProc(hWnd, message, wParam, lParam));
  320.     }
  321.     return (0L);
  322. }
  323.