Programming implementation of internal COM plug-ins for office2000 (2)

zhaozj2021-02-08  274

In Office applications, although the menu and toolbar buttons look less than it, they are substantially the same type of object. The Commandbars collection contains all commands in the program, such as toolbars and menu. Each Commandbars collection has a CommandBar object and it corresponds to it, and the CommandBar object can contain other CommandBar objects, which are used as a button or menu command. Every CommandBar will be referenced through the CommandBarControls object, and CommandBarControls can also include a set of CommandBarControl objects. Each CommandBarControl can contain a CommandBar object and can access control properties through it. Every CommandBarControl object is actually a collection of controls in COMMANDBarControls. CommandbarControl can have three forms:

l Pop-up (CommandBarpopup): Equivalent to a menu item in the menu bar.

l CombandBarComboBox: Similar to the combination box control in the toolbar. It includes a toolbar and a drop down arrow next to the toolbar. Click this button to display more menu commands with icons.

l Buttons (CommandBarbutton): The standard toolbar button is equivalent to the button with an icon.

In the sample program below, we will create a new toolbar in Outlook2k and add two buttons on it, and create a new menu strip in its menu Tools, which can be done in the ONCONNECTION interface.

First, we need to import the Office and Outlook type libraries in the project, you can add the following statement in the stdafx.h file (Note: where the path can be set according to the Office of the Office):

// Import project required Office2k and Outlook2k type library

#import "E: / Program files / microsoft office / office / mso9.dll" Rename_NameSpace ("Office"), named_guids

Using namespace office;

#import "E: / Program Files / Microsoft Office / Office / Msoutl9.OLB" RENAME_NAMESPACE ("Outlook"), Raw_Interfaces_only, Named_guids

Using Namespace Outlook;

Second, let's create a new toolbar in Outlook and add two buttons to it.

code show as below:

// Treatment when installing the plugin

StdMethod (iDispatch * Application, EXT_CONNECTMODE CONNECTMODE, IDISPATCH * AddInSt, SafeArray * * Custom)

{

CComptr spcmdbars;

// Outlook Application Interface_Application

CCOMQIPTR SpApp (Application);

Atlassert (SPAPP);

/ / Get the CommandBars interface

CComptr SPEXPLORER;

SpApp-> ActiveExplorer (& SPEXPLORER);

HRESULT HR = SPEXPLORER-> GET_COMMANDBARS (& SPCMDBARS); if (Failed (HR))

Return HR;

Atlassert (spcmdbars);

// Add a tool bar and two bitmap buttons

CCOMVARIANT VNAME ("Add Outlook2k Toolbar Plugin);

CComptr spnewcmdbar;

// New tool strip position

CComvariant VPOS (1);

Ccomvariant vTemp (variant_true); // temporary

CCOMVARIANT VEMPTY (DISP_E_PARAMNOTFOUND, VT_ERROR);

// Add a toolbar with the add method and let spnewcmdbar points to it.

Spnewcmdbar = spcmdbars-> add (vName, VPOS, Vempty, VTEMP);

// Get the CommandBarControls of the new toolbar so that the button is added there

CComptrols> spbarcontrols;

SPBARCONTROLS = SPNEWCMDBAR-> getControls ();

Atlassert (spbarcontrols);

// msoControlType :: msocontrolbutton = 1

Ccomvariant vtoolbartype (1);

// Display Toolbar

CCOMVARIANT VSHOW (VARIANT_TRUE);

CComptrol> SPNewBar;

CComptrol> SPNewbar2;

// Add the first button with the Add method in CommandBarControls and let spnewbar points to it

Spnewbar = spbarcontrols-> add (vtoolbartype, vempty, vempty, vempty, vshow);

Atlassert (SPNEWBAR);

// Add a second button with the Add method in CommandBarControls and let spnewbar2 point to it

Spnewbar2 = spbarcontrols-> add (vtoolbartype, vempty, vempty, vempty, vshow);

Atlassert (spnewbar2);

/ / Specify the _commandbarbutton interface for each button, can specify the display style of the button, etc.

CCOMQIPTR spcmdbutton (spnewbar);

CCOMQIPTR spcmdbutton2 (spnewbar2);

Atlassert (spcmdbutton);

Atlassert (spcmdbutton2);

/ / Set the bitmap button style, the bitmap is 32x32 size, put it in the shear board to post it on the specified button

Hbitmap HBMP = (Hbitmap) :: loadimage (_Module.getResourceInstance (),

MakeintResource (idb_bitmap), image_bitmap, 0,0, lr_loadmap3dcolors;

:: OpenClipboard (NULL);

:: EmptyClipboard ();

:: setClipboardData (cf_bitmap, (handle) HBMP;

:: closeclipboard ();

:: DeleteObject (HBMP);

// Paste the front settings display style

Spcmdbutton-> PutStyle (Office :: msobuttoniconandcaption);

HR = spcmdbutton-> pasteface ();

IF (Failed (HR))

Return HR;

SpcmdButton-> Putvisible (Variant_True);

SpcmdButton-> Putcaption (OLESTR ("button 1");

Spcmdbutton-> Putenabled (Variant_True);

SpcmdButton-> Puttooltiptext (OLESTR ("button 1 prompt information");

SpcmdButton-> Puttag (OLESTR ("Button 1 Sign");

/ / Show new toolbar

Spnewcmdbar-> putvisible (variant_true);

// Set the second toolbar button style

SPCMDButton2-> PutStyle (Office :: msobuttoniconandcaption);

// The second button indicates a bitmap that is predefined in Outlook2k in Outlook2k.

Spcmdbutton2-> PutfaceId (1760);

SpcmdButton2-> Putvisible (variant_true);

Spcmdbutton2-> Putcaption (OLESTR ("button 2"));

Spcmdbutton2-> Putenabled (variant_true);

SpcmdButton2-> Puttooltiptext (OLESTR ("button 2 prompt information");

Spcmdbutton2-> Puttag (OLESTR ("Button 2 Sign");

SpcmdButton2-> Putvisible (variant_true);

m_spButton = spcmdbutton;

m_spButton2 = spcmdbutton2;

......

Next, let's create a new menu bar in the menu "Tool".

code show as below:

_BSTR_T BSTRNEWMENUTEXT (OLESTR ("New Melody Bar"));

CComptrols> SPCMDCTRLS;

CComptrols> spcmdbarctrls;

CCIMPTR spcmdpopup;

CCIMPTR spcmdctrl;

CComptr spcmdbar;

/ / Get the Outlook main menu through CommandBar

HR = spcmdbars-> get_activemenubar (& spcmdbar);

IF (Failed (HR))

Return HR;

// Get the CommandBarControls of the menu bar

Spcmdctrls = SPCMDBAR-> getControls ();

Atlassert (spcmdctrls);

// Add a menu under the 5th "Tool" menu

CComvariant Vitem (5);

Spcmdctrl = spcmdctrls-> GetItem (VITEM);

Atlassert (spcmdctrl);

Idispatchptr SPDISP;

SPDITSP = SPCMDCTRL-> getControl (); // Get menu strip CommandBarpopup interface

CCOMQIPTR PPCMDPOPUP (SPDISP);

Atlassert (ppcmdpopup);

Spcmdbarctrls = ppcmdpopup-> getControls ();

Atlassert (spcmdbarctrls);

CCOMVARIANT VMENUTYPE (1); // Control Type - Menu

CComvariant VMenupos (6);

CCOMVARIANT VMENUEMPTY (DISP_E_PARAMNOTFOUND, VT_ERROR);

CCOMVARIANT VMENUSHOW (VARIANT_TRUE); // The menu will display

CCOMVARIANT VMENUTEMP (Variant_True); // Temporary

CComptrol> SPNewMenu;

// Create a new menu bar with the Add method

Spcmdbarctrls-> Add (vMenuType, vMenuempty, vMenuempty, vMenuempty, vMenuTemp);

Atlassert (SPNEWMENU);

SpnewMenu-> Putcaption (BSTRNEWMENUTEXT);

SpnewMenu-> Putenabled (Variant_true);

SpnewMenu-> Putvisible (Variant_True);

// Use CommandBarButton to display bitmaps before the menu bar

CCOMQIPTR spcmdmenubutton (spnewmenu);

Atlassert (spcmdmenubutton);

SpcmdMenubutton-> PutStyle (Office :: msobuttoniconandcaption);

// The first button bitmap of the new toolbar is the same method

Spcmdmenubutton-> pasteface ();

// Display menu

SpnewMenu-> Putvisible (Variant_True);

m_spmenu = spcmdmenubutton;

Thus, by loading the COM plugin by the method mentioned above in Outlook, you can see the interface effect shown in Figure 1, but there is no response when you click, let us solve this problem.

Toolbar Button CommandbarButton Distribution Interface The Response Event is _CommandbarButtonevents. ATL provides two template IdispeventImpl <> and iDispeventsImpleImpl <> to implement the receipt of the interface event, where we use iDispeventsImpleImpl (because it does not require additional type library information). It needs to set up a SINK map, callback parameter information through the _atl_sink_info structure, and eventually connect or disconnect from DISPEVENTADVISE and DISPEVENTUNADVISE. The implementation method is as follows:

1. Add iDispeventsImpleImpl inherited in the coutLookAddin inheritance, the code is as follows:

Class ATL_NO_VTABLE COUTLOOKADDIN:

Public CComobjectrootex ,

......

Public IDispeventsImpleImpl <1, coutLookAddin, & __ uuidof (office :: _ commandbarbuttonevents)> 2. Declaration _ATL_SINK_INFO structure callback parameter information. Add the following statement in the OutlookAddin.h file:

// Button event response information declaration

EXTERN _TL_FUNC_INFO OnClickButtonInfo;

Add a definition statement in the OutlookAddin.cpp file, as follows:

// Button event response information definition

_ATL_FUNC_INFO onClickButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {vt_dispatch, vt_byref | vt_bool}};

3. Join the SINK mapping, as follows:

EGIN_SINK_MAP (coutLookAddin)

SINK_ENTRY_INFO (1, __UUIDOF (Office :: _ commandbarbuttonevent, / * dispid * / 0x01, onclickbutton1, & onclickButtonInfo)

SINK_ENTRY_INFO (2, __UUIDOF (Office :: _ commandbarbuttonevents), / * DISPID * / 0X01, OnClickButton2, & OnClickButtonInfo)

SINK_ENTRY_INFO (3, __UUIDOF (Office :: _ commandbarbuttonevents), / * Dispid * / 0x01, onclickmenu, & onclickButtonInInfo)

END_SINK_MAP ()

4. Add events. Add a statement in OutlookAddin.h:

Void __stdcall onclickButton1 (idispatch * / * office :: _ commandbarbutton ** / ctrl, variant_bool * canceldefault);

Join the implementation in OutlookAddin.cpp:

// Toolbar Button 1 Click the event response

Void __stdcall coutlookaddin :: OnClickButton1 (idispatch * / * office :: _ commandbarbutton * * / ctrl, variant_bool * cancelDefault)

{

Uses_Conversion;

CCOMQIPTR PCommandbarbutton (Ctrl);

Hinstance Result = SHELLEXECUTE (NULL, _T ("Open"), _T ("http://www.vckbase.com"), null, null, sw_show;

}

5. Finally, open or disconnect the connection to the interface. Methods as below:

l On the last part of the ONCONNECTION interface, add the following code to open the connection:

CommandButton1events :: DispeventadVise ((idispatch *) m_spButton);

l In the ONDISCONNECTION interface, add the following code to disconnect:

CommandButton1 Events :: Dispeventunadvise (IDSPATCH *) M_SPButton;

Here, complete the minimum demand for an Office internal plugin, you can open the Outlook2000 to see how the Outlook2000 looks, detailed code can see the sample source code belonging in the article, there is a detailed note. references:

Building an office2k com addressin with vc / ATL - Amit Dey

ATL Development Guide (Second Edition) - Tom Armstrong & Ron Patton

Contact information:

Editor Email: jingzhou_xu@163.com

Future Studio (Future Studio)

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

New Post(0)