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; using DevComponents.DotNetBar.Keyboard; using JwKdsV.Core; using JwKdsV.Core.MessageEvent; namespace JwKdsV { public partial class SimpleKeyboard : BaseForm { private readonly int defaultWidth; private readonly int diffWidth; private readonly bool allowShowSearchBox; private readonly Action action; private SimpleKeyboard(Point location, Size size, bool showSearchBox = false, Action action = null) { InitializeComponent(); this.Name = "SimpleKeyboard"; this.allowShowSearchBox = showSearchBox; this.action = action; this.txtSearch.Multiline = false; this.topPanel.Visible = this.allowShowSearchBox; this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.Manual; if (location != null && !location.IsEmpty) { this.Location = new Point(location.X, location.Y); } if (size != null && !size.IsEmpty) { this.Size = new Size(size.Width, size.Height); } this.defaultWidth = this.Width / 10; this.diffWidth = (this.Width - this.defaultWidth * 10); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.keyboardControl.Renderer = new SearchRenderer(); this.keyboardControl.Keyboard = CreateDefaultKeyboard(); } public static void ShowKeyboard(IWin32Window owner, Point location, Size size, bool showSearchBox = false, Action action = null) { var form = Application.OpenForms["SimpleKeyboard"]; if (form == null) { form = new SimpleKeyboard(location, size, showSearchBox, action); form.Show(owner); } } public static bool Showing() { var form = Application.OpenForms["SimpleKeyboard"]; return form != null; } public static void CloseKeyboard() { var form = Application.OpenForms["SimpleKeyboard"]; if (form != null) { form.Close(); } } protected override void OnFormClosing(FormClosingEventArgs e) { e.Cancel = false; } #region Window activation handling private const int WM_MOUSEACTIVATE = 0x0021; private const int MA_NOACTIVATE = 0x0003; private const int WS_EX_NOACTIVATE = 0x08000000; protected override CreateParams CreateParams { get { if (this.allowShowSearchBox) { return base.CreateParams; } else { CreateParams createParams = base.CreateParams; createParams.ExStyle = createParams.ExStyle & WS_EX_NOACTIVATE; return createParams; } } } protected override void WndProc(ref Message m) { // This prevents the window from being activated with the mouse, // but allows the window to be resized (with the mouse). if (m.Msg == WM_MOUSEACTIVATE) { m.Result = (IntPtr)MA_NOACTIVATE; } else { base.WndProc(ref m); } } protected override void OnActivated(EventArgs e) { if (!this.allowShowSearchBox) { Owner.Activate(); } else { this.Activate(); } } #endregion private Keyboard CreateDefaultKeyboard() { Keyboard keyboard = new Keyboard(); LinearKeyboardLayout layout = new LinearKeyboardLayout(); layout.AddKey("1", width: defaultWidth); layout.AddKey("2", width: defaultWidth); layout.AddKey("3", width: defaultWidth); layout.AddKey("4", width: defaultWidth); layout.AddKey("5", width: defaultWidth); layout.AddKey("6", width: defaultWidth); layout.AddKey("7", width: defaultWidth); layout.AddKey("8", width: defaultWidth); layout.AddKey("9", width: defaultWidth); layout.AddKey("0", width: defaultWidth + diffWidth); layout.AddLine(); layout.AddKey("Q", width: defaultWidth); layout.AddKey("W", width: defaultWidth); layout.AddKey("E", width: defaultWidth); layout.AddKey("R", width: defaultWidth); layout.AddKey("T", width: defaultWidth); layout.AddKey("Y", width: defaultWidth); layout.AddKey("U", width: defaultWidth); layout.AddKey("I", width: defaultWidth); layout.AddKey("O", width: defaultWidth); layout.AddKey("P", width: defaultWidth + diffWidth); layout.AddLine(); layout.AddKey("A", width: defaultWidth); layout.AddKey("S", width: defaultWidth); layout.AddKey("D", width: defaultWidth); layout.AddKey("F", width: defaultWidth); layout.AddKey("G", width: defaultWidth); layout.AddKey("H", width: defaultWidth); layout.AddKey("J", width: defaultWidth); layout.AddKey("K", width: defaultWidth); layout.AddKey("L", width: defaultWidth); layout.AddKey("回车", info: "{ENTER}", width: defaultWidth + diffWidth, height: 21); layout.AddLine(); layout.AddKey("Z", width: defaultWidth); layout.AddKey("X", width: defaultWidth); layout.AddKey("C", width: defaultWidth); layout.AddKey("V", width: defaultWidth); layout.AddKey("B", width: defaultWidth); layout.AddKey("N", width: defaultWidth); layout.AddKey("M", width: defaultWidth); layout.AddKey("退格", info: "{BACKSPACE}", width: defaultWidth * 2); keyboard.Layouts.Add(layout); return keyboard; } private void OnSearchTextChanged(object sender, EventArgs e) { if (this.allowShowSearchBox && this.action != null) { this.action(this.txtSearch.Text.Trim()); } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Escape: { this.Close(); } break; case Keys.Right: { MsgEvent.Send(Constant.PRODUCT_KEYBOARD_NOTIFY, KeyboardAction.Right); } break; case Keys.Left: { MsgEvent.Send(Constant.PRODUCT_KEYBOARD_NOTIFY, KeyboardAction.Left); } break; case Keys.Up: { MsgEvent.Send(Constant.PRODUCT_KEYBOARD_NOTIFY, KeyboardAction.Up); } break; case Keys.Down: { MsgEvent.Send(Constant.PRODUCT_KEYBOARD_NOTIFY, KeyboardAction.Down); } break; case Keys.Enter: { } break; } return base.ProcessCmdKey(ref msg, keyData); } private void OnSendingKey(object sender, KeyboardKeyCancelEventArgs e) { this.txtSearch.Focus(); } private void OnSearchEnterClick(object sender, Component.EnterEventArg e) { this.action("Enter"); this.txtSearch.Text = string.Empty; this.txtSearch.Focus(); } } public class SearchRenderer : Renderer { private Font _font = new Font("宋体", 15, FontStyle.Bold); private StringFormat _format; public SearchRenderer() { _format = (StringFormat)StringFormat.GenericDefault.Clone(); _format.LineAlignment = StringAlignment.Center; _format.Alignment = StringAlignment.Center; } public override void DrawBackground(BackgroundRendererEventArgs args) { using (var brush = new SolidBrush(Color.White)) { args.Graphics.FillRectangle(brush, args.Bounds); } } public override void DrawCloseButton(CloseButtonRendererEventArgs args) { } public override void DrawKey(KeyRendererEventArgs args) { Rectangle keyBounds = args.Bounds; if (args.IsDown || args.Key.Style == KeyStyle.Pressed) { using (var brush = new SolidBrush(Color.Moccasin)) { args.Graphics.FillRectangle(brush, keyBounds); } using (var brush = new SolidBrush(Color.Black)) { args.Graphics.DrawString(args.Key.Caption, _font, brush, keyBounds, _format); } } else { using (var brush = new SolidBrush(Color.Tan)) { args.Graphics.FillRectangle(brush, keyBounds); } using (var brush = new SolidBrush(Color.Black)) { args.Graphics.DrawString(args.Key.Caption, _font, brush, keyBounds, _format); } } } public override void DrawTopBar(TopBarRendererEventArgs args) { args.Graphics.DrawString(args.Text, _font, ColorTable.TopBarTextBrush, args.Bounds); } } }