当前位置:编程学习 > C#/ASP.NET >>

C#中使用输入法

目前我的页面中有textBox1,button1,button2
代码部分为:
namespace Program
{
    public partial class Form1 : Form
    {
        [DllImport("user32", EntryPoint = "FindWindow", SetLastError = false,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int FindWindow(
            string IpClassName,
            string IpWindowName
            );
        [DllImport("user32", EntryPoint = "SetForegroundWindow", SetLastError = false,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int SetForegroundWindow(
            IntPtr hWnd
            );
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Process Process = new Process(); 
            Process.Start("C:\\WINDOWS\\system32\\OSK.exe");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = (IntPtr)FindWindow(null, "123.txt - 记事本");
            SetForegroundWindow(hwnd);
            SendKeys.Send("先用和FindWindow  这个WindowsAPI函数找到你要打字的窗体,让后用SetForegroundWindow激活这个窗体," +
                          "让后调用SendKeys.send 直接发送字符串就行了,给你举个例子吧:\n\r" +
                          "先在桌面新建一个文本文件 ,命名为:123.txt  , 然后双击打开123.txt  然后运行本程序,单击 button1 " +
                          "效果不错吧,是你想要的吧,O(∩_∩)O哈!"
                            );

        }
    }
}
目前button1在打开系统软键盘的时,只能在textbox1中输入英文,无法直接输入中文。而button2的功能是对记事本输入内容,不符合我的意图。我是想在打开软键盘的情况下,直接可以对文本框输入任意的中文,请各位大虾帮忙解决

--------------------编程问答-------------------- 其实你可以对文本框输入的字符校验,判断是否输入中文,否则不能输入,判断中文的方法:

1.protected bool  IsChineseLetter(string input,int index)   
2.{   
3.int code = 0;   
4.int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)   
5.        int chend = Convert.ToInt32("9fff", 16);   
6.if (input != "")   
7.{   
8.code = Char.ConvertToUtf32(input, index);    //获得字符串input中指定索引index处字符unicode编码   
9.  
10.if (code >= chfrom && code <= chend)   
11.{   
12.return true;     //当code在中文范围内返回true   
13.            }   
14.else  
15.{   
16.return false ;    //当code不在中文范围内返回false   
17.            }   
18.}   
19.return false;   
20.}   
21.方法二:   
22.public bool IsChina(string CString)   
23.{   
24.bool BoolValue = false;   
25.for (int i = 0; i < CString.Length; i++)   
26.{   
27.if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))   
28.{   
29.BoolValue = false;   
30.}   
31.else  
32.{   
33.return BoolValue = true;   
34.}   
35.}   
36.return BoolValue;   
37.} 
--------------------编程问答-------------------- --------------------编程问答-------------------- 问题是:我现在只是打开了软键盘,无法打出中文啊,只能打英文,在输入法那边怎么控制呢? --------------------编程问答-------------------- 用Windows系统带的屏幕键盘向窗体文本框输入中文没有问题啊 --------------------编程问答-------------------- 那是你输入法的问题吧
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,