Menu in ActiveX

转载 2005年05月06日 20:17:00

Environment: VC6, Windows 2000

It took me some time to figure out how to enable menu in an ATL/MFC ActiveX control and I think the result is simple but instructive. You simply have to change the style of the window of the AX control. ActiveX is the 3rd generation of COM (Basic COM and Automation are the 1st and 2nd) and it added support to the following weeknesses of its predeccessors: lack of user interface, standardized storage, custom events, optimization for the Internet. Basic COM and Automation servers could display and use dialog-boxes but not through the COM system. Being an elementary thing in a user interface, menu is very important.


Let's create a simple ATL AX Control:

Start Visual C++ and select File/New/ATL COM AppWizard, specify a location and let the project name be ATLMenu. Click OK then click Finish then OK. Now you will insert a Full Control through the menu items Insert/New ATL Object. Select Controls and Full Control, click Next. In the 'Short Name' box enter MyMenu then click on the Miscellaneous tab and click 'Windowed only' then click OK. Now select Insert/Resource/Menu from the VC's menu and click New. Type 'File' then 'MsgBox' in the menu items and ID_MSGBOX as the identifier for the 'MsgBox' menuitem. Choose the ClassView tab of th Workspace window and right click on CMyMenu and select 'Add Windows Message Handler'. Expand the tree if necessary. Select WM_CREATE then click on Add Handler, and OK. Now select the FileView tab of the Workspace window and expand the tree until MyMenu.h. Double click on it and locate the OnCreate function and type:

LRESULT OnCreate( UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled)
long lStyle;
HMENU hMenu;

lStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);

lStyle &= WS_DISABLED;
lStyle |= WS_CAPTION;

::SetWindowLong(m_hWnd, GWL_STYLE, lStyle);

hMenu = ::LoadMenu( _Module.GetModuleInstance(),
::SetMenu(m_hWnd, hMenu);

return 0;

Now you will add the code to handle the message sent by the menu item. Locate the BEGIN_MSG_MAP macro and alter it like this (COMMAND_ID_HANDLER):


Now add the handler function too and you are ready to compile:

LRESULT OnMsgBox( WORD wNotifyCode,
HWND hWndCtl,
BOOL& bHandled)
::MessageBox( m_hWnd,
"Hello World!",
"From Menu",
return 0;
Select Build/Set Active Configuration and set it to Release MinDependency and click OK. Now Build your AX Control by selecting Build/Build ATLMenu.dll. The Wizard automatically generated a MyMenu.html file in your project's folder, double click on it and test your work.

It's interesting that you can drag your AX Control by its caption and move it. You can prevent dragging by overriding WM_NCLBUTTONDOWN:

LRESULT OnNcLButtonDown( UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled)
// TODO : Add Code for message handler.
// Call DefWindowProc if necessary.
if (HTCAPTION == (INT)wParam)
return 1;
::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
return 0;

You can manipulate your Control by using the interfaces derived from IOleWindow. Add IOleInPlaceObject::SetObjectRects to your Control's class because this will be called by the Container as a response to call to IOleInPlaceSite::OnPosRectChange by your Control or when the user makes changes to the Container's window. When neccessary call OnPosRectChange which is implemented by the Container from your Control(m_spInPlaceSite->OnPosRectChange(&rc) where m_spInPlaceSite is a pointer to the Container's IOleInplaceSite interface).

LPCRECT prcClip)
//Call ::SetWindowPos Win32 API function
return NOERROR;


It's almost the same as in ATL but it's much easier because of the powerful ClassWizard:

Select File/New/MFC ActiveX Control Wizard and just click next and OK everywhere. Insert menu resource and add menu items. Use ClassWizard to add WM_CREATE, and the menu item handler code to the project. Add the code needed to OnCreate function: see the ATL way above but change from HMENU to CMenu etc or use the Win32 API functions just like in the case of ATL above. Finally add AfxMessageBox("Hello World!") to your menuitem handler code. Build and Register your AX Control(Tools/Register Control menu). The Wizard won't generate .html file this time, so you have to write it or create an MFC AppWizard(exe) container project and test it there.

It's interesting that if you test your MFC control in IE then the window acts like a child of the Control(You can drag it only in the Control's client area), but it has a menu. It's strange because as far as I know a child window can't have a menu. But if you test it in a dialog-based MFC container it works fine.

You should override the WM_NCLBUTTONDOWN message like this in case of MFC:

void CMFCMenuCtrl::OnNcLButtonDown( UINT nHitTest,
CPoint point)
if (HTCAPTION == nHitTest)
COleControl::OnNcLButtonDown(nHitTest, point);

The pointer to an IOleInPlaceSite interface is m_pInPlaceSite in case of MFC.

Adding Custom Actions to the List Item Menu in SharePoint 2010 Using SharePoint Designer 2010[Sharepoint中添加自定义菜单]
  • SseJava
  • SseJava
  • 2011年01月18日 14:04
  • 1133

ActiveX Support In SWT

Summary OLE Documents, such as Word, Excel or PowerPoint, and ActiveX Controls such as Internet Exp...

How to change the Text color of Menu item in Android?

How to change the Text color of Menu item in Android? up vote2down votefavori...

How to Activate the Drop Down Menu in MOSS 2010 Sites

MOSS 2010 sites can display drop down menus in the tabbed navigation across the top.  Unfortunately ...

How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单

How to create custom navigation menu in SharePoint with XML data source Did you ever wanted to crea...

ControlTemplate in WPF —— Menu

Creating an ActiveX Control in Visual Studio 2005

Published Wed, Aug 16 2006 6:26 | Paul June There have been misconceptions on technologies upon ...

ActiveX Script in MFC

  • 2008年12月21日 19:35
  • 45KB
  • 下载
您举报文章:Menu in ActiveX