Article ID: 189779
Article Last Modified on 1/8/2003
#include <afxtempl.h>
// Information about child controls in the Visual Basic
// ActiveX control.
typedef struct xcontrol_info
{
HWND hwnd; // handle of child control
char mnemonic_char; // mnemonic character key for the control
}
XCONTROL_INFO;
class CTestVBDlg : public CDialog
{
...
// Array of info for child controls.
CArray<XCONTROL_INFO, XCONTROL_INFO> m_XControlInfo;
...
}
// Callback function for EnumChildWindows() API
BOOL CALLBACK EnumActiveXControlChildProc(HWND hwnd, LPARAM lParam)
{
CArray<XCONTROL_INFO, XCONTROL_INFO>* arr =
(CArray<XCONTROL_INFO, XCONTROL_INFO>*)lParam;
CString str;
CWnd* control = CWnd::FromHandle(hwnd);
control->GetWindowText(str);
int index = str.Find('&'); // look for mnemonic
// character keycode
if (index > -1)
{
XCONTROL_INFO info;
info.hwnd = hwnd;
info.mnemonic_char = str[index+1];
arr->Add(info);
}
return TRUE;
}
BOOL CTestVBDlg ::OnInitDialog()
{
CDialog::OnInitDialog();
...
// Setup a callback function for EnumChildWindows() API to
// enumerate all child windows of the ActiveX control.
HWND hcontrol = GetDlgItem(IDC_VB_CONTROL1)->GetSafeHwnd();
EnumChildWindows(hcontrol, EnumActiveXControlChildProc,
(LPARAM) &m_XControlInfo);
return TRUE;
}
void CTestVBDlg ::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
// If user pressed an accelerator keystroke, loop through the
// array to find out if it is one of the access keys for a
// child control in the Visual Basic ActiveX control.
BOOL trap_accel = nID == SC_KEYMENU;
if (trap_accel)
{
int size = m_XControlInfo.GetSize();
for (int i = 0; i < size; i++)
{
if (toupper(LOWORD(lParam)) ==
toupper(m_XControlInfo[i].mnemonic_char))
{
HWND child = m_XControlInfo[i].hwnd;
::SetFocus(child);
::SendMessage(child, BM_CLICK, 0, 0L); // OPTIONAL!!!
}
}
}
if (!trap_accel)
CDialog::OnSysCommand(nID, lParam);
}
}
145698 PRB: Mnemonic Differences Between MFC 4.0 and Visual Basic 4.0
Additional query words: ocx kbOLE kbActiveX kbCtrl kbVBp500bug kbdsd kbMFC kbDSupport
Keywords: kbbug kbpending KB189779