Using a Hot Key in an ActiveX Control

For a normal executable, you use RegisterHotKey and WM_HOTKEY message handlers to add hotkey features, but the same code does not work in an ActiveX control. Others tell you that there is no message pump for an ActiveX control, so WM_HOTKEY doesn't work. I have seen some implementations geting around the problem by WH_GETMESSAGE hook (Q187988 PRB: ActiveX Control Is the Parent Window of Modeless Dialog. Here I give you another way, by creating a hiden modeless dialog that enables a message pump to enable hotkeys without a Windows hook. Why a dialog? Other top-level windows also have a message pump, but a modeless dialog does not require an additional thread for the message loop.

#pragma once


// CHotkeyDlg dialog

class CHotkeyDlg : public CDialog
{
   DECLARE_DYNAMIC(CHotkeyDlg)

public:
   CHotkeyDlg(CWnd* pParent = NULL);   // standard constructor
   virtual ~CHotkeyDlg();

// Dialog Data
   enum { IDD = IDD_ABOUTBOX_MFCACTIVEXHOTKEY };

protected:
   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV
                                                       // support

   DECLARE_MESSAGE_MAP()
public:
   virtual BOOL OnInitDialog();
   afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);
   afx_msg void OnDestroy();
   LRESULT OnHotKey(WPARAM wParam, LPARAM lParam);
protected:
   virtual void PostNcDestroy();
};

// HotkeyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfcactivexhotkey.h"
#include "HotkeyDlg.h"
#include ".\hotkeydlg.h"


// CHotkeyDlg dialog

IMPLEMENT_DYNAMIC(CHotkeyDlg, CDialog)
CHotkeyDlg::CHotkeyDlg(CWnd* pParent /*=NULL*/)
   : CDialog(CHotkeyDlg::IDD, pParent)
{
}

CHotkeyDlg::~CHotkeyDlg()
{
}

void CHotkeyDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CHotkeyDlg, CDialog)
   ON_WM_WINDOWPOSCHANGING()
   ON_MESSAGE(WM_HOTKEY, OnHotKey)
   ON_WM_DESTROY()
END_MESSAGE_MAP()


// CHotkeyDlg message handlers

BOOL CHotkeyDlg::OnInitDialog()
{
   CDialog::OnInitDialog();

   // TODO:  Add extra initialization here
   ATOM aHotKey=GlobalAddAtom("F11");
   BOOL bret = RegisterHotKey(m_hWnd,aHotKey,MOD_CONTROL,VK_F11);
   if(bret)
      OutputDebugString("RegisterHotKey return OK");
   else
      OutputDebugString("RegisterHotKey return false");
   return TRUE;    // return TRUE unless you set the focus to
                   // a control
   // EXCEPTION: OCX Property Pages should return FALSE
}

void CHotkeyDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
   if (lpwndpos->flags & SWP_SHOWWINDOW) {
      lpwndpos->flags &= ~SWP_SHOWWINDOW;
      PostMessage(WM_WINDOWPOSCHANGING, 0, (LPARAM)lpwndpos);
      ShowWindow(SW_HIDE);
   }
   else
      CDialog::OnWindowPosChanging(lpwndpos);
}

void CHotkeyDlg::OnDestroy()
{
   ATOM aHotKey=GlobalAddAtom("F11");
   UnregisterHotKey(m_hWnd,aHotKey);
   CDialog::OnDestroy();
}

LRESULT CHotkeyDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
   AfxMessageBox("hotkey");
   return LRESULT(0);
}

void CHotkeyDlg::PostNcDestroy()
{
   CDialog::PostNcDestroy();
   delete this;
   OutputDebugString(__FUNCTION__);
}

Add a member to your control as. Create the modeless dialog in the constructor of the ActiveX:

CmfcactivexhotkeyCtrl::CmfcactivexhotkeyCtrl()
{
   InitializeIIDs(&IID_Dmfcactivexhotkey,
                  &IID_DmfcactivexhotkeyEvents);
   // TODO: Initialize your control's instance data here.
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

   m_pHotKeyWnd = NULL;
   m_pHotKeyWnd = new CHotkeyDlg(0);
   m_pHotKeyWnd->Create(IDD_ABOUTBOX_MFCACTIVEXHOTKEY,
                           GetDesktopWindow());
}

Destroy the dialog in the destructor of the ActiveX:

// CmfcactivexhotkeyCtrl::~CmfcactivexhotkeyCtrl -
// Destructor CmfcactivexhotkeyCtrl::~CmfcactivexhotkeyCtrl() {
   if(m_pHotKeyWnd)
   {
      m_pHotKeyWnd->SendMessage(WM_CLOSE);
      m_pHotKeyWnd->SendMessage(WM_DESTROY);
      m_pHotKeyWnd->SendMessage(WM_NCDESTROY);
      OutputDebugString(__FUNCTION__);
   }
}

I saw someone posted a thread in www.csdn.net asking the question about how to use a hot key in an ActiveX control. I answered that question and gave the solution above. It may be useful for others too, so I'm posting it here. I have an MFC project if you prefer source code to textbook. Note, it is created with VC2003, on Windows 2003.



Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds