home *** CD-ROM | disk | FTP | other *** search
/ Computer Buyer 1996 March / buyer-0396.iso / micrgrap / abc.z / OLE_VBX.CPP < prev    next >
C/C++ Source or Header  |  1995-09-15  |  9KB  |  300 lines

  1. // ole_vbx.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ole_vbx.h"
  6.  
  7. #include "mainfrm.h"
  8. #include "ole_vdoc.h"
  9. #include "ole_vvw.h"
  10.  
  11. #include "abcauto.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // COle_vbxApp
  20.  
  21. BEGIN_MESSAGE_MAP(COle_vbxApp, CWinApp)
  22.     //{{AFX_MSG_MAP(COle_vbxApp)
  23.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  24.         // NOTE - the ClassWizard will add and remove mapping macros here.
  25.         //    DO NOT EDIT what you see in these blocks of generated code!
  26.     //}}AFX_MSG_MAP
  27.     // Standard file based document commands
  28.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  29.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // COle_vbxApp construction
  34.  
  35. COle_vbxApp::COle_vbxApp()
  36. {
  37.     // TODO: add construction code here,
  38.     // Place all significant initialization in InitInstance
  39. }
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // The one and only COle_vbxApp object
  43.  
  44. COle_vbxApp NEAR theApp;
  45.  
  46. // This identifier was generated to be statistically unique for your app.
  47. // You may change it if you prefer to choose a specific identifier.
  48. static const CLSID BASED_CODE clsid =
  49. { 0x76513a60, 0xa596, 0x101b, { 0x99, 0xa9, 0x0, 0x0, 0xc0, 0xb5, 0x8e, 0x81 } };
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // COle_vbxApp initialization
  53.  
  54. BOOL COle_vbxApp::InitInstance()
  55. {
  56.     // Initialize OLE 2.0 libraries
  57.     if (!AfxOleInit())
  58.     {
  59.         AfxMessageBox(IDP_OLE_INIT_FAILED);
  60.         return FALSE;
  61.     }
  62.  
  63.     // Standard initialization
  64.     // If you are not using these features and wish to reduce the size
  65.     //  of your final executable, you should remove from the following
  66.     //  the specific initialization routines you do not need.
  67.  
  68.     SetDialogBkColor();        // Set dialog background color to gray
  69.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  70.     EnableVBX();               // Initialize VBX support
  71.  
  72.     // Register the application's document templates.  Document templates
  73.     //  serve as the connection between documents, frame windows and views.
  74.  
  75.     CSingleDocTemplate* pDocTemplate;
  76.     pDocTemplate = new CSingleDocTemplate(
  77.         IDR_MAINFRAME,
  78.         RUNTIME_CLASS(COle_vbxDoc),
  79.         RUNTIME_CLASS(CMainFrame),     // main SDI frame window
  80.         RUNTIME_CLASS(COle_vbxView));
  81.     AddDocTemplate(pDocTemplate);
  82.  
  83.     // Connect the COleTemplateServer to the document template.
  84.     //  The COleTemplateServer creates new documents on behalf
  85.     //  of requesting OLE containers by using information
  86.     //  specified in the document template.
  87.     m_server.ConnectTemplate(clsid, pDocTemplate, TRUE);
  88.         // Note: SDI applications register server objects only if /Embedding
  89.         //   or /Automation is present on the command line.
  90.  
  91.     // Parse the command line to see if launched as OLE server
  92.     if (RunEmbedded() || RunAutomated())
  93.     {
  94.         // Register all OLE server (factories) as running.  This enables the
  95.         //  OLE 2.0 libraries to create objects from other applications.
  96.         COleTemplateServer::RegisterAll();
  97.  
  98.         // Application was run with /Embedding or /Automation.  Don't show the
  99.         //  main window in this case.
  100.         return TRUE;
  101.     }
  102.  
  103.     // When a server application is launched stand-alone, it is a good idea
  104.     //  to update the system registry in case it has been damaged.
  105.     m_server.UpdateRegistry(OAT_DISPATCH_OBJECT);
  106.     COleObjectFactory::UpdateRegistryAll();
  107.  
  108.     // Check to see that the VBX control is present
  109.     if(LoadVBXFile("ABCAUTO.VBX") > HINSTANCE_ERROR)
  110.         UnloadVBXFile("ABCAUTO.VBX");
  111.     else
  112.         {
  113.         AfxMessageBox("Cannot Load ABCAUTO.VBX\nPlace the file in your \\WINDOWS\\SYSTEM directory.",
  114.                         MB_OK | MB_ICONEXCLAMATION);
  115.         return FALSE;
  116.         }
  117.                 
  118.     // pull up the about box immediately            
  119.     OnAppAbout();                
  120.                                       
  121.        return TRUE;
  122. }
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CAboutDlg dialog used for App About
  126.  
  127. class CAboutDlg : public CDialog
  128. {
  129. public:
  130.     CAboutDlg();
  131.  
  132. // Dialog Data
  133.     //{{AFX_DATA(CAboutDlg)
  134.     enum { IDD = IDD_ABOUTBOX };
  135.     CVBControl*    m_pABCVBX;
  136.     //}}AFX_DATA
  137.     
  138.     Application m_ABCApp;                     
  139.                   
  140. // Implementation
  141. protected:
  142.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  143.     //{{AFX_MSG(CAboutDlg)
  144.     virtual BOOL OnInitDialog();
  145.     afx_msg void OnShapePal();
  146.     afx_msg void OnDoubleclicksubclassAbc1(UINT, int, CWnd*, LPVOID);
  147.     afx_msg void OnDeletesubclassAbc1(UINT, int, CWnd*, LPVOID);
  148.     afx_msg void OnHideShapePal();
  149.     afx_msg void OnShowShapePal();
  150.     //}}AFX_MSG
  151.     DECLARE_MESSAGE_MAP()
  152. };
  153.  
  154. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  155. {
  156.     //{{AFX_DATA_INIT(CAboutDlg)
  157.     m_pABCVBX = NULL;
  158.     //}}AFX_DATA_INIT
  159. }
  160.  
  161. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  162. {                              
  163.     CDialog::DoDataExchange(pDX);
  164.     //{{AFX_DATA_MAP(CAboutDlg)
  165.     DDX_VBControl(pDX, IDC_ABC1, m_pABCVBX);
  166.     //}}AFX_DATA_MAP
  167. }
  168.  
  169. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  170.     //{{AFX_MSG_MAP(CAboutDlg)
  171.     ON_VBXEVENT(VBN_DOUBLECLICKSUBCLASS, IDC_ABC1, OnDoubleclicksubclassAbc1)
  172.     ON_VBXEVENT(VBN_DELETESUBCLASS, IDC_ABC1, OnDeletesubclassAbc1)
  173.     ON_BN_CLICKED(IDC_HIDE_SHAPE_PAL, OnHideShapePal)
  174.     ON_BN_CLICKED(IDC_SHOW_SHAPE_PAL, OnShowShapePal)
  175.     //}}AFX_MSG_MAP
  176. END_MESSAGE_MAP()
  177.  
  178. // App command to run the dialog
  179. void COle_vbxApp::OnAppAbout()
  180. {
  181.     CAboutDlg aboutDlg;
  182.     
  183.     aboutDlg.DoModal();
  184. }
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187. // VB-Event registration
  188. // (calls to AfxRegisterVBEvent will be placed here by ClassWizard)
  189.  
  190. //{{AFX_VBX_REGISTER_MAP()
  191.     UINT NEAR VBN_DOUBLECLICKSUBCLASS = AfxRegisterVBEvent("DOUBLECLICKSUBCLASS");
  192.     UINT NEAR VBN_DELETESUBCLASS = AfxRegisterVBEvent("DELETESUBCLASS");
  193. //}}AFX_VBX_REGISTER_MAP
  194.  
  195. /////////////////////////////////////////////////////////////////////////////
  196. // COle_vbxApp commands
  197.  
  198. BOOL CAboutDlg::OnInitDialog()
  199. {
  200.     CDialog::OnInitDialog();
  201.     
  202.     // TODO: Add extra initialization here 
  203.     m_ABCApp.CreateDispatch("ABCFlow.application");
  204.     m_ABCApp.SetVisible(TRUE);           
  205.     
  206.     // Register the event with ABC ProcessAnalyzer 1.1          
  207.     
  208.     // NOTE: MFC included with Visual C++ 1.5 does not support 
  209.     //            LPDISPATCH VBX properties.
  210.     //
  211.     //            To allow the ABCFLOW.VBX to work with MFC some redundant properties
  212.     //            were added that are "Num"(long) properties.
  213.     //            YOU MUST cast them to (LPDISPATCH) before using them...
  214.     //            
  215.     //            App        ->        lApp
  216.     //            Chart    ->        lChart
  217.     //            Object    ->        lObject
  218.     //            FieldValue ->    lFieldValue
  219.     //            Menu    ->        lMenu
  220.     //            MenuItem ->        lMenuItem   
  221.     //            VBX         ->     lVBX
  222.     // 
  223.     LPDISPATCH lpVBXDisp = (LPDISPATCH)m_pABCVBX->GetNumProperty("lVBX");
  224.     VARIANT vEmpty;
  225.     
  226.     VariantInit(&vEmpty);
  227.     m_ABCApp.RegisterEvent(lpVBXDisp, "C++ Events Sample", "DoubleClickSUBCLASS", vEmpty);
  228.     m_ABCApp.RegisterEvent(lpVBXDisp, "C++ Events Sample", "DeleteSUBCLASS", vEmpty);
  229.                
  230.     return TRUE;  // return TRUE  unless you set the focus to a control
  231. }
  232.  
  233. void CAboutDlg::OnDoubleclicksubclassAbc1(UINT, int, CWnd*, LPVOID)
  234. {
  235.     // TODO: Add your VBX event notification handler code here
  236.     
  237.     // Setup the ABCObject
  238.     Object ABCObject;                                                               
  239.     
  240.     // You MUST call AttachDispatch with FALSE only when 
  241.     // attaching an LPDISPATCH from the ABC VBX
  242.     ABCObject.AttachDispatch((LPDISPATCH)m_pABCVBX->GetNumProperty("lObject"), FALSE); 
  243.     
  244.     // Set the passed object to Green    
  245.     ABCObject.SetColor(RGB(0,0xff,0));  
  246.     
  247.     ABCObject.SetText("C++ is easy.");
  248.     
  249.     m_pABCVBX->SetNumProperty("Override", TRUE);
  250. }
  251.  
  252. void CAboutDlg::OnDeletesubclassAbc1(UINT, int, CWnd*, LPVOID)
  253. {
  254.     // TODO: Add your VBX event notification handler code here
  255.     
  256.     // Setup the ABCChart
  257.     Chart ABCChart;
  258.     Objects ABCObjects;
  259.     Object SelectedObject; 
  260.                     
  261.     // Get the Chart that the delete occured in     
  262.                    
  263.     // You MUST call AttachDispatch with FALSE only when 
  264.     // attaching an LPDISPATCH from the ABC VBX
  265.     ABCChart.AttachDispatch((LPDISPATCH)m_pABCVBX->GetNumProperty("lChart"), FALSE); 
  266.     
  267.     // Get the Objects Collection
  268.     ABCObjects.AttachDispatch(ABCChart.GetObjects());
  269.     
  270.     do                        
  271.         {
  272.         // Get a selected object
  273.         SelectedObject.AttachDispatch(ABCObjects.ItemFromSelection());
  274.         
  275.         // Set the object to Dark Gray    
  276.         SelectedObject.SetColor(m_ABCApp.GetDk_gray());
  277.         }
  278.     while (SelectedObject.GetValid());
  279.     
  280.     m_pABCVBX->SetNumProperty("Override", TRUE);
  281. }
  282.  
  283. void CAboutDlg::OnHideShapePal()
  284. {
  285.     // TODO: Add your control notification handler code here
  286.     m_ABCApp.SetShapePaletteVisible(FALSE);
  287.     
  288.     BringWindowToTop();
  289.     GotoDlgCtrl(GetDlgItem(IDC_HIDE_SHAPE_PAL));    
  290. }
  291.  
  292. void CAboutDlg::OnShowShapePal()
  293. {
  294.     // TODO: Add your control notification handler code here
  295.     m_ABCApp.SetShapePaletteVisible(TRUE);
  296.     
  297.     BringWindowToTop();    
  298.     GotoDlgCtrl(GetDlgItem(IDC_SHOW_SHAPE_PAL));    
  299. }
  300.