如何在自定义的控件中使用输入法
1.需要将Control.ImeMode设置为On
2.处理OnKeyPress
public class EditorControl : System.Windows.Forms.Control
{
public EditorControl()
{
//开启输入法模式
this.ImeMode = System.Windows.Forms.ImeMode.On;
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
//将Text显示出来
System.Windows.Forms.TextRenderer.DrawText(e.Graphics,
this.Text,
new System.Drawing.Font("微软雅黑",30),
new System.Drawing.Point(0,0),
System.Drawing.Color.Black);
}

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
this.Text += e.KeyChar;
this.Invalidate();
e.Handled = true;//注意这里,如果这里没设置,输入中文会重复。
}
}
2.处理OnKeyPress
public class EditorControl : System.Windows.Forms.Control
{
public EditorControl()
{
//开启输入法模式
this.ImeMode = System.Windows.Forms.ImeMode.On;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
//将Text显示出来
System.Windows.Forms.TextRenderer.DrawText(e.Graphics,
this.Text,
new System.Drawing.Font("微软雅黑",30),
new System.Drawing.Point(0,0),
System.Drawing.Color.Black);
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
this.Text += e.KeyChar;
this.Invalidate();
e.Handled = true;//注意这里,如果这里没设置,输入中文会重复。
}
}

浙公网安备 33010602011771号