如何在Edit中切换为默认输入法

vc 专栏收录该内容
34 篇文章 0 订阅

     从CEdit继承一个新类,再将他子类化到IDC_EDIT2控件上,即

 

     CMyEdit2 m_MyEdit2;

     m_MyEdit2.SubclassDlgItem(IDC_EDIT2,this);

 

 

     新类CMyEdit2

 

 

    .h文件

 

#if !defined(AFX_MYEDIT2_H__62F117FB_70C8_4CA6_AFF7_C182A9E0C2C2__INCLUDED_)
#define AFX_MYEDIT2_H__62F117FB_70C8_4CA6_AFF7_C182A9E0C2C2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyEdit2.h : header file
//

/
// CMyEdit2 window

//在激活输入时切换输入法为默认输入法


class CMyEdit2 : public CEdit
{
// Construction
public:
 CMyEdit2();

// Attributes
public:

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CMyEdit2)
 protected:
 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMyEdit2();

 // Generated message map functions
protected:
 //{{AFX_MSG(CMyEdit2)
 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;        //锁定输入法

};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYEDIT2_H__62F117FB_70C8_4CA6_AFF7_C182A9E0C2C2__INCLUDED_)

.cpp文件

 

 

// MyEdit2.cpp : implementation file
//

#include "stdafx.h"

#include "MyEdit2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CMyEdit2

CMyEdit2::CMyEdit2()
{
 m_DefaultHKL = GetDefault_kbdLayout();
 m_Saved_kbdLayout = 0;
 bLockkbdLayout = TRUE;
}

CMyEdit2::~CMyEdit2()
{
}


BEGIN_MESSAGE_MAP(CMyEdit2, CEdit)
 //{{AFX_MSG_MAP(CMyEdit2)
 ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
 ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CMyEdit2 message handlers

LRESULT CMyEdit2::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 if (WM_INPUTLANGCHANGEREQUEST == message)
 {
  if(bLockkbdLayout)     //锁定输入法
   return 0;
  m_Saved_kbdLayout = 0;     //接受改变且不需要恢复输入法
 }
 
 return CEdit::WindowProc(message, wParam, lParam);
}

void CMyEdit2::OnSetfocus()
{
 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 CMyEdit2::OnKillfocus()
{
 if (m_Saved_kbdLayout)        //恢复输入法
 {
  if (ActivateKeyboardLayout(m_Saved_kbdLayout,0) == 0)
  {
   TRACE(_T("Set ActivateKeyboardLayout Err=%d"),GetLastError());
   m_Saved_kbdLayout = 0;
  }
 }
 
}
HKL CMyEdit2::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;
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值