Click here to Skip to main content
15,891,033 members
Articles / Web Development / HTML
Article

Invoke Hidden Commands in Your WebBrowser

Rate me:
Please Sign up or sign in to vote.
4.77/5 (16 votes)
11 Sep 20042 min read 133.6K   2.3K   50   25
Demonstrates an innovative way to invoke hidden commands to show modal dialogs such as "Add To Favorite" dialog, "Import/Export Wizard" dialog in your WebBrowser-based application.

IE Window Class Names

Demo

Introduction

Generally, when hosting the WebBrowser Control in our MFC application, we call ExecWB method of m_pBrowserApp (IWebBrowser2 interface) to invoke common web browser commands (zoom the browser, select all text in the browser, etc.).

We also invoke queryInterface to get an IDispatch pointer for the IOleCommandTarget interface and call its Exec method to invoke the "Find in This Page" dialog or "View Source" command. But there still exists some dialogs we can't invoke, i.e. the "Add To Favorite" dialog, the "Import/Export Wizard" dialog. Though we can invoke "AddFavorite" command and "ImportExportFavorites" command through IShellUIHelper interface, the former command results in a modeless "Add To Favorite" dialog independent from the application mainframe while the latter command results in a quite simple consequence.

This article will introduce an innovative but simple way to invoke the MODAL "Add To Favorite" dialog and MODAL "Import/Export Wizard" dialog in your own web browser.

Background

The idea comes from the IDocHostUIHandler::ShowContextMenu demo of "WebBrowser Customization" in the MSDN. The IDocHostUIHandler::ShowContextMenu demo presents us the way to manually build IE's context menu from the correlative resource file "SHDOCLC.DLL" and remove "View Source" from it. The key point of the code is after popping up the context menu, it calls SendMessage() API to send the MenuItem ID returned by TrackPopupMenu() to the "Internet Explorer_Server" window through WM_COMMAND message. So, if we get the MenuItem ID of the "Add To Favorite" command or the "Import/Export Wizard" command, we may probably solve the problems stated above.

......
// Show shortcut menu
int iSelection = ::TrackPopupMenu(hMenu,
                                  TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
                                  ppt->x,
                                  ppt->y,
                                  0,
                                  hwnd,
                                  (RECT*)NULL);
// Send selected shortcut menu item command to shell
LRESULT lr = ::SendMessage(hwnd, WM_COMMAND, iSelection, NULL);
......

Using the code

Some command messages are handled by the "Internet Explorer_Server" window while some others are handled by its parent "Shell DocObject View" window. Therefore, the first thing is to get the Window Handle of the two windows from your CHtmlView. To simplify the resolution, I borrow the cute class CFindWnd below from Paul DiLascia.

////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
//        CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
    //////////////////
    // This private function is used with EnumChildWindows to find the child
    // with a given class name. Returns FALSE if found (to stop enumerating).
    //
    static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
        CFindWnd *pfw = (CFindWnd*)lParam;
        HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
        if (hwnd) {
            pfw->m_hWnd = hwnd;    // found: save it
            return FALSE;            // stop enumerating
        }
        EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
        return TRUE;                // keep looking
    }

public:
    LPCSTR m_classname;            // class name to look for
    HWND m_hWnd;                    // HWND if found

    // ctor does the work--just instantiate and go
    CFindWnd(HWND hwndParent, LPCSTR classname)
        : m_hWnd(NULL), m_classname(classname)
    {
        FindChildClassHwnd(hwndParent, (LPARAM)this);
    }
};

void CDemoView::InvokeShellDocObjCommand(int nID)
{
    CFindWnd FindIEWnd( m_wndBrowser.m_hWnd, "Shell DocObject View");
    ::SendMessage( FindIEWnd.m_hWnd, WM_COMMAND, 
                        MAKEWPARAM(LOWORD(nID), 0x0), 0 );
}

void CDemoView::InvokeIEServerCommand(int nID)
{
    CFindWnd FindIEWnd( m_wndBrowser.m_hWnd, "Internet Explorer_Server");
    ::SendMessage( FindIEWnd.m_hWnd, WM_COMMAND, 
                        MAKEWPARAM(LOWORD(nID), 0x0), 0 );
}

Command IDs handled by the "Internet Explorer_Server" window:

#define ID_IE_CONTEXTMENU_ADDFAV        2261
#define ID_IE_CONTEXTMENU_VIEWSOURCE    2139
#define ID_IE_CONTEXTMENU_REFRESH       6042

Command IDs handled by the "Shell DocObject View" window:

#define ID_IE_FILE_SAVEAS               258
#define ID_IE_FILE_PAGESETUP            259
#define ID_IE_FILE_PRINT                260
#define ID_IE_FILE_NEWWINDOW            275
#define ID_IE_FILE_PRINTPREVIEW         277
#define ID_IE_FILE_NEWMAIL              279
#define ID_IE_FILE_SENDDESKTOPSHORTCUT  284
#define ID_IE_HELP_ABOUTIE              336
#define ID_IE_HELP_HELPINDEX            337
#define ID_IE_HELP_WEBTUTORIAL          338
#define ID_IE_HELP_FREESTUFF            341
#define ID_IE_HELP_PRODUCTUPDATE        342
#define ID_IE_HELP_FAQ                  343
#define ID_IE_HELP_ONLINESUPPORT        344
#define ID_IE_HELP_FEEDBACK             345
#define ID_IE_HELP_BESTPAGE             346
#define ID_IE_HELP_SEARCHWEB            347
#define ID_IE_HELP_MSHOME               348
#define ID_IE_HELP_VISITINTERNET        349
#define ID_IE_HELP_STARTPAGE            350
#define ID_IE_FILE_IMPORTEXPORT         374
#define ID_IE_FILE_ADDTRUST             376
#define ID_IE_FILE_ADDLOCAL             377
#define ID_IE_FILE_NEWPUBLISHINFO       387
#define ID_IE_FILE_NEWCORRESPONDENT     390
#define ID_IE_FILE_NEWCALL              395
#define ID_IE_HELP_NETSCAPEUSER         351
#define ID_IE_HELP_ENHANCEDSECURITY     375

The following code demonstrates how to invoke the MODAL "Add To Favorite" dialog.

void CDemoView::OnFavAddtofav()
{
    InvokeIEServerCommand(ID_IE_CONTEXTMENU_ADDFAV);
}

Points of Interest

You know, to keep working all the night, and finally you find the answer. I call this happiness!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Software Engineer

Comments and Discussions

 
GeneralGOOD Pin
Aric Wang3-Apr-10 0:04
Aric Wang3-Apr-10 0:04 
Questionhow to prevent webbrowser flicker Pin
Member 31028362-Sep-08 18:53
Member 31028362-Sep-08 18:53 
GeneralCannot send WM_KEYDOWN and WM_KEYUP message in "Internet Explorer_Server" window using SendMessage(...) Pin
bk_pasa4-Jul-07 17:53
bk_pasa4-Jul-07 17:53 
GeneralFlicker while sizing Pin
Euegene27-Mar-07 3:34
Euegene27-Mar-07 3:34 
GeneralApplet Pin
colin-12312-Sep-06 23:38
colin-12312-Sep-06 23:38 
GeneralWindows exlorer's command ids Pin
kdsasi23-Aug-06 0:51
kdsasi23-Aug-06 0:51 
GeneralRe: Windows exlorer's command ids Pin
Zhang Shuo28-Aug-06 4:06
Zhang Shuo28-Aug-06 4:06 
GeneralRe: Windows exlorer's command ids Pin
kdsasi4-Sep-06 19:55
kdsasi4-Sep-06 19:55 
GeneralMore COMMAND IDs Pin
JaeWook Choi16-Jun-05 8:55
JaeWook Choi16-Jun-05 8:55 
First of all, thank you for the article.

I have just found your article and surprised since I did very similar thing during last year. I wrote IE mouse gesture BHO add-in and I wanted to be able to assign and execute all those IE's menu commands from a specifc mouse gesture. From the very same article in MSDN I knew that I could send WM_COMMAND message to execute those menu command as well and figured out that all the IE menu command IDs can be found in "browselc.dll" (located in windows system folder). Open browselc.dll as resource in VC then Menu ID 267 is the IE's default menu. Most of menu command ID from the resource worked well when it was sent to either "IEFrame" or "Shell DocObject View" as WPARAM of WM_COMMAND message but some of those commands only worked for "IEFrame" (those were found to be mostly IE specific commands).

Addition to this, I also found that context menu command ID are located in "shdoclc.dll" (windows system folder as well). Menu ID 24641 is the IE's default context menu (right-click menu). Executing context menu is somewhat different from doing for regular menu item as it only worked under certain context (i.e. clicked on image). Menu 24641 has several submenus (Default, Image, Control, Anchor and so on). The most of the command ID from "Default" submenu seemed to work well for "Internet Explorer_Server" just by sending menu command ID through WM_COMMAND message. But menu IDs from the other submenu didn't work in the same manner. After quite digging into news group, I found solution. As you addressed in your article, menu command can be executed in two methods. The first is sending WM_COMMAND message to target window with specific menu command ID and the second is using IOleCommandTarget::Exec(). I was able to execute context menu directly for the image and anchor tags using IOleCommandTarget::Exec() but it was very important to focus the target html element before I invoked Exec().

So here is a portion of code I used to execute context menu command directly (and programmatically) on the specific HTMLelement(image or anchor).

CComQIPtr<iolecommandtarget> spCmd( spElementTarget );
if( !spCmd )
spCmd = spHTMLDoc;

if( !spCmd )
return E_FAIL;

spElementTarget->focus();

spCmd->Exec( &CGID_MSHTML, wMenuID, OLECMDEXECOPT_DODEFAULT, NULL, NULL );

spElementTarget->blur();

I hope this help someone!

Jae
General"Find in This Page" Pin
voncris3-Feb-05 15:08
voncris3-Feb-05 15:08 
GeneralRe: &quot;Find in This Page&quot; Pin
voncris6-Feb-05 13:42
voncris6-Feb-05 13:42 
Questionexit command? Pin
dapeng lin6-Dec-04 1:05
professionaldapeng lin6-Dec-04 1:05 
AnswerRe: exit command? Pin
eagleboost7-Dec-04 15:52
eagleboost7-Dec-04 15:52 
AnswerRe: exit command? Pin
kwon985730-Oct-07 1:39
kwon985730-Oct-07 1:39 
GeneralHi Pin
Prakash Nadar19-Sep-04 5:25
Prakash Nadar19-Sep-04 5:25 
GeneralRe: Hi Pin
eagleboost19-Sep-04 6:28
eagleboost19-Sep-04 6:28 
GeneralRe: Hi Pin
Prakash Nadar19-Sep-04 8:11
Prakash Nadar19-Sep-04 8:11 
GeneralThanks for posting Pin
Zuoliu Ding17-Sep-04 18:51
Zuoliu Ding17-Sep-04 18:51 
GeneralModal Print &amp; Find dlgs Pin
Ionut FIlip13-Sep-04 18:31
Ionut FIlip13-Sep-04 18:31 
GeneralRe: Modal Print &amp; Find dlgs Pin
eagleboost13-Sep-04 21:58
eagleboost13-Sep-04 21:58 
GeneralRe: Modal Print &amp; Find dlgs Pin
Ionut FIlip13-Sep-04 22:35
Ionut FIlip13-Sep-04 22:35 
GeneralRe: Modal Print &amp; Find dlgs Pin
eagleboost14-Sep-04 4:10
eagleboost14-Sep-04 4:10 
GeneralRe: Modal Print &amp; Find dlgs Pin
Ionut FIlip14-Sep-04 6:06
Ionut FIlip14-Sep-04 6:06 
Generalexcellent Pin
.dan.g.12-Sep-04 13:59
professional.dan.g.12-Sep-04 13:59 
GeneralRe: excellent Pin
eagleboost12-Sep-04 16:27
eagleboost12-Sep-04 16:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.