home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / oleview / oleview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  8.3 KB  |  336 lines

  1. // oleview.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include <winver.h>
  16.  
  17. #include <initguid.h>
  18. #include "iviewers\\iviewers.h"
  19.  
  20. #include "OleView.h"
  21.  
  22. #include "mainfrm.h"
  23. #include "doc.h"
  24. #include "obj_vw.h"
  25. #include "regview.h"
  26. #include "shadow.h"
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // COle2ViewApp
  35.  
  36. BEGIN_MESSAGE_MAP(COle2ViewApp, CWinApp)
  37.     //{{AFX_MSG_MAP(COle2ViewApp)
  38.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  39.     //}}AFX_MSG_MAP
  40.     ON_COMMAND(ID_HELP_INDEX, CWinApp::OnHelpIndex)
  41.     ON_COMMAND(ID_HELP_USING, CWinApp::OnHelpUsing)
  42.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  43.     ON_COMMAND(ID_CONTEXT_HELP, CWinApp::OnContextHelp)
  44.     ON_COMMAND(ID_DEFAULT_HELP, CWinApp::OnHelpIndex)
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // COle2ViewApp construction
  49.  
  50. COle2ViewApp::COle2ViewApp()
  51. {
  52.     // TODO: add construction code here,
  53.     // Place all significant initialization in InitInstance
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // The one and only COle2ViewApp object
  58.  
  59. COle2ViewApp theApp;
  60. #if _MFC_VER >= 0x0300
  61. OSVERSIONINFO  g_osvi ;
  62. #endif
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // COle2ViewApp initialization
  66.  
  67. BOOL COle2ViewApp::InitInstance()
  68. {
  69.     if (FAILED(AfxOleInit())) //OleInitialize(NULL)))
  70.     {
  71.         AfxMessageBox( _T("OleInitialize failed.  Could not initialized OLE 2; Ole2View cannot run.") ) ;
  72.         return FALSE;
  73.     }
  74.  
  75.     SetRegistryKey( IDS_REGISTRYKEY );
  76.  
  77.     m_pFont = ReallyCreateFont( NULL, _T("MS Sans Serif"), _T("Regular"), 8, 0 ) ;
  78.     m_pFontBold = ReallyCreateFont( NULL, _T("MS Sans Serif"), _T("Bold"), 8, 0 ) ;
  79.  
  80.     LoadBitmaps( FALSE ) ;
  81.  
  82.     TEXTMETRIC  tm ;
  83.     CDC dc ;
  84.     HDC hdc = GetDC( NULL ) ;
  85.     dc.Attach( hdc ) ;
  86.  
  87.     m_pFont = dc.SelectObject( m_pFont ) ;
  88.     dc.GetTextMetrics( &tm ) ;
  89.     m_pFont = dc.SelectObject( m_pFont ) ;
  90.     dc.Detach();
  91.     ReleaseDC( NULL, hdc ) ;
  92.  
  93.     m_cyFont = tm.tmHeight + tm.tmExternalLeading ;
  94.  
  95.     g_osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
  96.     if (GetVersionEx( &g_osvi ) && g_osvi.dwMajorVersion >= 4)
  97.     {
  98.         AfxEnableWin40Compatibility() ;
  99.     }
  100.     else
  101.         Enable3dControls();
  102.  
  103.  
  104.     // AutoRegister iviewers if not already registered
  105.     //
  106. //    char szBuf[80] ; LONG cb = sizeof(szBuf) ;
  107. //    if (RegQueryValue(HKEY_CLASSES_ROOT, "Ole2View.ITypeLib.1", szBuf, &cb) != ERROR_SUCCESS)
  108. //    {
  109.         CString str ;
  110.         CString str2 ;
  111.         HINSTANCE hinst = LoadLibrary( "iviewers.dll" ) ;
  112.         if (hinst > (HINSTANCE)HINSTANCE_ERROR)
  113.         {
  114.             // Get DllRegisterServer function
  115.             HRESULT (FAR STDAPICALLTYPE * lpDllEntryPoint)(void);
  116.             (FARPROC&)lpDllEntryPoint = GetProcAddress(hinst, "DllRegisterServer");
  117.             if (lpDllEntryPoint)
  118.             {
  119.                 HRESULT hr ;
  120.                 if (FAILED(hr = (*lpDllEntryPoint)()))
  121.                 {
  122.                     str.LoadString( IDS_AUTOREGFAILED ) ;
  123.                     str2.LoadString( IDS_IVIEWERSNOTFOUND2 ) ;
  124.                     str += str2 ;
  125.                     AfxMessageBox( str ) ;
  126.                 }
  127.             }
  128.             else
  129.             {
  130.                 str.LoadString( IDS_IVIEWERSNOTFOUND3 ) ;
  131.                 str2.LoadString( IDS_IVIEWERSNOTFOUND2 ) ;
  132.                 str += str2 ;
  133.                 AfxMessageBox( str ) ;
  134.             }
  135.  
  136.             FreeLibrary( hinst ) ;
  137.         }
  138.         else
  139.         {
  140.             str.LoadString( IDS_IVIEWERSNOTFOUND1 ) ;
  141.             str2.LoadString( IDS_IVIEWERSNOTFOUND2 ) ;
  142.             str += str2 ;
  143.             str2.LoadString( IDS_IVIEWERSNOTFOUND4 ) ;
  144.             str += str2 ;
  145.             AfxMessageBox( str ) ;
  146.         }
  147. //    }
  148.  
  149.     // Register the application's document templates.  Document templates
  150.     //  serve as the connection between documents, frame windows and views.
  151.     AddDocTemplate(new CSingleDocTemplate(IDR_MAINFRAME,
  152.             RUNTIME_CLASS(COle2ViewDoc),
  153.             RUNTIME_CLASS(CMainFrame),     // main SDI frame window
  154.             RUNTIME_CLASS(CObjTreeView)));
  155.  
  156.     int nCmdShow = m_nCmdShow ;
  157.     m_nCmdShow = SW_HIDE ;
  158.     OnFileNew() ;
  159.     ((CMainFrame*)GetMainWnd())->RestorePosition(nCmdShow) ;
  160.  
  161.     // TODO: If there's something on the command line, try to
  162.     // figure out if it's a
  163.     //  - CLSID or a ProgID: CoCreateInstance the thing
  164.     //  - A filename: Bind to the file, via a FileMoniker
  165.     // Then enum the interfaces as normal.
  166.     //
  167.     if (m_lpCmdLine != NULL && *m_lpCmdLine != '\0')
  168.     {
  169.         AfxMessageBox( IDS_BADCOMMANDLINE ) ;
  170.     }
  171.  
  172.     return TRUE;
  173. }
  174.  
  175. int COle2ViewApp::ExitInstance()
  176. {
  177.     if (m_pFont)
  178.         delete m_pFont ;
  179.  
  180.     if (m_pFontBold)
  181.         delete m_pFontBold ;
  182.  
  183. //    FreeBitmaps() ;
  184.  
  185.     return CWinApp::ExitInstance() ;
  186. }
  187.  
  188.  
  189. void COle2ViewApp::LoadBitmaps( BOOL fFree )
  190. {
  191. #if 0
  192.     COLORREF    rgbNorm, rgbHigh ;
  193.  
  194.     rgbNorm  = GetSysColor( COLOR_WINDOW ) ;
  195.     rgbHigh = GetSysColor( COLOR_HIGHLIGHT ) ;
  196.  
  197.     if (fFree)
  198.         FreeBitmaps() ;
  199.  
  200.     for (int i = IDB_FIRST ; i <= IDB_LAST ; i++)
  201.     {
  202.         m_rgbmNorm[BMINDEX(i)] = bmLoadAndTranslate( i, rgbNorm ) ;
  203.         m_rgbmHigh[BMINDEX(i)] = bmLoadAndTranslate( i, rgbHigh ) ;
  204.     }
  205. #else
  206.     if (fFree)
  207.         m_images.DeleteImageList() ;
  208.  
  209.     if (0 == m_images.Create( IDB_IMAGELIST, 16, 100, RGBLTGREEN ))
  210.     {
  211.         AfxMessageBox("Could not load bitmaps" ) ;
  212.     }
  213. #endif
  214. }
  215.  
  216. #if 0
  217. void FreeABitmap( CBitmap* p )
  218. {
  219.     if (p)
  220.         delete p ;
  221. }
  222. #endif
  223.  
  224. void COle2ViewApp::FreeBitmaps()
  225. {
  226. #if 0
  227.     for (int i = IDB_FIRST ; i <= IDB_LAST ; i++)
  228.     {
  229.         FreeABitmap( m_rgbmNorm[BMINDEX(i)]) ;
  230.         FreeABitmap( m_rgbmHigh[BMINDEX(i)]) ;
  231.     }
  232. #else
  233.     m_images.DeleteImageList() ;
  234. #endif
  235. }
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238. // COle2ViewApp commands
  239. // We override OnIdle so that we can ensure the right message is on the status
  240. // bar.   Note we have to  call OnIdle alawys to make sure stuff like
  241. // CoFreeUnusedLibraries actually works.   (Bug fixed in 1.13).
  242.  
  243. /////////////////////////////////////////////////////////////////////////////
  244. // CAboutDlg dialog used for App About
  245.  
  246. class CAboutDlg : public CDialog
  247. {
  248. public:
  249.     CAboutDlg();
  250.  
  251. // Dialog Data
  252.     //{{AFX_DATA(CAboutDlg)
  253.     enum { IDD = IDD_ABOUTBOX };
  254.     CButton m_Cancel;
  255.     CStatic m_WrittenBy;
  256.     CStatic m_Version;
  257.     CStatic m_Group;
  258.     CStatic m_Copyright;
  259.     CStatic m_AppName;
  260.     CStatic m_AllRightsReserved;
  261.     //}}AFX_DATA
  262.  
  263. // Implementation
  264. protected:
  265.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  266.     //{{AFX_MSG(CAboutDlg)
  267.     virtual BOOL OnInitDialog();
  268.     //}}AFX_MSG
  269.     DECLARE_MESSAGE_MAP()
  270. };
  271.  
  272. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  273. {
  274.     //{{AFX_DATA_INIT(CAboutDlg)
  275.     //}}AFX_DATA_INIT
  276. }
  277.  
  278. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  279. {
  280.     CDialog::DoDataExchange(pDX);
  281.     //{{AFX_DATA_MAP(CAboutDlg)
  282.     DDX_Control(pDX, IDCANCEL, m_Cancel);
  283.     DDX_Control(pDX, IDC_WRITTENBY, m_WrittenBy);
  284.     DDX_Control(pDX, IDC_VERSION, m_Version);
  285.     DDX_Control(pDX, IDC_GROUP, m_Group);
  286.     DDX_Control(pDX, IDC_COPYRIGHT, m_Copyright);
  287.     DDX_Control(pDX, IDC_APPNAME, m_AppName);
  288.     DDX_Control(pDX, IDC_ALLRIGHTSRESERVED, m_AllRightsReserved);
  289.     //}}AFX_DATA_MAP
  290. }
  291.  
  292. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  293.     //{{AFX_MSG_MAP(CAboutDlg)
  294.     //}}AFX_MSG_MAP
  295. END_MESSAGE_MAP()
  296.  
  297. // App command to run the dialog
  298. void COle2ViewApp::OnAppAbout()
  299. {
  300.     CAboutDlg aboutDlg;
  301.     aboutDlg.DoModal();
  302. }
  303.  
  304.  
  305.  
  306. typedef struct tagVS_VERSION
  307.   {
  308.     WORD wTotLen;
  309.     WORD wValLen;
  310.     TCHAR szSig[16];
  311.     VS_FIXEDFILEINFO vffInfo;
  312.   } VS_VERSION;
  313.  
  314.  
  315. BOOL CAboutDlg::OnInitDialog()
  316. {
  317.     CDialog::OnInitDialog();
  318.  
  319.     TCHAR szVersion[64] ;
  320.     wsprintf( szVersion, "Version 2.00.001 - %s",(LPTSTR)__DATE__  );
  321.  
  322. //    m_Cancel.SetFont( theApp.m_pFont ) ;
  323. //    m_AppName.SetFont( theApp.m_pFont ) ;
  324.     m_Version.SetWindowText( szVersion ) ;
  325. //    m_Version.SetFont( theApp.m_pFont ) ;
  326.  
  327. //    m_Copyright.SetFont( theApp.m_pFont ) ;
  328. //    m_AllRightsReserved.SetFont( theApp.m_pFont ) ;
  329. //    m_WrittenBy.SetFont( theApp.m_pFont ) ;
  330. //    m_Group.SetFont( theApp.m_pFont ) ;
  331.  
  332.     DlgCenter( m_hWnd, ::GetParent( m_hWnd ), FALSE ) ;
  333.  
  334.     return TRUE;  // return TRUE  unless you set the focus to a control
  335. }
  336.