Create a soft keyboard

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace keyboardDemo
{
    public partial class Form1 : Form
    {
        

        /// <summary>
        /// 涂聚文
        /// 软键盘http://code.msdn.microsoft.com/CSSoftKeyboard-0a86f914
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.Press += MiniKeyboardHandler;
            f.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiniKeyboardHandler(object sender, MiniKeyboardArgs e)
        {
            if (this.textBox1.Text == "←backpspace")
            {
                if (this.textBox1.Text.Length != 0)
                {
                        

                }

            }
            else
            {
                textBox1.Text += e.KeyCode;
                string s = this.textBox1.Text;
                if (s.Contains("←backpspace"))//去除一个字符串
                {
                    s = s.Replace("←backpspace", "");
                    if (s.Length > 0)
                    {
                        s = s.Substring(0, s.Length - 1);
                        this.textBox1.Text = s;
                    }
                    else
                    {
                        this.textBox1.Text = s;
                    }
                }
               
            }
            
        }
    }
}
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace keyboardDemo
{
    public partial class Form2 : Form
    {
        bool isShift = true;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void MiniKeyboardHandler(object sender, MiniKeyboardArgs e);
        /// <summary>
        /// 
        /// </summary>
        public event MiniKeyboardHandler Press;

        /// <summary>
        /// 
        /// </summary>
        private void BindEvent()
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is Button)
                    ctl.Click += MinikeyboardPress;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public Form2()
        {
            InitializeComponent();
            BindEvent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MinikeyboardPress(object sender, EventArgs e)
        {
            //大小写
            if (((Button)sender).Text == "Shift")
            {
                if (isShift == true)
                {
                    isShift = false;
                    string s = this.button1.Text;
                    s = s.ToUpper();
                    this.button1.Text = s;
                }
                else
                {
                    isShift = true;
                    string s = this.button1.Text;
                    s = s.ToLower();
                    this.button1.Text = s;
                }
            }
            else
            {
                OnMiniKeyboardHandle(new MiniKeyboardArgs(((Button)sender).Text));
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        private void OnMiniKeyboardHandle(MiniKeyboardArgs e)
        {
            MiniKeyboardHandler temp = Press;
            if (temp != null)
                temp(this, e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            //if (isShift == true)
            //{
            //    isShift = false;
            //    this.button1.Text.ToUpper();
            //}
            //else
            //{
            //    isShift = true;
            //}
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class MiniKeyboardArgs : EventArgs
    {
        public string KeyCode { get; private set; }

        public MiniKeyboardArgs(string code)
        {
            if (code == "←backpspace")
            {
                if (code.Length != 0)
                {
                    KeyCode = code;
                }
                else
                {
                    KeyCode = code;
                    //KeyCode = "";Caps Lock
                }
            }
            else if (code == "Shift")
            {
                KeyCode = "";

            }
            else if (code == "Caps Lock")
            {
                KeyCode = "";

            }
            else
            {
                KeyCode = code;
            }
        }
    }
   
}
复制代码

 

复制代码
        /// <summary>
        /// 
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            //在TEXTBOX控件中加一个按钮控件,如QQ软键盘一样的图标
            this.textBox1.Controls.Add(DropButton);
            DropButton.Location = new Point(this.textBox1.Width - 20, 0);



        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropButton_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("erro");
            Form2 f = new Form2();
            f.StartPosition = FormStartPosition.Manual;//窗体位置设置
            f.Location = new Point(this.Location.X / 2 + this.textBox1.Location.X + 420, this.Location.Y / 2 + this.textBox1.Height + this.Height / 2+150);//位置设置
            f.Press += MiniKeyboardHandler;
            f.ShowDialog();

        }
复制代码

 

posted @   ®Geovin Du Dream Park™  阅读(343)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2012-09-21 SQL 生成公曆和農曆對照數據續--创建萬年曆查找各種周期性節日數據
2010-09-21 Csharp,Javascript 获取显示器的大小的几种方式
2009-09-21 谷歌地图API学习
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示