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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds