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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds