CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Corporate Gifts
Shop Online
Televisions
Promote Your Website
Calling Cards
Disney World Tickets
Web Hosting Directory
Server Racks
GPS
Compare Prices
Promos and Premiums
PDA Phones & Cases
Car Donations
Imprinted Promotions


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> Internet & Networking >> IE Programming

Becoming a Better Project Manager: Best Practices, Tips, Strategies & More. Download Exclusive eBook Now.

How to Modify/Remove Right-Click Menu in IE WebBrowser Control
Rating:

Santhosh Cheeran (view profile)
July 29, 2002

Environment: VC6 SP4, NT4 SP5, WIN 2000/98/95
(continued)



Turbo Screen Sharing
Adobe Acrobat Connect Professional offers users the ability to have a more productive and engaging web conferencing experience while providing the IT department with a program that efficiently utilizes bandwidth and minimally impacts the infrastructure. Learn More! »

Informal Learning: Extending the Impact of Enterprise Ideas and Information
Forward-thinking organizations are turning to enterprise learning in their quest to be better informed, better skilled, better supported at the point of need, and more competitive in their respective marketplaces. Learn More! »

Rapid E-Learning: Maturing Technology Brings Balance and Possibilities
Rapid e-learning addresses both time and cost issues by using technology tools to shift the dynamics of e-learning development. Learn why more skilled learning professionals use these tools and how you can get a solution to keep pace with your business demands. »

Delivering on the Promise of ELearning
This white paper defines the framework to launch e-learning as a set of teaching, training, and learning practices not bound by a specific technology platform or learning management system. It offers practical suggestions for creating digital learning experiences that engage learners by building interest and motivation and providing opportunities for active participation. »

Here is an easy solution for hiding/replacing the right-click menu on a Web browser window. An example would be my situation. I had to implement a news group reader and writer. The news reader/writer used IwebBrowser2 control as the message display/edit window. If you do a right-click on it, there is an option View Source,.. other menu items. If I had an option, I want to act like I implemented everything from scratch, but the right-click menu put a hole in my plan and revealed that I reused the IE control. Besides, anyway, we didn't want our users to mess around with HTML source code, and so forth, or expose some of them to brain damage from seeing real weird stuff while all they asked for was some news group item. The Web browser captures the right-click and shows the default IE right click menu. So how do I remove it?

I trap the PreTranslateMessage function of the application class (CWinApp derived class). In case of a right click, I will see if it is a Web browser control which generated the message, and if so, I display our menu. Here is our example.

BOOL CWebBrowserTestApp::PreTranslateMessage(MSG* pMsg)
{
  // We have a right click
  if (  (WM_RBUTTONDOWN == pMsg->message) ||
    (WM_RBUTTONDBLCLK == pMsg->message))
  {
    // Now verify it is from our Web browser
    CWebBrowserTestDlg* pWBDlg =
                     (CWebBrowserTestDlg*)AfxGetMainWnd();
    if(pWBDlg->isThisOurBrowser(pMsg->hwnd))
    {
      // If so, show our menu!
      pWBDlg->showOurMenu();
      // mark as handled
      return TRUE;
    }

  return CWinApp::PreTranslateMessage(pMsg);
}

Now that I have trapped the right click, I will have to ask the window that owns the Web browser control to see if the window that got the right click is a Web browser window. It is a little twitchy here. I will have to specifically go case by case, meaning I will have to check, if not this Web browser, the other usage instance of webbrowser and other (meaning if a dialog box had three controls which were Web browsers, I might have a CWnd m_WndBrowser1, CWnd m_WndBrowser2, and CWnd m_WndBrowser3). In the attached example, I have a dialog with one webbrowser control in it. I have a member variable for the Web browser window as follows.

CWnd m_WndBrowser

My function will have to do a little bit more than just matching the window handles. Why so? A nested frame inside IE is again an instance of IE. HTMLs having nested frames will return a different window handle for different frames inside the same parent IE window. So what will be my logic?! I will have to traverse via GetParent from a frame to the parent IE window, whose window handle I already know or till I don't have a parent of type IE. How do we know if it's an IE Window? The Window class name will be "Internet Explorer_Server." That's what the next function does, see if our window is an Internet Explorer, if so we know, the outer shell window will be the parent of doc-view which will be the parent of the IE class window.

// This fn checks if this is a IE type window and returns
// the outer hosting Shell window handle for it, if it is
// one! else NULL
HWND CWebBrowserTestDlg::getParentWebBrowserWnd(HWND hMyWnd)
{
  HWND   hRetWBWnd = NULL;

  static TCHAR tszIEClass[] = "Internet Explorer_Server";
  TCHAR   tszClass[sizeof(tszIEClass)+1];

  // Compare if our window is of type IE class
  ::GetClassName( hMyWnd,tszClass,sizeof(tszClass));
  if(0 == _tcscmp(tszClass, tszIEClass))
  {
   // Then get the Shell which hosts the IE doc view window
     hRetWBWnd = ::GetParent(::GetParent(hMyWnd));
  }
  return hRetWBWnd;
}

// Checks if message window is our WebBrowser control
// and if so returns TRUE
BOOL CWebBrowserTestDlg::isThisOurBrowser(HWND hMyWnd)
{
  HWND hWndOurWebBrowser = (HWND)m_WndBrowser.GetSafeHwnd();
  // Get the Parent Web browser window if any
  HWND hParentWBWnd = getParentWebBrowserWnd(hMyWnd);

  // Now, we can have nested frames ..
  // meaning a frame inside a frame is yet another webbrowser
  // So our m_WndBrowser guy can be way up there ..
  // so we have to iterate up to him through parent frames
  // till we have a match or no more WB type parent
  while(  (NULL != hParentWBWnd) &&
    (hParentWBWnd != hWndOurWebBrowser))
  {
    // Get the Parent of the Web browser window
    //I have, recursively
    hParentWBWnd =
           getParentWebBrowserWnd(::GetParent(hParentWBWnd));
  }
  return (NULL != hParentWBWnd);
}

Now the next function is a normal function to show a popup menu from a resource and I am not going to explain it. People who need explanation, please read the help file for TrackPopupMenu.

// This is a trivial fn which gets a submenu from a resource
// and displays it at the current cursor position
void CWebBrowserTestDlg::showOurMenu()
{
  POINT  CurPoint;
  GetCursorPos(&CurPoint);

  CMenu  ReplaceMenu;
  //Make your custom menu here
  ReplaceMenu.LoadMenu(IDR_RIGHTCLICK);
  // Do we have a menu loaded?
  if(::IsMenu(ReplaceMenu.m_hMenu))
  {
    // this is my popup menu
    CMenu* pMyPopup = ReplaceMenu.GetSubMenu(0);
    if(NULL != pMyPopup)
    {
      // Show it
      pMyPopup->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
                                CurPoint.x,
                                CurPoint.y, this );
      // deleting me and my parent when I am done
      pMyPopup->DestroyMenu();
      ReplaceMenu.DestroyMenu();
    }
  }
}

Downloads

Download demo project - 16.6 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Becoming a Better Project Manager. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance. Learn more.
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Improve How You Manage Information: Becoming a Better Project Manager. Exclusive eBook - Download Now.


RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
For Win32 - prabharr (11/04/2004)
I like it - Sanhan (03/07/2004)
How to block pop-ups - Legacy CodeGuru (07/08/2003)
Any C# versions? - Legacy CodeGuru (06/05/2003)
My solution - Legacy CodeGuru (02/06/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers