#pragma once // EditEx.h : header file // / // CEditEx window class CEditEx : public CEdit { // Construction public: CEditEx(); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CEditEx) protected: virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); //}}AFX_VIRTUAL // Implementation public: virtual ~CEditEx(); // Generated message map functions protected: //{{AFX_MSG(CEditEx) afx_msg void OnSetfocus(); afx_msg void OnKillfocus(); //}}AFX_MSG DECLARE_MESSAGE_MAP() protected: HKL m_DefaultHKL; HKL m_Saved_KbdLayout; HKL GetDefault_KbdLayout(); public: BOOL bLockKbdLayout; //锁定输入法 }; // EditEx.cpp : implementation file // #include "stdafx.h" #include "EditEx.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CEditEx CEditEx::CEditEx() { m_DefaultHKL = GetDefault_KbdLayout(); m_Saved_KbdLayout = 0; bLockKbdLayout = TRUE; } CEditEx::~CEditEx() { } BEGIN_MESSAGE_MAP(CEditEx, CEdit) //{{AFX_MSG_MAP(CEditEx) ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus) ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus) //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CEditEx message handlers void CEditEx::OnSetfocus() { // TODO: Add your control notification handler code here if(m_DefaultHKL) { HKL CurKbdLayout = GetKeyboardLayout(0); if(CurKbdLayout != m_DefaultHKL) { m_Saved_KbdLayout = CurKbdLayout; //保存输入法 if(ActivateKeyboardLayout(m_DefaultHKL, 0) == 0) //设置为默认输入法 { TRACE( _T("Set ActivateKeyboardLayout Err=%d"), GetLastError()); } } } } void CEditEx::OnKillfocus() { // TODO: Add your control notification handler code here if(m_Saved_KbdLayout) //恢复输入法 { if(ActivateKeyboardLayout(m_Saved_KbdLayout, 0) == 0) TRACE( _T("Restore ActivateKeyboardLayout Err=%d"), GetLastError()); m_Saved_KbdLayout = 0; } } HKL CEditEx::GetDefault_KbdLayout() { HKL HKL_Ret= 0; int nSize = GetKeyboardLayoutList(0 , 0); if(nSize != 0) { HKL FAR * lpList = (HKL FAR *)new HKL[nSize]; ZeroMemory(lpList, sizeof(HKL) * nSize); if(GetKeyboardLayoutList(nSize, lpList) == nSize) { for(int i=0; i<nSize; i++) { HKL Val = lpList[i]; if(HIWORD(Val) == LOWORD(Val)) HKL_Ret = Val; TRACE( _T("0x%08X/n"), Val); } } delete []lpList; } return HKL_Ret; } LRESULT CEditEx::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class if(WM_INPUTLANGCHANGEREQUEST == message) //输入法改变 { if(bLockKbdLayout) //锁定输入法 return 0; m_Saved_KbdLayout = 0; //接受该变且不需要恢复输入法 } return CEdit::WindowProc(message, wParam, lParam); }