Use VC ++ ATL to develop the Office com plug-in (2)

Source: Internet
Author: User
7. Add menus and buttons to the plug-in:

Here is a brief introduction to the office command bar: in office, the menu bar, toolbar, and pop-up menu are all called command bar objects ). All these types of command lines can contain other command lines and unlimited controls. For example, a button is its control relative to the command bar on the toolbar. Some controls (such as menus) are command lines and can be nested at multiple levels. All command lines can be controlled by a commandbars set. The commandbars set is a common shared and programmable object. You can use its add () method to add a commandbar object (a command bar) to it ), each commandbar object has a commandbarcontrols set, which contains all the controls in this command bar. You can use the CONTROLS attribute of the commandbar object to reference controls in the command bar.

Add a toolbar, buttons, and a menu to word:

First, add the Office and Word type libraries to the project, and add the following code to the stdafx. h file:

# Import "C:/program files/Microsoft Office/office/mso9.dll "/
Rename_namespace ("Office") named_guids
Using namespace office;

# Import "C:/program files/Microsoft Office/office/msoutl9.olb"
Rename_namespace ("word"), raw_interfaces_only, named_guids
Using namespace outlook;
Note: You must change the installation path to the same as that of the Office.

In the word object model, the application object represents the highest level object of the entire project. We can use its getcommandbars method to obtain the commandbars object. Because the commandbars object is a set of all the tool bar and menu items of word, so you can add a new toolbar by calling its add method. Then add a new button to the toolbar. In fact, the method is as simple as that. We can call the getcontrols method of commandbar to obtain the commandbarcontrols set of the toolbar. As mentioned above, the commandbarcontrols set is a set of all controls of the toolbar. The buttons are naturally one of them. Then, we can call the Add method of the commandbarcontrols set to add a new button. The specific implementation code is as follows:

Ccomqiptr <_ Application> spapp (application );

Atlassert (spapp );
M_spapp = spapp;
Hresult hR = appevents: dispeventadvise (m_spapp );

If (failed (HR ))
Return hr;

Ccomptr <Office: _ commandbars> spcmdbars;
Ccomptr <Office: commandbar> spcmdbar;
HR = m_spapp-> get_commandbars (& spcmdbars );

If (failed (HR ))
Return hr;

Atlassert (spcmdbars );

// Now we add a new toolband to word
// To which we ''ll add 2 buttons
Ccomvariant vname ("wordaddin ");
Ccomptr spnewcmdbar;

// Position it below all toolbands
// Msobarposition: msobartop = 1
Ccomvariant VPOs (1 );
Ccomvariant vtemp (variant_true); // menu is temporary
Ccomvariant vempty (disp_e_paramnotfound, vt_error );

// Add a new toolband through add Method
// Vmenutemp holds an unspecified Parameter
// Spnewcmdbar points to the newly created toolband
Spnewcmdbar = spcmdbars-> Add (vname, VPOs, vempty, vtemp );

// Now get the toolband's commandbarcontrols
Ccomptr <Office: commandbarcontrols> spbarcontrols;
Spbarcontrols = spnewcmdbar-> getcontrols ();
Atlassert (spbarcontrols );

// Msocontroltype: msocontrolbutton = 1
Ccomvariant vtoolbartype (1 );

// Show the toolbar?
Ccomvariant vshow (variant_true );
Ccomptr <Office: commandbarcontrol> spnewbar;
Ccomptr <Office: commandbarcontrol> spnewbar2;

// Add first button
Spnewbar = spbarcontrols-> Add (vtoolbartype, vempty, vshow );
Atlassert (spnewbar );

// Add 2nd button
Spnewbar2 = spbarcontrols-> Add (vtoolbartype, vempty, vshow );
Atlassert (spnewbar2 );

_ Bstr_t bstrnewcaption (olestr ("Item1 "));
_ Bstr_t bstrtiptext (olestr ("tooltip for Item1 "));

// Get commandbarbutton interface for Each toolbar button
// So we can specify button styles and stuff
// Each button displays a bitmap and caption next to it
Ccomqiptr <Office: _ commandbarbutton> spcmdbutton (spnewbar );
Ccomqiptr <Office: _ commandbarbutton> spcmdbutton2 (spnewbar2 );

Atlassert (spcmdbutton );
M_spbutton = spcmdbutton;
Atlassert (spcmdbutton2 );

// To set a bitmap to a button, load a 32x32 bitmap
// And copy it to clipboard. Call commandbarbutton's pasteface ()
// To copy the bitmap to the button face. To use
// Word's set of predefined bitmap, set button's faceid to //
// Button whose bitmap you want to use
Hbitmap hbmp = (hbitmap): LoadImage (_ module. getresourceinstance (),
Makeintresource (idb_bitmap1), image_bitmap, 0, 0, lr_loadmap3dcolors );

// Put bitmap into clipboard
: Openclipboard (null );
: Emptyclipboard ();
: Setclipboarddata (cf_bitmap, (handle) hbmp );
: Closeclipboard ();
: Deleteobject (hbmp );

// Set style before setting bitmap
Spcmdbutton-> putstyle (Office: msobuttoniconandcaption );
HR = spcmdbutton-> pasteface ();
If (failed (HR ))
Return hr;

Spcmdbutton-> putvisible (variant_true );
Spcmdbutton-> putcaption (olestr ("Item1 "));
Spcmdbutton-> putenabled (variant_true );
Spcmdbutton-> puttooltiptext (olestr ("tooltip for Item1 "));
Spcmdbutton-> puttag (olestr ("tag for Item1 "));
Spcmdbutton2-> put_onaction (olestr ("mymacro1 "));

// Show the toolband
Spnewcmdbar-> putvisible (variant_true );
Spcmdbutton2-> putstyle (Office: msobuttoniconandcaption );

// Specify predefined bitmap
Spcmdbutton2-> putfaceid (1758 );
Spcmdbutton2-> putvisible (variant_true );
Spcmdbutton2-> putcaption (olestr ("item2 "));
Spcmdbutton2-> putenabled (variant_true );
Spcmdbutton2-> puttooltiptext (olestr ("tooltip for item2 "));
Spcmdbutton2-> puttag (olestr ("tag for item2 "));
Spcmdbutton2-> putvisible (variant_true );
Spcmdbutton2-> put_onaction (olestr ("mymacro2 "));

The process of adding a menu item is similar to that of adding a menu item:

First, call the get_activemenubar () method of the commandbars set to obtain a commandbar object, which represents the active menu in the current project, and then call getcontrols of the commandbar to obtain the control set of the current menu. Try to add a new menu item to the "format" menu (5th menus) of word, and call the getitem (5) of commandbarcontrols to obtain the required "format" menu item, it is also a commandbarcontrols set (as mentioned above, the control set can be nested), so that you can add a new menu item by calling its add method. The specific implementation code is as follows:

_ Bstr_t bstrnewmenutext (olestr ("new menu item "));
Ccomptr <Office: commandbarcontrols> spcmdctrls;
Ccomptr <Office: commandbarcontrols> spcmdbarctrls;
Ccomptr <Office: commandbarpopup> spcmdpopup;
Ccomptr <Office: commandbarcontrol> sp1_ctrl;

// Get commandbar that is word's main menu
HR = spcmdbars-> get_activemenubar (& spcmdbar );
If (failed (HR ))
Return hr;

// Get menu as commandbarcontrols
Spcmdctrls = spcmdbar-> getcontrols ();
Atlassert (spcmdctrls );

// We want to add a menu entry to outlook's 6th (tools) menu // item
Ccomvariant vitem (5 );
Spcmdctrl = spcmdctrls-> getitem (vitem );
Atlassert (spcmdctrl );

Idispatchptr spdisp;
Spdisp = spdisctrl-> getcontrol ();

// A commandbarpopup interface is the actual menu item
Ccomqiptr <Office: commandbarpopup> ppcmdpopup (spdisp );
Atlassert (ppcmdpopup );
Spcmdbarctrls = ppcmdpopup-> getcontrols ();
Atlassert (spcmdbarctrls );

Ccomvariant vmenutype (1); // type of control-menu
Ccomvariant vmenupos (6 );
Ccomvariant vmenuempty (disp_e_paramnotfound, vt_error );
Ccomvariant vmenushow (variant_true); // menu shocould be visible
Ccomvariant vmenutemp (variant_true); // menu is temporary

Ccomptr <Office: commandbarcontrol> spnewmenu;

// Now create the actual menu item and add it
Spnewmenu = spcmdbarctrls-> Add (vmenutype, vmenuempty, vmenuempty,
Vmenuempty, vmenutemp );
Atlassert (spnewmenu );
Spnewmenu-> putcaption (bstrnewmenutext );
Spnewmenu-> putenabled (variant_true );
Spnewmenu-> putvisible (variant_true );

// We 'd like our new menu item to look cool and display
// An icon. Get menu item as a commandbarbutton
Ccomqiptr <Office: _ commandbarbutton> spcmdmenubutton (spnewmenu );
Atlassert (spcmdmenubutton );
Spcmdmenubutton-> putstyle (Office: msobuttoniconandcaption );

// We want to use the same toolbar bitmap for menuitem too.
// We grab the commandbarbutton interface so we can add
// A bitmap to it through pasteface ().
Spcmdmenubutton-> pasteface ();

// Show the menu
Spnewmenu-> putvisible (variant_true );

Return s_ OK;
Then, run the program and you can see that the added buttons and menu items have already appeared in word. You also need to add a response event for them to truly extend the word function through the plug-in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.