using System; using System.Drawing; using System.Windows.Forms; using DevComponents.DotNetBar.Keyboard; namespace POSV { public partial class NumericKeyboard : BusinessForm { private readonly System.Timers.Timer _timer = null; private NumericKeyboard() { InitializeComponent(); this.Name = "NumericKeyboard"; this._timer = new System.Timers.Timer(); this._timer.Interval = 100; this._timer.Elapsed += OnElapsed; } 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.keyboardControl.Keyboard = CreateNumericKeyboard(); this.Activate(); } private Keyboard CreateNumericKeyboard() { Keyboard keyboard = new Keyboard(); LinearKeyboardLayout layout = new LinearKeyboardLayout(); layout.AddKey("7"); layout.AddKey("8"); layout.AddKey("9"); //layout.AddKey("+" , "{ADD}" , height: 21); layout.AddKey("删除" , "{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}" , height: 21); layout.AddLine(); layout.AddKey("0" , width: 21); layout.AddKey("."); keyboard.Layouts.Add(layout); return keyboard; } #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 StartTimer() { this._timer.Start(); } private void OnElapsed(object sender , System.Timers.ElapsedEventArgs e) { this.Invoke(new Action(() => { if (!this.Bounds.Contains(Control.MousePosition) && Control.MouseButtons == MouseButtons.Left) { var form = Application.OpenForms["NumericKeyboard"]; if(form != null) { ((NumericKeyboard)form).StopTimer(); form.Close(); } } })); } private void StopTimer() { if (this._timer != null) { this._timer.Stop(); } } public static void CloseKeyboard() { var form = Application.OpenForms["NumericKeyboard"]; if (form != null) { ((NumericKeyboard)form).StopTimer(); form.Close(); } } /// /// 计算键盘显示坐标 /// /// /// /// public static Point CalculateLocation(Point targetPoint, int targetWidth) { Point p = Point.Empty; p.X = targetPoint.X + targetWidth + 5; p.Y = targetPoint.Y; if (p.Y + 250 > Screen.PrimaryScreen.Bounds.Height) { p.Y -= 250; } return p; } public static void ShowKeyboard(IWin32Window owner, Control control) { ShowKeyboard(owner, control, Point.Empty); } public static void ShowKeyboard(IWin32Window owner, Point point) { ShowKeyboard(owner, null, point); } public static void ShowKeyboard(IWin32Window owner, Control control, Point point) { try { var form = Application.OpenForms["NumericKeyboard"]; if(form == null) { form = new NumericKeyboard(); } if (control != null) { Point location = control.PointToScreen(Point.Empty); location.Offset(0 , control.Height + 1); form.Location = location; } else { form.Location = point; } ((NumericKeyboard)form).StartTimer(); form.Show(owner); } catch { var form = Application.OpenForms["NumericKeyboard"]; if (form != null) { ((NumericKeyboard)form).StopTimer(); form.Close(); } } } } }