home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_15 / Text / Text.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-24  |  8.5 KB  |  370 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : text.cpp                                                             //
  10. //  Description: Text demo program, Chapter 15                                       //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define _WIN32_WINNT 0x0500
  16. #define NOCRYPT
  17.  
  18. #pragma pack(push, 4)
  19. #include <windows.h>
  20. #pragma pack(pop)
  21.  
  22. #include <assert.h>
  23. #include <tchar.h>
  24. #include <math.h>
  25.  
  26. #include "..\..\include\wingraph.h"
  27. #include "..\..\include\MVC.h"
  28. #include "..\..\include\fonttext.h"
  29. #include "..\..\include\gdiobject.h"
  30. #include "..\..\include\emf.h"
  31. #include "..\..\include\listview.h"
  32. #include "..\..\include\OutlineMetric.h"
  33.  
  34. #include "Resource.h"
  35. #include "Panose.h"
  36. #include "ChooseFont.h"
  37. #include "TextDemo.h"
  38.  
  39. class KDemoView : public KView
  40. {
  41.     int        m_nCommand;
  42.     
  43.     virtual int OnDraw(HDC hDC, const RECT * rcPaint)
  44.     {
  45.         switch ( m_nCommand )
  46.         {
  47.             case IDM_VIEW_TERM:
  48.                 Demo_Term(hDC, rcPaint);
  49.                 break;
  50.  
  51.             case IDM_VIEW_STOCK:
  52.                 Demo_StockFonts(hDC, rcPaint);
  53.                 break;
  54.  
  55.             case IDM_VIEW_COORDINATE:
  56.                 Demo_CoordinateSystem(hDC, rcPaint);
  57.                 break;
  58.  
  59.             case IDM_VIEW_ALIGN:
  60.                 Demo_TextAlignment(hDC, rcPaint);
  61.                 break;
  62.  
  63.             case IDM_VIEW_RTLREADING:
  64.                 Demo_RTL(hDC, rcPaint);
  65.                 break;
  66.  
  67.             case IDM_VIEW_JUSTIFICATION:
  68.                 Demo_Justification(hDC, rcPaint);
  69.                 break;
  70.  
  71.             case IDM_VIEW_WIDTH:
  72.                 Demo_CharacterWidth(hDC, rcPaint);
  73.                 break;
  74.  
  75.             case IDM_VIEW_GLYPHINDEX:
  76.                 Demo_GlyphIndex(hDC, rcPaint);
  77.                 break;
  78.  
  79.             case IDM_VIEW_PLACEMENT:
  80.                 Demo_CharacterPlacement(hDC, rcPaint);
  81.                 break;
  82.  
  83.             case IDM_VIEW_GLYPHOUTLINE:
  84.                 Demo_GlyphOutline(hDC);
  85.                 break;
  86.  
  87.             case IDM_VIEW_TABBEDTEXTOUT:
  88.                 Demo_TabbedTextOut(hDC);
  89.                 break;
  90.  
  91.             case IDM_VIEW_DRAWTEXT:
  92.                 Demo_Paragraph(hDC, false);
  93.                 break;
  94.  
  95.             case IDM_VIEW_PARAGRAPH:
  96.                 Demo_Paragraph(hDC, true);
  97.                 break;
  98.  
  99.             case IDM_VIEW_TEXTCOLOR:
  100.                 Demo_TextColor(hDC, (HINSTANCE) GetWindowLong(WindowFromDC(hDC), GWL_HINSTANCE));
  101.                 break;
  102.  
  103.             case IDM_VIEW_TEXTSTYLE:
  104.                 Demo_TextStyle(hDC);
  105.                 break;
  106.  
  107.             case IDM_VIEW_TEXTGEOMETRY:
  108.                 Demo_TextGeometry(hDC);
  109.                 break;
  110.  
  111.             case IDM_VIEW_TEXTASBITMAP:
  112.                 Demo_TextasBitmap(hDC);
  113.                 break;
  114.  
  115.             case IDM_VIEW_TEXTASBITMAP2:
  116.                 Demo_TextasBitmap2(hDC);
  117.                 break;
  118.  
  119.             case IDM_VIEW_TEXTASCURVE:
  120.                 Demo_TextasCurve(hDC);
  121.                 break;
  122.  
  123.             case IDM_VIEW_TEXTCURVE2:
  124.                 Demo_TextasCurve2(hDC);
  125.                 break;
  126.         }
  127.  
  128.         return View_NoChange;
  129.     }
  130.     
  131. public:
  132.  
  133.     virtual int OnCommand(int cmd, HWND hWnd)
  134.     {
  135.         if ( cmd==m_nCommand )
  136.             return View_NoChange;
  137.  
  138.         switch ( cmd )
  139.         {
  140.             case IDM_VIEW_TERM:
  141.             case IDM_VIEW_STOCK:
  142.             case IDM_VIEW_COORDINATE:
  143.             case IDM_VIEW_ALIGN:
  144.             case IDM_VIEW_RTLREADING:
  145.             case IDM_VIEW_JUSTIFICATION:
  146.             case IDM_VIEW_WIDTH:
  147.             case IDM_VIEW_GLYPHINDEX:
  148.             case IDM_VIEW_PLACEMENT:
  149.             case IDM_VIEW_GLYPHOUTLINE:
  150.                 m_nCommand = cmd;
  151.                 return View_Redraw;
  152.  
  153.             case IDM_VIEW_TABBEDTEXTOUT:
  154.                 m_nCommand = cmd;
  155.                 SetWindowText(hWnd, "TabbledTextOut/DrawText");
  156.                 return View_Redraw;
  157.  
  158.             case IDM_VIEW_DRAWTEXT:
  159.                 m_nCommand = cmd;
  160.                 SetWindowText(hWnd, "Text Formatting - DrawText");
  161.                 return View_Redraw;
  162.  
  163.             case IDM_VIEW_PARAGRAPH:
  164.                 m_nCommand = cmd;
  165.                 SetWindowText(hWnd, "Text Formatting - High Precision");
  166.                 return View_Redraw;
  167.  
  168.             case IDM_VIEW_TEXTCOLOR:
  169.                 m_nCommand = cmd;
  170.                 SetWindowText(hWnd, "Text Effects - Color");
  171.                 return View_Redraw;
  172.  
  173.             case IDM_VIEW_TEXTSTYLE:
  174.                 m_nCommand = cmd;
  175.                 SetWindowText(hWnd, "Text Effects - Style");
  176.                 return View_Redraw;
  177.  
  178.             case IDM_VIEW_TEXTGEOMETRY:
  179.                 m_nCommand = cmd;
  180.                 SetWindowText(hWnd, "Text Effects - Geometry");
  181.                 return View_Redraw;
  182.  
  183.             case IDM_VIEW_TEXTASBITMAP:
  184.                 m_nCommand = cmd;
  185.                 SetWindowText(hWnd, "Text Effects - Text as Bitmap");
  186.                 return View_Redraw;
  187.  
  188.             case IDM_VIEW_TEXTASBITMAP2:
  189.                 m_nCommand = cmd;
  190.                 SetWindowText(hWnd, "Text Effects - Text as Bitmap - Transparent Embossing");
  191.                 return View_Redraw;
  192.  
  193.             case IDM_VIEW_TEXTASCURVE:
  194.                 m_nCommand = cmd;
  195.                 SetWindowText(hWnd, "Text Effects - Text as Curve");
  196.                 return View_Redraw;
  197.  
  198.             case IDM_VIEW_TEXTCURVE2:
  199.                 m_nCommand = cmd;
  200.                 SetWindowText(hWnd, "Text Effects - Text Curve Transformation");
  201.                 return View_Redraw;
  202.  
  203.             case IDM_FILE_SAVE:
  204.                 {
  205.                     HDC hDC = QuerySaveEMFFile("Text EMF Sample\0", NULL, NULL);
  206.  
  207.                     if ( hDC )
  208.                     {
  209.                         OnDraw(hDC, NULL);
  210.                         HENHMETAFILE hEmf = CloseEnhMetaFile(hDC);
  211.                         DeleteEnhMetaFile(hEmf);
  212.                     }
  213.                 }
  214.                 break;
  215.  
  216.         }
  217.  
  218.         return View_NoChange;
  219.     }
  220.     
  221.     KDemoView(void)
  222.     {
  223.         m_nCommand  = IDM_VIEW_STOCK;
  224.     }
  225. };
  226.  
  227.  
  228. ////////////////////////// MDI Frame Window
  229.  
  230. const int Translate[] =
  231. {
  232.     IDM_FILE_CLOSE,
  233.     IDM_FILE_EXIT,
  234.     IDM_WINDOW_TILE,
  235.     IDM_WINDOW_CASCADE,
  236.     IDM_WINDOW_ARRANGE,
  237.     IDM_WINDOW_CLOSEALL
  238. };
  239.  
  240.  
  241. class KMyMDIFrame : public KMDIFrame
  242. {
  243.     void GetWndClassEx(WNDCLASSEX & wc)
  244.     {
  245.         KMDIFrame::GetWndClassEx(wc);
  246.     
  247.         wc.hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_TEXT));
  248.     }
  249.  
  250.  
  251.     void CreateMDIChild(KCanvas * canvas, const TCHAR * klass, const TCHAR * title)
  252.     {
  253.         MDICREATESTRUCT mdic;
  254.  
  255.         mdic.szClass = klass;
  256.         mdic.szTitle = title;
  257.         mdic.hOwner  = m_hInst;
  258.         mdic.x       = CW_USEDEFAULT;
  259.         mdic.y       = CW_USEDEFAULT;
  260.         mdic.cx      = CW_USEDEFAULT;
  261.         mdic.cy      = CW_USEDEFAULT;
  262.         mdic.style   = WS_VISIBLE | WS_BORDER;
  263.         mdic.lParam  = (LPARAM) canvas;
  264.  
  265.         HWND hWnd = (HWND) SendMessage(m_hMDIClient, WM_MDICREATE, 0, (LPARAM) & mdic);
  266.     }
  267.  
  268.     
  269.     BOOL CreateCanvas(KView * pView, const TCHAR * Title)
  270.     {
  271.         if ( pView==NULL )
  272.             return FALSE;
  273.  
  274.         KMDICanvas   * pCanvas = new KMDICanvas();
  275.  
  276.         if ( pCanvas )
  277.         {
  278.             if ( pCanvas->Initialize(m_hInst, m_pStatus, pView, IDR_DEMO, IDI_TEXT) )
  279.             {
  280.                 CreateMDIChild(pCanvas, pCanvas->GetClassName(), Title);
  281.                 return TRUE;
  282.             }
  283.  
  284.             delete pCanvas;
  285.         }
  286.  
  287.         delete pView;
  288.         return FALSE;
  289.     }
  290.  
  291.     
  292.     virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
  293.     {
  294.         switch ( LOWORD(wParam) )
  295.         {
  296.             case IDM_FILE_DEMO:
  297.                 CreateCanvas(new KDemoView(), _T("Demo - "));
  298.                 return TRUE;
  299.  
  300.             case IDM_FILE_PANOSE:
  301.                 {
  302.                     KPanoseView * pList = new KPanoseView;
  303.  
  304.                     if ( pList )
  305.                         if ( pList->Initialize(m_hInst, m_pStatus, m_hWnd) )
  306.                             CreateMDIChild(pList, _T("PanoseViewClass"), _T("PANOSE Font Matching"));
  307.                         else
  308.                             delete pList;
  309.                 }
  310.                 return TRUE;
  311.  
  312.             case IDM_FILE_CHOOSEFONT:
  313.                 FontSelection(m_hInst);
  314.                 return TRUE;
  315.  
  316.             case IDM_FILE_LC:
  317.                 Test_LC();
  318.                 return TRUE;
  319.  
  320.             case IDM_FILE_POINT:
  321.                 Test_Point();
  322.         }
  323.  
  324.         return FALSE;
  325.     }
  326.  
  327.     
  328.     virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  329.     {
  330.         return KMDIFrame::WndProc(hWnd, uMsg, wParam, lParam);
  331.     }
  332.  
  333. public:
  334.     
  335.     KMyMDIFrame(HINSTANCE hInstance, const TBBUTTON * pButtons, int nCount,
  336.         KToolbar * pToolbar, KStatusWindow * pStatus) :
  337.         KMDIFrame(hInstance, pButtons, nCount, pToolbar, pStatus, Translate)
  338.     {
  339.     }
  340. };
  341.  
  342.  
  343. const TBBUTTON tbButtons[] =
  344. {
  345.     { STD_FILENEW,     IDM_FILE_NEW,   TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILENEW,  0 },
  346.     { STD_FILEOPEN,  IDM_FILE_OPEN,  TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILEOPEN, 0 }
  347. };
  348.  
  349.  
  350.  
  351. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow)
  352. {
  353.     KToolbar      toolbar;
  354.     KStatusWindow status;
  355.  
  356.     KMyMDIFrame frame(hInst, tbButtons, 2, & toolbar, & status);
  357.     
  358.     frame.CreateEx(0, _T("ClassName"), _T("Text"),
  359.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  360.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  361.         NULL, LoadMenu(hInst, MAKEINTRESOURCE(IDR_MAIN)), hInst);
  362.  
  363.     frame.ShowWindow(nShow);
  364.     frame.UpdateWindow();
  365.  
  366.     frame.MessageLoop();
  367.  
  368.     return 0;
  369. }
  370.