Windows Mobile 中输入法改变事件的捕获
在Windows Mobile开发中遇到一个问题,就是在用户改变输入法时动态改变窗体的高度,而输入法控件InputPanel中没有输入法改变的事件,因些要对输入法改变的事件进行捕获。在网上搜索了到了相关的代码,但是有一些问题,作了一些修改后可以正常使用。基本原理是对InputPanel进行扩展,通过消息捕获添加输入法改变的事件InputMethodChanged。代码如下:
/// <summary>
/// InputPanel扩展类。
/// LastUpdate:2007-12-26 NSnaiL
/// </summary>
public class InputPanelEx : InputPanel
{
#region Fields
/// <summary>
/// 当输入法切换时发生。
/// </summary>
public event EventHandler InputMethodChanged;
private MsgHook Msg;
internal class MsgHook : MessageWindow
{
#region Fields
private const int WM_SETTINGCHANGE = 0x001A;
private InputPanelEx BaseObject;
#endregion
#region Methods
public MsgHook(InputPanelEx baseObj)
{
BaseObject = baseObj;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETTINGCHANGE)
BaseObject.InputMethodChanged(this, EventArgs.Empty);
base.WndProc(ref m);
}
#endregion
}
#endregion
#region Methods
public InputPanelEx()
{
Msg = new MsgHook(this);
}
public new void Dispose()
{
Msg.Dispose();
base.Dispose();
}
#endregion
}
/// InputPanel扩展类。
/// LastUpdate:2007-12-26 NSnaiL
/// </summary>
public class InputPanelEx : InputPanel
{
#region Fields
/// <summary>
/// 当输入法切换时发生。
/// </summary>
public event EventHandler InputMethodChanged;
private MsgHook Msg;
internal class MsgHook : MessageWindow
{
#region Fields
private const int WM_SETTINGCHANGE = 0x001A;
private InputPanelEx BaseObject;
#endregion
#region Methods
public MsgHook(InputPanelEx baseObj)
{
BaseObject = baseObj;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETTINGCHANGE)
BaseObject.InputMethodChanged(this, EventArgs.Empty);
base.WndProc(ref m);
}
#endregion
}
#endregion
#region Methods
public InputPanelEx()
{
Msg = new MsgHook(this);
}
public new void Dispose()
{
Msg.Dispose();
base.Dispose();
}
#endregion
}
需要注意的是,在退出窗体时一定要调用InputPanelEx的Dispose()方法,否则会出问题。