* 修改国际化的Demo

This commit is contained in:
Sunny 2024-07-23 14:33:14 +08:00
parent d4a6cc80cc
commit 855719997c
4 changed files with 67 additions and 885 deletions

View File

@ -64,6 +64,7 @@
this.uiSymbolButton13 = new Sunny.UI.UISymbolButton(); this.uiSymbolButton13 = new Sunny.UI.UISymbolButton();
this.uiLine11 = new Sunny.UI.UILine(); this.uiLine11 = new Sunny.UI.UILine();
this.uiSymbolButton12 = new Sunny.UI.UISymbolButton(); this.uiSymbolButton12 = new Sunny.UI.UISymbolButton();
this.uiPagination1 = new Sunny.UI.UIPagination();
this.SuspendLayout(); this.SuspendLayout();
// //
// btnError // btnError
@ -326,7 +327,7 @@
this.uiLine5.Location = new System.Drawing.Point(30, 292); this.uiLine5.Location = new System.Drawing.Point(30, 292);
this.uiLine5.MinimumSize = new System.Drawing.Size(16, 16); this.uiLine5.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine5.Name = "uiLine5"; this.uiLine5.Name = "uiLine5";
this.uiLine5.Size = new System.Drawing.Size(670, 20); this.uiLine5.Size = new System.Drawing.Size(442, 20);
this.uiLine5.TabIndex = 58; this.uiLine5.TabIndex = 58;
this.uiLine5.Text = "UINotifier"; this.uiLine5.Text = "UINotifier";
this.uiLine5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.uiLine5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
@ -434,9 +435,9 @@
this.uiLine6.Location = new System.Drawing.Point(30, 450); this.uiLine6.Location = new System.Drawing.Point(30, 450);
this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16); this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine6.Name = "uiLine6"; this.uiLine6.Name = "uiLine6";
this.uiLine6.Size = new System.Drawing.Size(328, 20); this.uiLine6.Size = new System.Drawing.Size(668, 20);
this.uiLine6.TabIndex = 59; this.uiLine6.TabIndex = 59;
this.uiLine6.Text = "ULocalize"; this.uiLine6.Text = "国际化";
this.uiLine6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.uiLine6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// //
// btnEN // btnEN
@ -450,7 +451,7 @@
this.btnEN.Size = new System.Drawing.Size(100, 35); this.btnEN.Size = new System.Drawing.Size(100, 35);
this.btnEN.Symbol = 57571; this.btnEN.Symbol = 57571;
this.btnEN.TabIndex = 61; this.btnEN.TabIndex = 61;
this.btnEN.Text = "EN"; this.btnEN.Text = "English";
this.btnEN.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnEN.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnEN.Click += new System.EventHandler(this.btnEN_Click); this.btnEN.Click += new System.EventHandler(this.btnEN_Click);
// //
@ -466,7 +467,7 @@
this.btnCH.StyleCustomMode = true; this.btnCH.StyleCustomMode = true;
this.btnCH.Symbol = 57571; this.btnCH.Symbol = 57571;
this.btnCH.TabIndex = 60; this.btnCH.TabIndex = 60;
this.btnCH.Text = "CH"; this.btnCH.Text = "中文";
this.btnCH.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnCH.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnCH.Click += new System.EventHandler(this.btnCH_Click); this.btnCH.Click += new System.EventHandler(this.btnCH_Click);
// //
@ -637,10 +638,10 @@
this.uiLine10.BackColor = System.Drawing.Color.Transparent; this.uiLine10.BackColor = System.Drawing.Color.Transparent;
this.uiLine10.Font = new System.Drawing.Font("宋体", 12F); this.uiLine10.Font = new System.Drawing.Font("宋体", 12F);
this.uiLine10.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); this.uiLine10.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48)))));
this.uiLine10.Location = new System.Drawing.Point(372, 450); this.uiLine10.Location = new System.Drawing.Point(486, 292);
this.uiLine10.MinimumSize = new System.Drawing.Size(16, 16); this.uiLine10.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine10.Name = "uiLine10"; this.uiLine10.Name = "uiLine10";
this.uiLine10.Size = new System.Drawing.Size(328, 20); this.uiLine10.Size = new System.Drawing.Size(214, 20);
this.uiLine10.TabIndex = 74; this.uiLine10.TabIndex = 74;
this.uiLine10.Text = "选择文件夹"; this.uiLine10.Text = "选择文件夹";
this.uiLine10.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.uiLine10.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
@ -649,7 +650,7 @@
// //
this.uiSymbolButton13.Cursor = System.Windows.Forms.Cursors.Hand; this.uiSymbolButton13.Cursor = System.Windows.Forms.Cursors.Hand;
this.uiSymbolButton13.Font = new System.Drawing.Font("宋体", 12F); this.uiSymbolButton13.Font = new System.Drawing.Font("宋体", 12F);
this.uiSymbolButton13.Location = new System.Drawing.Point(372, 482); this.uiSymbolButton13.Location = new System.Drawing.Point(484, 324);
this.uiSymbolButton13.MinimumSize = new System.Drawing.Size(1, 1); this.uiSymbolButton13.MinimumSize = new System.Drawing.Size(1, 1);
this.uiSymbolButton13.Name = "uiSymbolButton13"; this.uiSymbolButton13.Name = "uiSymbolButton13";
this.uiSymbolButton13.Padding = new System.Windows.Forms.Padding(28, 0, 0, 0); this.uiSymbolButton13.Padding = new System.Windows.Forms.Padding(28, 0, 0, 0);
@ -688,11 +689,32 @@
this.uiSymbolButton12.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.uiSymbolButton12.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiSymbolButton12.Click += new System.EventHandler(this.uiSymbolButton12_Click); this.uiSymbolButton12.Click += new System.EventHandler(this.uiSymbolButton12_Click);
// //
// uiPagination1
//
this.uiPagination1.ButtonFillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(128)))), ((int)(((byte)(204)))));
this.uiPagination1.ButtonStyleInherited = false;
this.uiPagination1.CausesValidation = false;
this.uiPagination1.Font = new System.Drawing.Font("宋体", 12F);
this.uiPagination1.Location = new System.Drawing.Point(258, 482);
this.uiPagination1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.uiPagination1.MinimumSize = new System.Drawing.Size(1, 1);
this.uiPagination1.Name = "uiPagination1";
this.uiPagination1.PagerCount = 11;
this.uiPagination1.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.uiPagination1.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
this.uiPagination1.ShowText = false;
this.uiPagination1.Size = new System.Drawing.Size(442, 35);
this.uiPagination1.TabIndex = 79;
this.uiPagination1.Text = "uiDataGridPage1";
this.uiPagination1.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
this.uiPagination1.TotalCount = 40;
//
// FDialogs // FDialogs
// //
this.AllowShowTitle = true; this.AllowShowTitle = true;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(800, 549); this.ClientSize = new System.Drawing.Size(800, 590);
this.Controls.Add(this.uiPagination1);
this.Controls.Add(this.uiLine11); this.Controls.Add(this.uiLine11);
this.Controls.Add(this.uiSymbolButton12); this.Controls.Add(this.uiSymbolButton12);
this.Controls.Add(this.uiLine1); this.Controls.Add(this.uiLine1);
@ -776,5 +798,6 @@
private UISymbolButton uiSymbolButton13; private UISymbolButton uiSymbolButton13;
private UILine uiLine11; private UILine uiLine11;
private UISymbolButton uiSymbolButton12; private UISymbolButton uiSymbolButton12;
private UIPagination uiPagination1;
} }
} }

View File

@ -4,13 +4,19 @@ using System.Threading;
namespace Sunny.UI.Demo namespace Sunny.UI.Demo
{ {
public partial class FDialogs : UIPage public partial class FDialogs : UIPage, ITranslate
{ {
public FDialogs() public FDialogs()
{ {
InitializeComponent(); InitializeComponent();
} }
public override void Init()
{
base.Init();
btnCH.PerformClick();
}
private void btnInfo_Click(object sender, EventArgs e) private void btnInfo_Click(object sender, EventArgs e)
{ {
this.ShowInfoDialog("默认信息提示框"); this.ShowInfoDialog("默认信息提示框");
@ -140,16 +146,6 @@ namespace Sunny.UI.Demo
this.ShowErrorNotifier("Error"); this.ShowErrorNotifier("Error");
} }
private void btnCH_Click(object sender, EventArgs e)
{
UILocalizeHelper.SetCH();
}
private void btnEN_Click(object sender, EventArgs e)
{
UILocalizeHelper.SetEN();
}
private void uiSymbolButton9_Click(object sender, EventArgs e) private void uiSymbolButton9_Click(object sender, EventArgs e)
{ {
this.ShowSuccessTip("轻便消息提示框 - 成功"); this.ShowSuccessTip("轻便消息提示框 - 成功");
@ -234,5 +230,32 @@ namespace Sunny.UI.Demo
Thread.Sleep(2000); Thread.Sleep(2000);
this.HideProcessForm(); this.HideProcessForm();
} }
private void btnCH_Click(object sender, EventArgs e)
{
UILocalizeHelper.SetCH();
LoginText = "登录";
UIStyles.Translate();
}
private void btnEN_Click(object sender, EventArgs e)
{
UILocalizeHelper.SetEN();
LoginText = "Login";
UIStyles.Translate();
}
private string LoginText = "登录";
public void Translate()
{
uiSymbolButton6.Text = btnSuccess.Text = uiSymbolButton9.Text = UILocalize.SuccessTitle;
uiSymbolButton5.Text = btnWarn.Text = uiSymbolButton8.Text = UILocalize.WarningTitle;
uiSymbolButton4.Text = btnError.Text = uiSymbolButton7.Text = UILocalize.ErrorTitle;
uiSymbolButton2.Text = btnInfo.Text = UILocalize.InfoTitle;
btnAsk.Text = UILocalize.AskTitle;
uiSymbolButton3.Text = uiSymbolButton10.Text = LoginText;
}
} }
} }

View File

@ -1,864 +0,0 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2024 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UHook.cs
* :
* : V3.1
* : 2020-01-01
*
* 2020-01-01: V2.2.0
******************************************************************************/
using Sunny.UI.Win32;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
#pragma warning disable 1591
namespace Sunny.UI
{
/// <summary>
/// Abstract base class for Mouse and Keyboard hooks
/// </summary>
public abstract class GlobalHook
{
#region Windows API Code
[StructLayout(LayoutKind.Sequential)]
protected class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
protected class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
protected class MouseLLHookStruct
{
public POINT pt;
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
protected class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
protected static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam);
[DllImport("user32")]
protected static extern int ToAscii(
int uVirtKey,
int uScanCode,
byte[] lpbKeyState,
byte[] lpwTransKey,
int fuState);
[DllImport("user32")]
protected static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
protected static extern short GetKeyState(int vKey);
protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
protected const int WH_MOUSE_LL = 14;
protected const int WH_KEYBOARD_LL = 13;
protected const int WH_MOUSE = 7;
protected const int WH_KEYBOARD = 2;
protected const int WM_MOUSEMOVE = 0x200;
protected const int WM_LBUTTONDOWN = 0x201;
protected const int WM_RBUTTONDOWN = 0x204;
protected const int WM_MBUTTONDOWN = 0x207;
protected const int WM_LBUTTONUP = 0x202;
protected const int WM_RBUTTONUP = 0x205;
protected const int WM_MBUTTONUP = 0x208;
protected const int WM_LBUTTONDBLCLK = 0x203;
protected const int WM_RBUTTONDBLCLK = 0x206;
protected const int WM_MBUTTONDBLCLK = 0x209;
protected const int WM_MOUSEWHEEL = 0x020A;
protected const int WM_KEYDOWN = 0x100;
protected const int WM_KEYUP = 0x101;
protected const int WM_SYSKEYDOWN = 0x104;
protected const int WM_SYSKEYUP = 0x105;
protected const byte VK_SHIFT = 0x10;
protected const byte VK_CAPITAL = 0x14;
protected const byte VK_NUMLOCK = 0x90;
protected const byte VK_LSHIFT = 0xA0;
protected const byte VK_RSHIFT = 0xA1;
protected const byte VK_LCONTROL = 0xA2;
protected const byte VK_RCONTROL = 0x3;
protected const byte VK_LALT = 0xA4;
protected const byte VK_RALT = 0xA5;
protected const byte LLKHF_ALTDOWN = 0x20;
#endregion Windows API Code
#region Private Variables
protected int _hookType;
protected int _handleToHook;
protected bool _isStarted;
protected HookProc _hookCallback;
#endregion Private Variables
#region Properties
public bool IsStarted
{
get
{
return _isStarted;
}
}
#endregion Properties
#region Constructor
public GlobalHook()
{
Application.ApplicationExit += Application_ApplicationExit;
}
#endregion Constructor
#region Methods
public void Start()
{
if (!_isStarted && _hookType != 0)
{
// Make sure we keep a reference to this delegate!
// If not, GC randomly collects it, and a NullReference exception is thrown
_hookCallback = HookCallbackProcedure;
_handleToHook = SetWindowsHookEx(_hookType, _hookCallback, (IntPtr)Kernel.GetModuleHandle(null), 0);
//Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
// Were we able to sucessfully start hook?
if (_handleToHook != 0)
{
_isStarted = true;
}
}
}
public void Stop()
{
if (_isStarted)
{
UnhookWindowsHookEx(_handleToHook);
_isStarted = false;
}
}
protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
{
// This method must be overriden by each extending hook
return 0;
}
protected void Application_ApplicationExit(object sender, EventArgs e)
{
if (_isStarted)
{
Stop();
}
}
#endregion Methods
}
/// <summary>
/// Captures global keyboard events
/// </summary>
public class KeyboardHook : GlobalHook
{
#region Events
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;
public event KeyPressEventHandler KeyPress;
#endregion Events
#region Constructor
public KeyboardHook()
{
_hookType = WH_KEYBOARD_LL;
}
#endregion Constructor
#region Methods
protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
{
bool handled = false;
if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
{
KeyboardHookStruct keyboardHookStruct =
(KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
// Is Control being held down?
bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
((GetKeyState(VK_RCONTROL) & 0x80) != 0);
// Is Shift being held down?
bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
((GetKeyState(VK_RSHIFT) & 0x80) != 0);
// Is Alt being held down?
bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
((GetKeyState(VK_RALT) & 0x80) != 0);
// Is CapsLock on?
bool capslock = (GetKeyState(VK_CAPITAL) != 0);
// Create event using keycode and control/shift/alt values found above
KeyEventArgs e = new KeyEventArgs(
(Keys)(
keyboardHookStruct.vkCode |
(control ? (int)Keys.Control : 0) |
(shift ? (int)Keys.Shift : 0) |
(alt ? (int)Keys.Alt : 0)
));
// Handle KeyDown and KeyUp events
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (KeyDown != null)
{
KeyDown(this, e);
handled = e.Handled;
}
break;
case WM_KEYUP:
case WM_SYSKEYUP:
if (KeyUp != null)
{
KeyUp(this, e);
handled = e.Handled;
}
break;
}
// Handle KeyPress event
if (wParam == WM_KEYDOWN &&
!handled &&
!e.SuppressKeyPress &&
KeyPress != null)
{
byte[] keyState = new byte[256];
byte[] inBuffer = new byte[2];
GetKeyboardState(keyState);
if (ToAscii(keyboardHookStruct.vkCode,
keyboardHookStruct.scanCode,
keyState,
inBuffer,
keyboardHookStruct.flags) == 1)
{
char key = (char)inBuffer[0];
if ((capslock ^ shift) && Char.IsLetter(key))
key = Char.ToUpper(key);
KeyPressEventArgs e2 = new KeyPressEventArgs(key);
KeyPress(this, e2);
handled = e.Handled;
}
}
}
if (handled)
{
return 1;
}
else
{
return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
}
}
#endregion Methods
}
/// <summary>
/// Captures global mouse events
/// </summary>
public class MouseHook : GlobalHook
{
#region MouseEventType Enum
private enum MouseEventType
{
None,
MouseDown,
MouseUp,
DoubleClick,
MouseWheel,
MouseMove
}
#endregion MouseEventType Enum
#region Events
public event MouseEventHandler MouseDown;
public event MouseEventHandler MouseUp;
public event MouseEventHandler MouseMove;
public event MouseEventHandler MouseWheel;
public event MouseEventHandler Click;
public event MouseEventHandler DoubleClick;
#endregion Events
#region Constructor
public MouseHook()
{
_hookType = WH_MOUSE_LL;
}
#endregion Constructor
#region Methods
protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
{
if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
{
MouseLLHookStruct mouseHookStruct =
(MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
MouseButtons button = GetButton(wParam);
MouseEventType eventType = GetEventType(wParam);
MouseEventArgs e = new MouseEventArgs(
button,
(eventType == MouseEventType.DoubleClick ? 2 : 1),
mouseHookStruct.pt.x,
mouseHookStruct.pt.y,
(eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
// Prevent multiple Right Click events (this probably happens for popup menus)
if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
{
eventType = MouseEventType.None;
}
switch (eventType)
{
case MouseEventType.MouseDown:
MouseDown?.Invoke(this, e);
break;
case MouseEventType.MouseUp:
Click?.Invoke(this, e);
MouseUp?.Invoke(this, e);
break;
case MouseEventType.DoubleClick:
DoubleClick?.Invoke(this, e);
break;
case MouseEventType.MouseWheel:
MouseWheel?.Invoke(this, e);
break;
case MouseEventType.MouseMove:
MouseMove?.Invoke(this, e);
break;
}
}
return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
}
private MouseButtons GetButton(Int32 wParam)
{
switch (wParam)
{
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
return MouseButtons.Left;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
return MouseButtons.Right;
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
return MouseButtons.Middle;
default:
return MouseButtons.None;
}
}
private MouseEventType GetEventType(Int32 wParam)
{
switch (wParam)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
return MouseEventType.MouseDown;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
return MouseEventType.MouseUp;
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
return MouseEventType.DoubleClick;
case WM_MOUSEWHEEL:
return MouseEventType.MouseWheel;
case WM_MOUSEMOVE:
return MouseEventType.MouseMove;
default:
return MouseEventType.None;
}
}
#endregion Methods
}
/// <summary>
/// Standard Keyboard Shortcuts used by most applications
/// </summary>
public enum StandardShortcut
{
Copy,
Cut,
Paste,
SelectAll,
Save,
Open,
New,
Close,
Print
}
/// <summary>
/// Simulate keyboard key presses
/// </summary>
public static class KeyboardSimulator
{
#region Windows API Code
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
[DllImport("user32.dll")]
private static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
#endregion Windows API Code
#region Methods
public static void KeyDown(Keys key)
{
keybd_event(ParseKey(key), 0, 0, 0);
}
public static void KeyUp(Keys key)
{
keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
}
public static void KeyPress(Keys key)
{
KeyDown(key);
KeyUp(key);
}
public static void SimulateStandardShortcut(StandardShortcut shortcut)
{
switch (shortcut)
{
case StandardShortcut.Copy:
KeyDown(Keys.Control);
KeyPress(Keys.C);
KeyUp(Keys.Control);
break;
case StandardShortcut.Cut:
KeyDown(Keys.Control);
KeyPress(Keys.X);
KeyUp(Keys.Control);
break;
case StandardShortcut.Paste:
KeyDown(Keys.Control);
KeyPress(Keys.V);
KeyUp(Keys.Control);
break;
case StandardShortcut.SelectAll:
KeyDown(Keys.Control);
KeyPress(Keys.A);
KeyUp(Keys.Control);
break;
case StandardShortcut.Save:
KeyDown(Keys.Control);
KeyPress(Keys.S);
KeyUp(Keys.Control);
break;
case StandardShortcut.Open:
KeyDown(Keys.Control);
KeyPress(Keys.O);
KeyUp(Keys.Control);
break;
case StandardShortcut.New:
KeyDown(Keys.Control);
KeyPress(Keys.N);
KeyUp(Keys.Control);
break;
case StandardShortcut.Close:
KeyDown(Keys.Alt);
KeyPress(Keys.F4);
KeyUp(Keys.Alt);
break;
case StandardShortcut.Print:
KeyDown(Keys.Control);
KeyPress(Keys.P);
KeyUp(Keys.Control);
break;
}
}
private static byte ParseKey(Keys key)
{
// Alt, Shift, and Control need to be changed for API function to work with them
switch (key)
{
case Keys.Alt:
return 18;
case Keys.Control:
return 17;
case Keys.Shift:
return 16;
default:
return (byte)key;
}
}
#endregion Methods
}
/// <summary>
/// And X, Y point on the screen
/// </summary>
public struct MousePoint
{
public MousePoint(Point p)
{
X = p.X;
Y = p.Y;
}
public int X;
public int Y;
public static implicit operator Point(MousePoint p)
{
return new Point(p.X, p.Y);
}
}
/// <summary>
/// Mouse buttons that can be pressed
/// </summary>
public enum MouseButton
{
Left = 0x2,
Right = 0x8,
Middle = 0x20
}
/// <summary>
/// Operations that simulate mouse events
/// </summary>
public static class MouseSimulator
{
#region Windows API Code
[DllImport("user32.dll")]
private static extern int ShowCursor(bool show);
[DllImport("user32.dll")]
private static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
private const int MOUSEEVENTF_MOVE = 0x1;
private const int MOUSEEVENTF_LEFTDOWN = 0x2;
private const int MOUSEEVENTF_LEFTUP = 0x4;
private const int MOUSEEVENTF_RIGHTDOWN = 0x8;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
private const int MOUSEEVENTF_MIDDLEUP = 0x40;
private const int MOUSEEVENTF_WHEEL = 0x800;
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
#endregion Windows API Code
#region Properties
/// <summary>
/// Gets or sets a structure that represents both X and Y mouse coordinates
/// </summary>
public static MousePoint Position
{
get
{
return new MousePoint(Cursor.Position);
}
set
{
Cursor.Position = value;
}
}
/// <summary>
/// Gets or sets only the mouse's x coordinate
/// </summary>
public static int X
{
get
{
return Cursor.Position.X;
}
set
{
Cursor.Position = new Point(value, Y);
}
}
/// <summary>
/// Gets or sets only the mouse's y coordinate
/// </summary>
public static int Y
{
get
{
return Cursor.Position.Y;
}
set
{
Cursor.Position = new Point(X, value);
}
}
#endregion Properties
#region Methods
/// <summary>
/// Press a mouse button down
/// </summary>
/// <param name="button"></param>
public static void MouseDown(MouseButton button)
{
mouse_event(((int)button), 0, 0, 0, 0);
}
public static void MouseDown(MouseButtons button)
{
switch (button)
{
case MouseButtons.Left:
MouseDown(MouseButton.Left);
break;
case MouseButtons.Middle:
MouseDown(MouseButton.Middle);
break;
case MouseButtons.Right:
MouseDown(MouseButton.Right);
break;
}
}
/// <summary>
/// Let a mouse button up
/// </summary>
/// <param name="button"></param>
public static void MouseUp(MouseButton button)
{
mouse_event(((int)button) * 2, 0, 0, 0, 0);
}
public static void MouseUp(MouseButtons button)
{
switch (button)
{
case MouseButtons.Left:
MouseUp(MouseButton.Left);
break;
case MouseButtons.Middle:
MouseUp(MouseButton.Middle);
break;
case MouseButtons.Right:
MouseUp(MouseButton.Right);
break;
}
}
/// <summary>
/// Click a mouse button (down then up)
/// </summary>
/// <param name="button"></param>
public static void Click(MouseButton button)
{
MouseDown(button);
MouseUp(button);
}
public static void Click(MouseButtons button)
{
switch (button)
{
case MouseButtons.Left:
Click(MouseButton.Left);
break;
case MouseButtons.Middle:
Click(MouseButton.Middle);
break;
case MouseButtons.Right:
Click(MouseButton.Right);
break;
}
}
/// <summary>
/// Double click a mouse button (down then up twice)
/// </summary>
/// <param name="button"></param>
public static void DoubleClick(MouseButton button)
{
Click(button);
Click(button);
}
public static void DoubleClick(MouseButtons button)
{
switch (button)
{
case MouseButtons.Left:
DoubleClick(MouseButton.Left);
break;
case MouseButtons.Middle:
DoubleClick(MouseButton.Middle);
break;
case MouseButtons.Right:
DoubleClick(MouseButton.Right);
break;
}
}
/// <summary>
/// Show a hidden current on currently application
/// </summary>
public static void Show()
{
ShowCursor(true);
}
/// <summary>
/// Hide mouse cursor only on current application's forms
/// </summary>
public static void Hide()
{
ShowCursor(false);
}
#endregion Methods
}
}

View File

@ -266,7 +266,7 @@ namespace Sunny.UI
UILocalize.Saturday = "SAT"; UILocalize.Saturday = "SAT";
UILocalize.Sunday = "SUN"; UILocalize.Sunday = "SUN";
UILocalize.Prev = "Previous"; UILocalize.Prev = "Prev";
UILocalize.Next = "Next"; UILocalize.Next = "Next";
UILocalize.SelectPageLeft = "Page"; UILocalize.SelectPageLeft = "Page";
UILocalize.SelectPageRight = ""; UILocalize.SelectPageRight = "";