C#下實現空白窗體上中文輸入
關鍵字:類PS的文字工具,中文輸入.重複截取中文
最近在研究做一個類PS的文字工具N多的資料,問了N多個人,總算功夫不負有人心.終於給弄出來了.寫出來給大家一起討論.
在空白窗體上打開輸入法,在C#裡的空白視窗是無論如何也是打不開輸入法.設定了this.ImeMode= ImeMode.NoControl也是無法開啟輸入法的錄影視窗.去Microsoft 開發論壇上問了一些問題.感謝週雪峰版主與Riquel_Dong 版主給的指點.用了API函數: ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);終於把輸入法給調了出來,它的功能是把輸入與指定的視窗進行關聯.
:
f (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)
ImmAssociateContext(this.Handle, m_hImc );
}
case WM_IME_CHAR:
sb.Append(str.To MessageBox.Show( str.ToString());
ble and the application passes this message to DefWindowProc
是不是問題就出在這裡了.是發送訊息兩次的問題.
方案:加上判斷
if (m.WParam.ToInt32() == PM_REMOVE)
測試.終於沒有問題了
代碼貼文上
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;Windows.Forms;
IntPtr m_hImc;
bool isShowChina = false;
private const int WM_IME_CHAR = 0x0286;
private const int WM_IME_COMPOSITION = 0x010F;
[DllImport (" Imm32.dll")]
public static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll")]
public static extern IntPtr ImmAssociateContext(IntPtr hndIM)Int [DllImport(" imm32 .dll")]
static extern int ImmGetCompositionString( IntPtr hIMC, int dwIndex, StringBuilder lpBuf, int dwBufLen);
public UserControl1()
{
}
{
m_hI mc = ImmGetContext(this.Handle);
受保護訊息m) { base.WndProc(ref m);if == 1) { ImmAssociateContext(this.Handle, m_hImc ) ;
}
case WM_ CHAR:
對應 Key | ModifierKeys);
char a = (char)e.KeyData; // sb.Append(a);
isShowChina = false;
break;
if (m.WParam.ToInt32() == PM_REMOVE ) //如果不做這個判斷,會印出重複的中文
{
int size = Imm GetCompositionString(m_hImc, GCS_COMPSTR, null, 0); sizeof (Char);
ImmGetCompositionString(m_hI sb.Append(str.ToString());
MessageBox.Show intoText();
isShowChina = true;
break;
///
/// 列印文字
///
/// intoText()// { Graphics g = this.CreateGraphics(); 10); }
}
}
更多C#下實現空白窗體上中文輸入,可以實現類PS的文字工具 相關文章請關注PHP中文網!