C#虚拟键盘实现 如何让窗口不获得焦点

虚拟按键比较简单 sendkeys 就可以了
但是如果点击就得到焦点,就无法在文本框中输入内容。
实现代码如下

  protected override CreateParams CreateParams    //<---
        {
            get
            {
                const int WS_EX_NOACTIVATE = 0x08000000;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }
        }

源码下载地址

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace Monitor
{
    public partial class Form1 : Form
    {
        //(2)声明类对象

        public Form1()
        {
            InitializeComponent();
        }
        protected override CreateParams CreateParams    //<---
        {
            get
            {
                const int WS_EX_NOACTIVATE = 0x08000000;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            windowchange("中");
            //这个区域不包括任务栏的
            Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
            //这个区域包括任务栏,就是屏幕显示的物理范围
         //   Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            int width1 = ScreenArea.Width; //屏幕宽度 
            int height1 = ScreenArea.Height; //屏幕高度
            this.Location = new Point(width1 - this.Width - 10, height1 - this.Height - 10);

            this.TopMost = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{=}");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{/}");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{*}");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{-}");
        }
        private void button8_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{+}");

        }

        private void button5_Click(object sender, EventArgs e)
        {
            SendKeys.Send("7");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SendKeys.Send("8");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SendKeys.Send("9");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            SendKeys.Send("4");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            SendKeys.Send("5");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            SendKeys.Send("6");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            SendKeys.Send("1");
        }

        private void button14_Click(object sender, EventArgs e)
        {
            SendKeys.Send("2");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            SendKeys.Send("3");
        }

        private void button17_Click(object sender, EventArgs e)
        {
            SendKeys.Send("0");
        }

        private void button18_Click(object sender, EventArgs e)
        {
            SendKeys.Send("00");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            SendKeys.Send(".");
        }
        //小窗口
        private void button20_Click(object sender, EventArgs e)
        {
            windowchange("小");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            windowchange("中");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            windowchange("大");
        }

       public void windowchange(string str)
        {
            if (str.Equals("小"))
            {
                this.Width = 250;
                this.Height = 310;
            }
            if (str.Equals("中"))
            {
                this.Width = 370;
                this.Height = 420;
            }
            if (str.Equals("大"))
            {
                this.Width = 530;
                this.Height = 560;
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{ENTER}");
        }

        private void button21_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{BACKSPACE}");
        }
    }
}

sendkeys用法参考https://www.cnblogs.com/shaozhuyong/p/5951779.html

转载请注明出处:

转载自http://lxfamn.cn/blog

未经允许不得转载:lxfamn » C#虚拟键盘实现 如何让窗口不获得焦点

赞 (0) 打赏

置顶推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏