公司一个大型的项目中使用了 DELPHI 来开发 OCX 控件做数据库的管理,但是GIS图形系统使用 MFC 开发,最后将 OCX 嵌入到 MFC 程序中作为一个完整的系统交付用户使用,但是在使用过程中发现一个问题:当启动 DELPHI 开发的管理程序使用 OCX 时, "剪贴、复制、黏贴"的快捷键都是好使的,但是当 MFC 程序加载 OCX 后,OCX 中的文本框只能使用右键菜单来完成这些事情,使用快捷键毫无反应(当然对于 MFC 中的文本框是好使,即使没有设置加速键表),几经尝试,最终灵光一闪突然想到了一个简单的方法,经过测试该问题已经解决,代码如下;
BOOL CAppEntry::PreTranslateMessage(MSG* pMsg)
{
int Key = pMsg->wParam ; UINT Msg = pMsg->message ;
if( Msg == WM_KEYDOWN && ::GetFocus( ) && ::GetAsyncKeyState( VK_CONTROL ) )
{
if( Key == 'Z' ){ ::SendMessage( ::GetFocus( ) , WM_UNDO , 0 , 0 ); return TRUE; }
if( Key == 'X' ){ ::SendMessage( ::GetFocus( ) , WM_CUT , 0 , 0 ); return TRUE; }
if( Key == 'C' ){ ::SendMessage( ::GetFocus( ) , WM_COPY , 0 , 0 ); return TRUE; }
if( Key == 'V' ){ ::SendMessage( ::GetFocus( ) , WM_PASTE, 0 , 0 ); return TRUE; }
}
return CWinApp::PreTranslateMessage(pMsg);
}
{
int Key = pMsg->wParam ; UINT Msg = pMsg->message ;
if( Msg == WM_KEYDOWN && ::GetFocus( ) && ::GetAsyncKeyState( VK_CONTROL ) )
{
if( Key == 'Z' ){ ::SendMessage( ::GetFocus( ) , WM_UNDO , 0 , 0 ); return TRUE; }
if( Key == 'X' ){ ::SendMessage( ::GetFocus( ) , WM_CUT , 0 , 0 ); return TRUE; }
if( Key == 'C' ){ ::SendMessage( ::GetFocus( ) , WM_COPY , 0 , 0 ); return TRUE; }
if( Key == 'V' ){ ::SendMessage( ::GetFocus( ) , WM_PASTE, 0 , 0 ); return TRUE; }
}
return CWinApp::PreTranslateMessage(pMsg);
}
需要注意的是:在 WM_KEYDOWN 消息中的字符都是大写的,小写字母是在 WM_CHAR 中结合 CAPS LOCK 和 SHIFT 按键的状态转换出来的,所以上面代码中的按键判断只需要判断大写字母即可(实际情况中小写字母是不会进来的)