COM sample (four)
- Client code
This article is the last article of this series of articles, the source code of the Russen client. The client uses the MFC implementation, where the framework class CMAINFRAME implements Imodulesite, IerrorReport, and ICompanyInfo, and the other window wrapper CTaskManager implements ItaskManager, and is aggregated by CMAInframe to exhibit CMAInframe implementation ITaskManager. Due to the longer code, this article is only the definition of CMAINFRAME :: OnCreate and CMAINFrame, which implements an instance of the acquisition department component (recorded by the COM component category function, not by registry), and management. If you want to see all the code of this example, you can download it at the end of the article. The executable of this example is also available in the text. Since this instance is just a result in a program that I have previously edited, there may be a lot of childish places in the code. I am not interested in re-revision, just roughly trimming, please forgive me.
CMAINFRAME definition
#include "NewStatusBar.h" class CMainFrame: public CFrameWnd {// MFC define macros DECLARE_DYNCREATE (CMainFrame) DECLARE_MESSAGE_MAP () DECLARE_INTERFACE_MAP () // auxiliary structure private: struct TEMPSTRUCT {HWND m_hWnd; HMENU m_hMenu; IModule * m_pModule; CLSID m_CLSID; Tempstruct (): m_hwnd (null), m_hmenu (null), m_pmodule (null) {// nothing} ~ Tempstruct () {SafeRelese (m_pmodule);}}; // Construction, destructure public: cMAINFRAME () ; ~ Cmainframe (); // Operation Public: Void UpdateErrorState () // Update the error flag on the status bar to indicate {Assert_Valid (this); if (M_WndstatusBar.getsafehwnd ()) M_WndstatusBar.invalide () ;} // helper protected: // according to the menu item ID to determine whether the frame is the basic menu commands BOOL BeMenuOfBase (DWORD nID) const; // member variable protected: CNewStatusBar m_wndStatusBar; CToolBar m_wndToolBar; DWORD m_Selected; long m_OldViewID; IUnknown * m_ptaskmanager; c TypedPtrList
// custom message protected: afx_msg LRESULT OnAllTaskTerminated (WPARAM, LPARAM); // interface maps public: // IModuleSite BEGIN_INTERFACE_PART (ModuleSite, IModuleSite) INIT_INTERFACE_PART (CMainFrame, ModuleSite) STDMETHOD (ChangeModule) (REFCLSID clsid, WCHAR * pModuleName, ULONG command, ULONG param); STDMETHOD (GetFrameWindow) (HWND * pHwnd); END_INTERFACE_PART_STATIC (ModuleSite) // IErrorReport BEGIN_INTERFACE_PART (ErrorReport, IErrorReport) INIT_INTERFACE_PART (CMainFrame, ErrorReport) STDMETHOD (ReportSoftError) (WCHAR * fileName, ULONG row, WCHAR * errorString ); Stdmethod (Wchar * filename, ulong row, wchar * errorstring); end_interface_part_static (errorreport) // iCompanyInfo Begin_INTERF ACE_PART (CompanyInfo, ICompanyInfo) INIT_INTERFACE_PART (CMainFrame, CompanyInfo) STDMETHOD (GetDataServerInfo) (WCHAR ** pLoaction, WCHAR ** pServer, WCHAR ** pPassword); END_INTERFACE_PART_STATIC (CompanyInfo)}; CMainFrame :: OnCreate Code
int
CMainFrame :: OnCreate (LPCREATESTRUCT lpCreateStruct) {if (CFrameWnd :: OnCreate (lpCreateStruct) == -1) return -1; // create a basic tool bar and status bar EnableDocking (CBRS_ALIGN_ANY);! If (m_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, CRect (0, 0, 0, 0))) WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATETOOLBAR); else {m_wndToolBar.EnableDocking (CBRS_ALIGN_ANY); DockControlBar (& m_wndToolBar) ;} If (! M_wndstatusbar.create (this) ||! M_wndstatusbar.setindicators (INDICATORS, SIZEOF (INDICATORS) / SIZEOF (WRITESOFTERRORLOG (__line__, __wfile__, IDS_MAINFRM_CREATESTATUSBAR); return -1;} else {CCmdTarget * pTarget = m_wndStatusBar.Initial (); if (pTarget) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATESTATUSBAR);! Return -1;} ASSERT_VALID (pTarget); // set the task management pointing device such that the polymerization CMainFrame CTaskManager pTarget-> m_pOuterUnknown = GetControllingUnknown (); m_pTaskManager = reinterpret_cast
CATID tempCATID [1] = {CATID_Example}; if (FAILED (CoCreateInstance (CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatInformation, reinterpret_cast
", g_syscaption); WriteharderRroug (__line__, __wfile__, ids_killingerror); SafeRelease (PCAT); return -1;} PCAT-> Release (); // enumerate each module information g_theapp.beginwaitcursor (); CLSID clsid; CStringW temp; TEMPSTRUCT * pStruct = NULL; IModuleUI * pUI = NULL; IMenuUpdate * pMenuUp = NULL; long index = -1; IModuleSite * pSite = static_cast
index - 1))) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATEMODULE); delete pStruct; continue;} // acquisition module window if (FAILED (pStruct-> m_pModule-> QueryInterface (IID_IModuleUI, reinterpret_cast
QueryInterface (IID_IMenuUpdate, reinterpret_cast
SDC.FillSolidRect (0, 0, iconwidth * count, iconheight, rgb (0xc0, 0xc0, 0xc0); hicon hicon = null; ulong i = 0; position pos = m_modulelist.getHeadPosition (); cmenu * pmenu = getMenu () -> getSubmenu (0) -> getSubmenu (0); assert_valid (pmenu); // Empty the original placeholder While (pmenu-> deletemenu (0, mf_byposition)); while (POS) {pstruct = m_ModuleList.getNext ( POS); assert (pstruct); // Put the icon to the bitmap IF (succeeded (pstruct-> m_pmodule-> geticon (& HiCon))) :: Drawiconex (sdc.getsafehdc (), i * iconwidth, 0, Hicon, Iconwidth, iconheight, 0, NULL, DI_NORMAL, ELSE WRITESOFTERRORLOG (__LINE__, __WFILE__, IDS_MAINFRM_GETMODULICON); // Add Menu {// Get Module Name Wchar * IO = NULL; if (Failed (pstruct-> m_pmodule-> getname (& IO))) WritesoftErrorLog (__line__, __wfile__, ids_mainfrm_getmodulen;