COM sample (four) - client code

zhaozj2021-02-16  46

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 m_ModuleList; // overload protected: void GetMessageString (UINT nID, CString & rMessage) const; BOOL OnCommand (WPARAM wParam, LPARAM lParam); // message window protected: afx_msg int OnCreate (LPCREATESTRUCT lpCreateStruct); afx_msg void OnActivate (UINT nState, CWnd * pWndOther, BOOL bMinimized); afx_msg void OnSetFocus (CWnd * pOldWnd); afx_msg void OnInitMenuPopup (CMenu * pPopupMenu, UINT nIndex, BOOL bSysMenu); afx_msg void OnInitMenu (CMenu * pMenu); afx_msg BOOL OneRaseBkGnd (CDC * PDC); AFX_MSG Void OnClose (); // Menu Message Protected: Void OnModule; Void OnUpdateModule;

// 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 (& pTarget-> m_xInnerUnknown); ASSERT (m_pTaskManager);} // Create a component category manager and obtains an enumeration module CLSID Ienumclsid * penum = null; ictinformation * pcat = null;

CATID tempCATID [1] = {CATID_Example}; if (FAILED (CoCreateInstance (CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatInformation, reinterpret_cast (& pCat))) || FAILED (pCat-> EnumClassesOfCategories (1, tempCATID, 0, NULL, & PENUM)) {MessageBox (L "fatal error! The system is about to exit.

", 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 (GetInterface (& IID_IModuleSite)); pSite-> AddRef (); While (penum-> next (1, & clsid, null) == s_ok) { index; pstruct = new tempstruct; if (! Pstruct) {WRITESOFTERRORLOG (__line__, __wfile__, ids_outofmemory); Continue;} // Create a module object if (Failed (CLSID, NULL, CLSCTX_INPROC_SERVER, IID_IModule, reinterpret_cast (& pStruct-> m_pModule)))) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATEMODULE); delete pStruct; continue;} pStruct-> m_CLSID = clsid; // initialization module if (FAILED ( Pstruct-> m_pmodule-> initialmodule (psite, idc_module_view

index - 1))) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATEMODULE); delete pStruct; continue;} // acquisition module window if (FAILED (pStruct-> m_pModule-> QueryInterface (IID_IModuleUI, reinterpret_cast (& pUI) ))) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_GETMODULEWND); delete pStruct; continue;} if (FAILED (pUI-> GetMainWindow (& pStruct-> m_hWnd)) || pStruct-> m_hWnd) {WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_GETMODULEWND! ); Pui-> release (); delete pstruct; continue;} pui-> release (); { // modify the window style, ensuring that the module window has WS_EX_CLIENTEDGE style if (! :: SetWindowLong (pStruct-> m_hWnd, GWL_EXSTYLE, :: GetWindowLong (pStruct-> m_hWnd, GWL_EXSTYLE) | WS_EX_CLIENTEDGE)) WriteSoftErrorLog (__LINE__, __WFILE__, IDS_SETWINDOWSTYLE); } // Hide Module Window :: ShowWindow (Pstruct-> M_HWND, SW_HIDE); // Get the module's menu handle and insert into mainframe if (succeeded (pstruct-> m_pmodule->

QueryInterface (IID_IMenuUpdate, reinterpret_cast (& pMenuUp)))) {// menu module, acquiring menu handle if (FAILED (pMenuUp-> GetMenu (& pStruct-> m_hMenu))) WriteSoftErrorLog (__LINE__, __WFILE__, IDS_MAINFRM_CREATEMODULEMENU ); Pmenuup-> release ();} m_modulelist.addtail (pstruct);} psite-> release (); penum-> release (); // A module does not activate m_selected = static_cast (-1); Ulong count = m_modulelist.getCount (); if (count) {// Get the module icon and insert it into the module toolbar and menu M_WndtoolBar.setsizes (CSIZE (23, 22), CSIZE (16, 15)) ; If (! M_wndtoolbar.setButtons (null, count) WritesoftRroug (__line__, __wfile__, ids_createtoolbarbutton); Else {// Pitting the icons in the bitmap; cdc * ps creen = CDC :: FromHandle (:: GetDC (NULL)); CDC sdc; if {WriteSoftErrorLog (__LINE__, __WFILE__, (bitmap.CreateCompatibleBitmap (pScreen, ICONWIDTH * count, ICONHEIGHT) || sdc.CreateCompatibleDC (pScreen)!!) L "Create a buffer bit map failed! "); Return 0;} sdc.selectObject (& Bitmap);

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;

转载请注明原文地址:https://www.9cbs.com/read-22590.html

New Post(0)