C#目标平台x86下启动软键盘

文章目录

    • 方式一:调用系统osk
    • 方式二:通过第三方库

方式一:调用系统osk

C#目标平台x86下启动软键盘_第1张图片

C#目标平台x86下启动软键盘_第2张图片
实现方式:
以下代码来源互联网

        #region 目标平台x86下启动系统软键盘		
		
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

        public const UInt32 WM_SYSCOMMAND = 0x112;
        public const UInt32 SC_RESTORE = 0xf120;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
        string OnScreenKeyboadApplication = "osk.exe";

        private void StartOsk()
        {
     
            string processName = System.IO.Path.GetFileNameWithoutExtension(OnScreenKeyboadApplication);

            Process[] keyboardProcess = Process.GetProcessesByName(processName);
            // launch it if it doesn't exist
            if (keyboardProcess.Length == 0)
            {
     
                IntPtr ptr = new IntPtr(); ;
                bool sucessfullyDisabledWow64Redirect = false;

                // Disable x64 directory virtualization if we're on x64,
                // otherwise keyboard launch will fail.
                if (Environment.Is64BitOperatingSystem)
                {
     
                    sucessfullyDisabledWow64Redirect = Wow64DisableWow64FsRedirection(ref ptr);
                }

                // osk.exe is in windows/system folder. So we can directky call it without path
                using (Process osk = new Process())
                {
     
                    osk.StartInfo.FileName = OnScreenKeyboadApplication;
                    osk.Start();
                    //osk.WaitForInputIdle(2000);
                }

                // Re-enable directory virtualisation if it was disabled.
                if (Environment.Is64BitOperatingSystem)
                    if (sucessfullyDisabledWow64Redirect)
                        Wow64RevertWow64FsRedirection(ptr);
            }
            else
            {
     
                // Bring keyboard to the front if it's already running
                var windowHandle = keyboardProcess[0].MainWindowHandle;
                SendMessage(windowHandle, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
            }
        }

方式二:通过第三方库

效果预览:
C#目标平台x86下启动软键盘_第3张图片

  1. 需要引用:DevComponents.DotNetBar.Keyboard.dll

  2. 在窗体中创建TouchKeyboard实例

  3. 然后对textbox1设置

this.touchKeyboard1.SetShowTouchKeyboard(this.textBox1, DevComponents.DotNetBar.Keyboard.TouchKeyboardStyle.Inline);

  1. 实例化VirtualKeyboardColorTable。

  2. 通过修改TouchKeyboardStyle的值可以更新键盘展示方式。

  3. 通过对VirtualKeyboardColorTable的属性修改可以更改键盘样式。

完整代码:

Form6.Designer.cs

namespace TestVirtualKeyboard
{
     
    partial class Form6
    {
     
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
     
            if (disposing && (components != null))
            {
     
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
     
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.touchKeyboard1 = new DevComponents.DotNetBar.Keyboard.TouchKeyboard();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(30, 41);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(592, 109);
            this.textBox1.TabIndex = 0;

            this.touchKeyboard1.SetShowTouchKeyboard(this.textBox1, DevComponents.DotNetBar.Keyboard.TouchKeyboardStyle.Inline);
            // 
            // touchKeyboard1
            // 
            this.touchKeyboard1.FloatingLocation = new System.Drawing.Point(0, 500);
            this.touchKeyboard1.FloatingSize = new System.Drawing.Size(740, 250);
            this.touchKeyboard1.Location = new System.Drawing.Point(100, 400);
            this.touchKeyboard1.Text = "Keyboard";
            // 
            // Form6
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(669, 470);
            this.Controls.Add(this.textBox1);
            this.Name = "Form6";
            this.Text = "Form6";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form6_FormClosing);
            this.Load += new System.EventHandler(this.Form6_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private DevComponents.DotNetBar.Keyboard.TouchKeyboard touchKeyboard1;
    }
}

Form6.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Keyboard;

namespace TestVirtualKeyboard
{
     
    public partial class Form6 : Form
    {
     
        private VirtualKeyboardColorTable _ColorTableDefault = new VirtualKeyboardColorTable();

        public Form6()
        {
     
            InitializeComponent();
        }

        private void Form6_Load(object sender, EventArgs e)
        {
     
            touchKeyboard1.ColorTable = _ColorTableDefault;
        }

        private void Form6_FormClosing(object sender, FormClosingEventArgs e)
        {
     
            _ColorTableDefault.Dispose();
        }

    }
}

你可能感兴趣的:(C#实践,c#,软键盘)