home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_17 / CodePrint / CodePrint.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-24  |  4.8 KB  |  176 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   : codeprint.cpp                                                         //
  10. //  Description: Syntax-highlighted program displaying and printing                  //
  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. #include <stdio.h>
  26.  
  27. #include "..\..\include\win.h"
  28. #include "..\..\include\dialog.h"
  29. #include "..\..\include\Toolbar.h"
  30. #include "..\..\include\Status.h"
  31. #include "..\..\include\FrameWnd.h"
  32. #include "..\..\include\GDIObject.h"
  33. #include "..\..\include\pen.h"
  34. #include "..\..\include\filedialog.h"
  35. #include "..\..\include\outputsetup.h"
  36. #include "..\..\include\fonttext.h"
  37. #include "..\..\include\pagecanvas.h"
  38.  
  39. #include "resource.h"
  40. #include "progview.h"
  41.  
  42. const int Translate[] =
  43. {
  44.     IDM_FILE_CLOSE,
  45.     IDM_FILE_EXIT,
  46.     IDM_WINDOW_TILE,
  47.     IDM_WINDOW_CASCADE,
  48.     IDM_WINDOW_ARRANGE,
  49.     IDM_WINDOW_CLOSEALL
  50. };
  51.  
  52. class KMyMDIFrame : public KMDIFrame
  53. {
  54.     void CreateMDIChild(KCanvas * canvas, const TCHAR * klass, const TCHAR * title)
  55.     {
  56.         MDICREATESTRUCT mdic;
  57.  
  58.         mdic.szClass = klass;
  59.         mdic.szTitle = title;
  60.         mdic.hOwner  = m_hInst;
  61.         mdic.x       = CW_USEDEFAULT;
  62.         mdic.y       = CW_USEDEFAULT;
  63.         mdic.cx      = CW_USEDEFAULT;
  64.         mdic.cy      = CW_USEDEFAULT;
  65.         mdic.style   = WS_VISIBLE | WS_BORDER;
  66.         mdic.lParam  = (LPARAM) canvas;
  67.  
  68.         HWND hWnd = (HWND) SendMessage(m_hMDIClient, WM_MDICREATE, 0, (LPARAM) & mdic);
  69.     }
  70.  
  71.     BOOL CreatePageCanvas(const TCHAR * Title, char * buffer, int size)
  72.     {
  73.         KPageCanvas  * pCanvas;
  74.         
  75.         pCanvas = new KProgramPageCanvas(buffer, size, GetSysColorBrush(COLOR_BTNSHADOW));
  76.  
  77.         if ( pCanvas )
  78.         {
  79.             if ( pCanvas->Initialize(m_hInst, m_pStatus, IDR_DEMO) )
  80.             {
  81.                 CreateMDIChild(pCanvas, pCanvas->GetClassName(), Title);
  82.                 return TRUE;
  83.             }
  84.  
  85.             delete pCanvas;
  86.         }
  87.  
  88.         return FALSE;
  89.     }
  90.  
  91.     virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
  92.     {
  93.         switch ( LOWORD(wParam) )
  94.         {
  95.             case IDM_FILE_OPEN:
  96.             {
  97.                 KFileDialog fd;
  98.  
  99.                 if ( ! fd.GetOpenFileName(m_hWnd, "cpp", "C++ Programs") )
  100.                     return TRUE;
  101.  
  102.                 HANDLE hFile = CreateFile(fd.m_TitleName, GENERIC_READ, FILE_SHARE_READ, NULL,
  103.                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  104.  
  105.                 if (hFile == INVALID_HANDLE_VALUE)
  106.                     return TRUE;
  107.  
  108.                 int size = GetFileSize(hFile, NULL);
  109.                 char * buffer = new char[size+1];
  110.                     
  111.                 if ( buffer )
  112.                 {
  113.                     buffer[size] = 0;
  114.  
  115.                     DWORD dwRead;
  116.                     ReadFile(hFile, buffer, size, & dwRead, NULL);
  117.  
  118.                     CreatePageCanvas(fd.m_TitleName, buffer, size);
  119.                 }
  120.  
  121.                 CloseHandle(hFile);
  122.             }
  123.             return TRUE;
  124.         }
  125.  
  126.         return FALSE;
  127.     }
  128.  
  129.     virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  130.     {
  131.         return KMDIFrame::WndProc(hWnd, uMsg, wParam, lParam);
  132.     }
  133.  
  134.     void GetWndClassEx(WNDCLASSEX & wc)
  135.     {
  136.         KMDIFrame::GetWndClassEx(wc);
  137.  
  138.         wc.hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_PRINT));
  139.     }
  140.  
  141. public:
  142.     KMyMDIFrame(HINSTANCE hInstance, const TBBUTTON * pButtons, int nCount,
  143.         KToolbar * pToolbar, KStatusWindow * pStatus) :
  144.         KMDIFrame(hInstance, pButtons, nCount, pToolbar, pStatus, Translate)
  145.     {
  146.     }
  147. };
  148.  
  149.  
  150. const TBBUTTON tbButtons[] =
  151. {
  152.     { STD_FILENEW,     IDM_FILE_NEW,   TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILENEW,  0 },
  153.     { STD_FILEOPEN,  IDM_FILE_OPEN,  TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILEOPEN, 0 }
  154. };
  155.  
  156.  
  157. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow)
  158. {
  159.     KToolbar      toolbar;
  160.     KStatusWindow status;
  161.  
  162.     KMyMDIFrame frame(hInst, tbButtons, 2, & toolbar, & status);
  163.     
  164.     frame.CreateEx(0, _T("ClassName"), _T("CodePrint"),
  165.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  166.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  167.         NULL, LoadMenu(hInst, MAKEINTRESOURCE(IDR_MAIN)), hInst);
  168.  
  169.     frame.ShowWindow(nShow);
  170.     frame.UpdateWindow();
  171.  
  172.     frame.MessageLoop();
  173.  
  174.     return 0;
  175. }
  176.