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

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds