C#自己编写软键盘

       最近的一个项目需要在工控机上完成,触摸屏,包括键盘输入。此时需要一个软键盘,只完成数字的输入就可以,但是操作系统自带的软键盘是包含非数字内容,且占用面积大。因此需要自己写一个软键盘的程序,在有数字输入的时候调用显示即可。

一、添加控件DotNetBar

       在vs中,工具箱右键->选择项->浏览dotnetbar的dll 加载进来 ,工具箱里面就有dotnetbar 的控件了。在Form中拖入控件keyboardControl。

二、用代码添加软键盘的按键

       public Form1()
        {
            InitializeComponent();
            keyboardControl1.Keyboard = CreateNumericKeyboard();
            keyboardControl1.Invalidate();
            keyboardControl1.Renderer = new ThreeDRenderer();
        }
        private Keyboard CreateNumericKeyboard()
        {
            Keyboard keyboard = new Keyboard();
           
             LinearKeyboardLayout klNumLockOn = new LinearKeyboardLayout();
            klNumLockOn.AddKey("1");
            klNumLockOn.AddKey("2");
            klNumLockOn.AddKey("3");
            klNumLockOn.AddKey("4");
            klNumLockOn.AddKey("5");
            klNumLockOn.AddKey("6");
            klNumLockOn.AddKey("7");
            klNumLockOn.AddKey("8");
            klNumLockOn.AddKey("9");
            klNumLockOn.AddKey("0");
            klNumLockOn.AddKey(".");
            klNumLockOn.AddKey("←", info: "{BACKSPACE}", width: 21);
            keyboard.Layouts.Add(klNumLockOn);
            return keyboard;
        }
三、将键盘设置为浮动工具条窗体,不抢占焦点
        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;
       
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
                cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
                return cp;
                //return base.CreateParams;
            }
        }
四、改变键盘按键上的字体大小、字体、或背景色
 public class ThreeDRenderer : Renderer
    {
        private Font _Font = new Font("Microsoft YaHei", 15, FontStyle.Bold);
        private StringFormat _Format;
        ///
        /// Initializes a new instance of the ThreeDRenderer class.
        ///

        public ThreeDRenderer()
        {
            _Format = (StringFormat)StringFormat.GenericDefault.Clone();
            _Format.LineAlignment = StringAlignment.Center;
            _Format.Alignment = StringAlignment.Center;
        }
       
        public override void DrawBackground(BackgroundRendererEventArgs args)
        {
            /*
            using (TextureBrush brush = new TextureBrush(global::NumberTouchKeyBoard.Properties.Resources.Fiber))
            {
                brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
                args.Graphics.FillRectangle(brush, args.Bounds);
            }
            */
        }
       
        public override void DrawKey(KeyRendererEventArgs args)
        {
            Rectangle keyBounds = args.Bounds;
            args.Graphics.FillRectangle(ColorTable.KeysBrush, keyBounds);
            if (args.IsDown || args.Key.Style == KeyStyle.Pressed || args.Key.Style == KeyStyle.Toggled)
            {
                Draw3DBorder(args.Graphics, keyBounds, ColorTable.DarkKeysBrush, ColorTable.LightKeysBrush);
                keyBounds.Offset(1, 1);
                if (args.Key.Style == KeyStyle.Toggled)
                    args.Graphics.DrawString(args.Key.Caption, _Font, ColorTable.ToggleTextBrush, keyBounds, _Format);
                else
                    args.Graphics.DrawString(args.Key.Caption, _Font, ColorTable.TextBrush, keyBounds, _Format);
            }
            else
            {
                Draw3DBorder(args.Graphics, keyBounds, ColorTable.LightKeysBrush, ColorTable.DarkKeysBrush);
                args.Graphics.DrawString(args.Key.Caption, _Font, ColorTable.TextBrush, keyBounds, _Format);
            }
        }
        public override void DrawTopBar(TopBarRendererEventArgs args)
        {
            args.Graphics.DrawString(args.Text, _Font, ColorTable.TopBarTextBrush, args.Bounds);
        }
       
        public override void DrawCloseButton(CloseButtonRendererEventArgs args)
        {
           
            if (args.IsDown)
                Draw3DBorder(args.Graphics, args.Bounds, ColorTable.DarkKeysBrush, ColorTable.LightKeysBrush);
            else
                Draw3DBorder(args.Graphics, args.Bounds, ColorTable.LightKeysBrush, ColorTable.DarkKeysBrush);
            Rectangle rect = args.Bounds;
            rect.Inflate(-5, -5);
            using (Pen p = new Pen(ColorTable.TextBrush, 2))
            {
                args.Graphics.DrawLine(p, rect.Left, rect.Top, rect.Right, rect.Bottom);
                args.Graphics.DrawLine(p, rect.Left, rect.Bottom, rect.Right, rect.Top);
            }
           
        }
       
        private static void Draw3DBorder(Graphics g, Rectangle bounds, Brush light, Brush dark)
        {
            int borderSize = 1;
            g.FillRectangle(light, new Rectangle(bounds.Left, bounds.Top, bounds.Width, borderSize));
            g.FillRectangle(light, new Rectangle(bounds.Left, bounds.Top, borderSize, bounds.Height));
            g.FillRectangle(dark, new Rectangle(bounds.Left, bounds.Bottom - borderSize, bounds.Width, borderSize));
            g.FillRectangle(dark, new Rectangle(bounds.Right - borderSize, bounds.Top, borderSize, bounds.Height));
        }
    }

五、效果图(关闭按钮是自己用Button控件添加的)
六、最后调用软键盘
       软键盘写好了,就需要在其他程序里面调用。
       其实有很多的解决方案,我是直接在需要出现该软件的事件中,添加消息响应,如在EditBox中添加Click响应事件,调用键盘进程。注意,进程不要忘了kill掉,否则会出现很多进程。每次点击最好将之前的进程全部杀死,然后重建,否则会有软键盘存在,却看不到(在界面后面)的情况发生。该方法未必最科学,请大家批评指正。
     public static bool start_keyboard() //启动键盘
        {
            try
            {
                Process[] prc = Process.GetProcesses();
                foreach (Process pr in prc) //遍历整个进程
                {
                    if (keyboard_name1 == pr.ProcessName) //如果进程存在
                    {
                        pr.Kill();                      
                    }
                }
                 psInfo = new ProcessStartInfo(@keyboard_name);
                 pro.StartInfo = psInfo;
                if (pro.Start())
                {
                    SetForegroundWindow(pro.MainWindowHandle);
                }
               
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


     



你可能感兴趣的:(.Net)