HRESULT CMyAddin::InstallInterface(IExchExtCallback *lpExchangeCallback) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); try { CString csTitle = APP_NAME; // This will be the display-name of the toolbar HBITMAP hBmp; // First we need to get the active Explorer, you could see this as the active window (it's more to it but let's keep it at that for now) Outlook::_ExplorerPtr spExplorer = m_OLAppPtr->ActiveExplorer(); if (spExplorer == NULL) { return S_FALSE; } // Then we need to have a pointer to the commandbars, that is the toolbars in the UI Office::_CommandBarsPtr spCmdBars = spExplorer->CommandBars; if (spCmdBars == NULL) { return S_FALSE; } try { CComVariant vName(csTitle); // This is the caption of the toolbar CComVariant vTemp(TRUE); CComPtr <Office::CommandBar> spToolbar; CComVariant vPos(1); //CComVariant vTemp(VARIANT_TRUE); CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR); /*spToolbar = spCmdBars->Add(vName, // Caption vtMissing, // <ignore this for now> vtMissing, // <ignore this for now> vTemp); // The toolbar should be temporary, // that way we have to add it every time we*/ //spToolbar = spCmdBars->Add(vName, // Caption // vPos, // <ignore this for now> // vEmpty, // <ignore this for now> // vTemp); // The toolbar should be temporary, // that way we have to add it every time we CComPtr <Office::CommandBarControl> pButton = NULL; CComPtr <Office::CommandBarControls> pBarControls = NULL; //pBarControls = spToolbar->GetControls(); /*m_pToolBar = (Office::CommandBarControlPtr)(pBarControls->Add((long)Office::msoBarFloating, //msoControlButton, vtMissing, vtMissing, vtMissing, vTemp));*/ // get the outlok Context Menu. for this enumerate all the command bars for "Context Menu" command bar CComPtr<Office::CommandBar> pTempBar; for(long i = 0; i < spCmdBars->Count ; i++) { CComVariant vItem(i+1); //zero based index pTempBar=NULL; spCmdBars->get_Item(vItem,&pTempBar); if((pTempBar) && (!wcscmp(CString(APP_NAME).AllocSysString(),pTempBar->Name))) { return S_FALSE; } } m_pToolBar = (Office::CommandBarPtr)(spCmdBars->Add(APP_NAME, vPos, /*Office::msoBarFloating,*/ vtMissing, vTemp)); pButton = (Office::CommandBarControlPtr)m_pToolBar->Controls->Add(Office::msoControlButton, vtMissing, vtMissing, vtMissing, vTemp); pButton->Caption = APP_NAME; pButton->OnAction = "MyButtonsAction"; BOOL bSetImage = TRUE; // I've set this to FALSE, but if you want an image to appear on the button, // you will need to have an image in the resources. // The only drawback of this is that there is only one way of // putting an image on the button, and that is by going through the // clipboard. And by doing so, the contents of the clipboard will be // lost, unless you implement some logic to store the clipboard-data // before doing this and then restoring the data once the image is on // the button. if (bSetImage) { UINT uiBitmapId = 0; CComQIPtr <Office::_CommandBarButton> spCmdButton(pButton); // 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 // Outlook's set of predefined bitmap, set button's FaceId to // the button whose bitmap you want to use hBmp =(HBITMAP)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_PHONE), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_LOADTRANSPARENT); if (hBmp) { // put bitmap into Clipboard ::OpenClipboard(NULL); ::EmptyClipboard(); ::SetClipboardData(CF_BITMAP, (HANDLE)hBmp); ::CloseClipboard(); ::DeleteObject(hBmp); // set style before setting bitmap spCmdButton->PutStyle(Office::msoButtonIconAndCaption); if (FAILED(spCmdButton->PasteFace())) { TRACE("Failed to paste face to button\n"); } }/* else { char serr[50]; DWORD err = GetLastError(); ltoa(err, serr, 10); MessageBox(NULL, serr, "error", MB_OK); }*/ } // With the button physically added to the toolbar, we just need to // create an object of COutlookButton to sink it's events. m_pMyButton = new COutlookButton(pButton); m_pMyButton->m_OLAppPtr = m_OLAppPtr; // The only thing left to do is to make the toolbar visible. //spToolbar->PutVisible(VARIANT_TRUE); LoadToolbarSettings(); } catch(...) { } // Add menu item _bstr_t bstrNewMenuText(CString(APP_NAME).AllocSysString()); CComPtr < Office::CommandBarControls> spCmdCtrls; CComPtr < Office::CommandBarControls> spCmdBarCtrls; CComPtr < Office::CommandBarPopup> spCmdPopup; CComPtr < Office::CommandBarControl> spCmdCtrl; CComPtr < Office::CommandBar> spCmdBar; // get CommandBar that is Outlook's main menu HRESULT hr = spCmdBars->get_ActiveMenuBar(&spCmdBar); if (FAILED(hr)) return S_FALSE; //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 = spCmdCtrl->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 should 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); spNewMenu->OnAction = "MyButtonsAction"; //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(); // Empty whatever we put in the clipboard ::OpenClipboard(NULL); ::EmptyClipboard(); ::CloseClipboard(); // With the button physically added to the toolbar, we just need to // create an object of COutlookButton to sink it's events. m_pMyMenuItem = new COutlookButton(spNewMenu); m_pMyMenuItem->m_OLAppPtr = m_OLAppPtr; // show the menu spNewMenu->PutVisible(VARIANT_TRUE); } catch(...) { } return S_FALSE; }
INT_PTR CALLBACK OptionsDlgProc( _In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam ) { switch (uMsg) { case WM_INITDIALOG: { HWND toolbarCombo = GetDlgItem(hwndDlg, IDC_DISPLAYSTYLECOMBO); HWND searchboxCombo = GetDlgItem(hwndDlg, IDC_SEARCHBOX_DISPLAYSTYLECOMBO); ComboBox_AddString(toolbarCombo, L"No Text Labels"); // Displays no text label for the toolbar buttons. ComboBox_AddString(toolbarCombo, L"Selective Text"); // (Selective Text On Right) Displays text for just the Refresh, Options, Find Handles and Sysinfo toolbar buttons. ComboBox_AddString(toolbarCombo, L"Show Text Labels"); // Displays text labels for the toolbar buttons. ComboBox_SetCurSel(toolbarCombo, PhGetIntegerSetting(SETTING_NAME_TOOLBARDISPLAYSTYLE)); ComboBox_AddString(searchboxCombo, L"Always show"); ComboBox_AddString(searchboxCombo, L"Hide when inactive"); //ComboBox_AddString(searchboxCombo, L"Auto-hide"); ComboBox_SetCurSel(searchboxCombo, PhGetIntegerSetting(SETTING_NAME_SEARCHBOXDISPLAYMODE)); Button_SetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_TOOLBAR), PhGetIntegerSetting(SETTING_NAME_ENABLE_TOOLBAR) ? BST_CHECKED : BST_UNCHECKED); Button_SetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_SEARCHBOX), PhGetIntegerSetting(SETTING_NAME_ENABLE_SEARCHBOX) ? BST_CHECKED : BST_UNCHECKED); Button_SetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_STATUSBAR), PhGetIntegerSetting(SETTING_NAME_ENABLE_STATUSBAR) ? BST_CHECKED : BST_UNCHECKED); Button_SetCheck(GetDlgItem(hwndDlg, IDC_RESOLVEGHOSTWINDOWS), PhGetIntegerSetting(SETTING_NAME_ENABLE_RESOLVEGHOSTWINDOWS) ? BST_CHECKED : BST_UNCHECKED); } break; case WM_COMMAND: { switch (GET_WM_COMMAND_ID(wParam, lParam)) { case IDCANCEL: EndDialog(hwndDlg, IDCANCEL); break; case IDC_CUSTOMIZETOOLBAR: PostMessage(ToolBarHandle, TB_CUSTOMIZE, 0, 0); break; case IDOK: { PhSetIntegerSetting(SETTING_NAME_TOOLBARDISPLAYSTYLE, (DisplayStyle = (TOOLBAR_DISPLAY_STYLE)ComboBox_GetCurSel(GetDlgItem(hwndDlg, IDC_DISPLAYSTYLECOMBO)))); PhSetIntegerSetting(SETTING_NAME_SEARCHBOXDISPLAYMODE, (SearchBoxDisplayMode = (SEARCHBOX_DISPLAY_MODE)ComboBox_GetCurSel(GetDlgItem(hwndDlg, IDC_SEARCHBOX_DISPLAYSTYLECOMBO)))); PhSetIntegerSetting(SETTING_NAME_ENABLE_TOOLBAR, (EnableToolBar = Button_GetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_TOOLBAR)) == BST_CHECKED)); PhSetIntegerSetting(SETTING_NAME_ENABLE_SEARCHBOX, (EnableSearchBox = Button_GetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_SEARCHBOX)) == BST_CHECKED)); PhSetIntegerSetting(SETTING_NAME_ENABLE_STATUSBAR, (EnableStatusBar = Button_GetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_STATUSBAR)) == BST_CHECKED)); PhSetIntegerSetting(SETTING_NAME_ENABLE_RESOLVEGHOSTWINDOWS, Button_GetCheck(GetDlgItem(hwndDlg, IDC_RESOLVEGHOSTWINDOWS)) == BST_CHECKED); LoadToolbarSettings(); InvalidateRect(ToolBarHandle, NULL, TRUE); EndDialog(hwndDlg, IDOK); } break; } } break; } return FALSE; }