using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.Linq;
|
using System.Runtime.InteropServices;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace RichCreator.Utility
|
{
|
public sealed class Utils
|
{
|
|
|
/// <summary>
|
/// 得到屏幕大小
|
/// </summary>
|
/// <param name="width"></param>
|
/// <param name="height"></param>
|
public static void GetWindowSize(out Int32 width, out Int32 height)
|
{
|
GetWindowSize(GetDesktopWindow(), out width, out height);
|
}
|
|
/// <summary>
|
/// 得到屏幕大小
|
/// </summary>
|
/// <param name="width"></param>
|
/// <param name="height"></param>
|
private static void GetWindowSize(IntPtr hWnd, out Int32 width, out Int32 height)
|
{
|
Rect windowRect = new Rect();
|
GetWindowRect(hWnd, ref windowRect);
|
width = windowRect.right - windowRect.left;
|
height = windowRect.bottom - windowRect.top;
|
}
|
|
|
/// <summary>
|
/// 获取颜色
|
/// </summary>
|
/// <param name="x">鼠标相对于显示器的坐标X</param>
|
/// <param name="y">鼠标相对于显示器的坐标Y</param>
|
/// <returns></returns>
|
public static Color GetColor(int x, int y)
|
{
|
byte r = 0;
|
byte g = 0;
|
byte b = 0;
|
GetColor(x, y, out r, out g, out b);
|
Color color = Color.FromArgb(r, g, b);
|
|
return color;
|
}
|
|
/// <summary>
|
/// 获取颜色
|
/// </summary>
|
/// <param name="x"></param>
|
/// <param name="y"></param>
|
/// <returns></returns>
|
public static System.Windows.Media.Color GetWPFColor(Int32 x, Int32 y)
|
{
|
byte r = 0;
|
byte g = 0;
|
byte b = 0;
|
GetColor(x, y, out r, out g, out b);
|
System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(0xff,r, g, b);
|
return color;
|
}
|
|
/// <summary>
|
/// 获取颜色
|
/// </summary>
|
/// <param name="x"></param>
|
/// <param name="y"></param>
|
/// <param name="r"></param>
|
/// <param name="g"></param>
|
/// <param name="b"></param>
|
public static void GetColor(Int32 x, Int32 y, out byte r, out byte g, out byte b)
|
{
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
uint pixel = GetPixel(hdc, x, y);
|
ReleaseDC(IntPtr.Zero, hdc);
|
r = (byte)(pixel & 0x000000FF);
|
g = (byte)((pixel & 0x0000FF00) >> 8);
|
b = (byte)((pixel & 0x00FF0000) >> 16);
|
}
|
|
/// <summary>
|
/// Num Lock是否打开
|
/// </summary>
|
/// <returns></returns>
|
public static bool NumLockIsOn()
|
{
|
return (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
|
}
|
|
/// <summary>
|
/// CapsLock是否打开
|
/// </summary>
|
/// <returns></returns>
|
public static bool CapsLockIsOn()
|
{
|
return (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
|
}
|
|
/// <summary>
|
/// ScrollLock是否打开
|
/// </summary>
|
/// <returns></returns>
|
public static bool ScrollLockIsOn()
|
{
|
return (((ushort)GetKeyState(0x91)) & 0xffff) != 0; ;
|
}
|
|
|
/// <summary>
|
/// 得到当前鼠标位置
|
/// </summary>
|
/// <param name="x"></param>
|
/// <param name="y"></param>
|
public static void GetCursorPoint(out Int32 x, out Int32 y)
|
{
|
Utils.MarshalPoint point = new Utils.MarshalPoint();//必须用与之相兼容的结构体,类也可以
|
Utils.GetCursorPos(ref point);//获取当前鼠标坐标
|
x = point.X;
|
y = point.Y;
|
}
|
|
#region DLL Import
|
|
[DllImport("user32.dll")]
|
internal static extern IntPtr GetDesktopWindow();
|
/// <summary>
|
/// 获取指定窗口的设备场景
|
/// </summary>
|
/// <param name="hwnd">将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC</param>
|
/// <returns>指定窗口的设备场景句柄,出错则为0</returns>
|
[DllImport("user32.dll")]
|
private static extern IntPtr GetDC(IntPtr hwnd);
|
|
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
|
[DllImport("gdi32.dll")]
|
internal static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
|
|
[DllImport("gdi32.dll")]
|
internal static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
|
|
[DllImport("gdi32.dll")]
|
internal static extern IntPtr CreateCompatibleDC(IntPtr hDC);
|
|
[DllImport("gdi32.dll")]
|
internal static extern bool DeleteDC(IntPtr hDC);
|
|
[DllImport("gdi32.dll")]
|
internal static extern bool DeleteObject(IntPtr hObject);
|
|
[DllImport("gdi32.dll")]
|
internal static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
|
|
|
|
|
[DllImport("user32.dll")]
|
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
|
|
|
[DllImport("user32.dll")]
|
internal static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
|
[DllImport("user32.dll")]
|
internal static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
|
|
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
|
internal static extern int GetCursorPos(ref MarshalPoint point);
|
|
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
|
internal static extern int WindowFromPoint(int x, int y);
|
|
/// <summary>
|
/// 在指定的设备场景中取得一个像素的RGB值
|
/// </summary>
|
/// <param name="hdc">一个设备场景的句柄</param>
|
/// <param name="x">逻辑坐标中要检查的横坐标</param>
|
/// <param name="y">逻辑坐标中要检查的纵坐标</param>
|
/// <returns>指定点的颜色</returns>
|
[DllImport("gdi32.dll")]
|
private static extern uint GetPixel(IntPtr hdc, int x, int y);
|
|
|
//控制键CapLocks状态的API函数
|
[DllImport("user32.dll")]
|
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
|
/// <summary>
|
/// 取得键状态的API函数
|
/// </summary>
|
/// <param name="keyCode"></param>
|
/// <returns></returns>
|
[DllImport("user32.dll",CharSet = CharSet.Auto,ExactSpelling = true,CallingConvention = CallingConvention.Winapi)]
|
public static extern short GetKeyState(int keyCode);
|
|
|
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
|
internal struct MarshalPoint
|
{
|
public int X;
|
public int Y;
|
}
|
|
[StructLayout(LayoutKind.Sequential)]
|
internal struct Rect
|
{
|
public int left;
|
public int top;
|
public int right;
|
public int bottom;
|
}
|
#endregion
|
}
|
}
|