using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar.Controls; using DevComponents.DotNetBar.Keyboard; using JwKdsV.Core; using JwKdsV.Core.MessageEvent; using JwKdsV.Core.Utils; namespace JwKdsV.Component { public partial class VirtualKeyboard : Form { private KeyboardType _defaultKeyboardType = KeyboardType.英文; public VirtualKeyboard(KeyboardType keyboardType) { InitializeComponent(); //订购界面变更通知事件 MsgEvent.RemoveListener(Constant.KEYBOARD_CHANGED_NOTIFY, this.KeyboardChangedEventNotify); MsgEvent.Receive(Constant.KEYBOARD_CHANGED_NOTIFY, this.KeyboardChangedEventNotify); this._defaultKeyboardType = keyboardType; } protected void KeyboardChangedEventNotify(object sender, MsgEventArgs args) { this.Invoke(new Action(() => { this._defaultKeyboardType = (KeyboardType)args.Data; switch (this._defaultKeyboardType) { case KeyboardType.英文: { this.chkEnglish.Checked = true; } break; case KeyboardType.中文: { this.chkChinese.Checked = true; } break; case KeyboardType.数字: { this.chkNumber.Checked = true; } break; } })); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.Manual; this.keyboardControl.Renderer = new ThreeDRenderer(); //标题拖动事件 this.topPanel.MouseDown += OnTitleMouseDown; switch (this._defaultKeyboardType) { case KeyboardType.英文: { this.chkEnglish.Checked = true; } break; case KeyboardType.中文: { this.chkChinese.Checked = true; } break; case KeyboardType.数字: { this.chkNumber.Checked = true; } break; } this.Activate(); } private void OnTitleMouseDown(object sender, MouseEventArgs e) { WindowsAPI.MoveWindows(this.Handle); Owner.Activate(); } #region Window activation handling private const int WM_MOUSEACTIVATE = 0x0021; private const int MA_NOACTIVATE = 0x0003; private const int WS_EX_NOACTIVATE = 0x08000000; // Override CreateParams to specify that this Form should not be activated. protected override CreateParams CreateParams { get { 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) { Owner.Activate(); } #endregion private void OnCloseClick(object sender, EventArgs e) { this.Hide(); } public void ShowVirtualKeyboard(IWin32Window owner, KeyboardType keyboardType) { this.Hide(); this.Show(owner); MsgEvent.Send(Constant.KEYBOARD_CHANGED_NOTIFY, keyboardType); } private void KeyboardChanged(object sender, EventArgs e) { var item = sender as CheckBoxX; if (item.Checked) { KeyboardType type = KeyboardType.中文; Enum.TryParse(item.Tag.ToString(), out type); switch (type) { case KeyboardType.中文: { this.lblInputLanguage.Visible = true; var lang = InputLanguage.FromCulture(CultureInfo.GetCultureInfo("zh-CN")); InputLanguage.CurrentInputLanguage = lang; this.keyboardControl.Keyboard = Keyboard.CreateDefaultKeyboard(); } break; case KeyboardType.英文: { this.lblInputLanguage.Visible = false; var lang = InputLanguage.FromCulture(CultureInfo.GetCultureInfo("en-US")); InputLanguage.CurrentInputLanguage = lang; this.keyboardControl.Keyboard = Keyboard.CreateDefaultKeyboard(); } break; case KeyboardType.数字: { this.lblInputLanguage.Visible = false; this.keyboardControl.Keyboard = CreateNumericKeyboard(); } break; } } this.keyboardControl.Invalidate(); this.Owner.Activate(); } private Keyboard CreateNumericKeyboard() { Keyboard keyboard = new Keyboard(); LinearKeyboardLayout layout = new LinearKeyboardLayout(); layout.AddKey("7"); layout.AddKey("8"); layout.AddKey("9"); layout.AddKey("Backspace", "{BACKSPACE}", height: 21); layout.AddLine(); layout.AddKey("4"); layout.AddKey("5"); layout.AddKey("6"); layout.AddLine(); layout.AddKey("1"); layout.AddKey("2"); layout.AddKey("3"); layout.AddKey("Enter", "{ENTER}", height: 21); layout.AddLine(); layout.AddKey("0", width: 21); layout.AddKey("."); keyboard.Layouts.Add(layout); return keyboard; } private void OnChangedLanguage(object sender, EventArgs e) { InputSimulatorUtils.SendCtrlShift(); } } public class ThreeDRenderer : Renderer { private Font _Font = new Font("Segoe UI", 11, 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) { // } 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)); } } }