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
{
///
/// 得到屏幕大小
///
///
///
public static void GetWindowSize(out Int32 width, out Int32 height)
{
GetWindowSize(GetDesktopWindow(), out width, out height);
}
///
/// 得到屏幕大小
///
///
///
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;
}
///
/// 获取颜色
///
/// 鼠标相对于显示器的坐标X
/// 鼠标相对于显示器的坐标Y
///
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;
}
///
/// 获取颜色
///
///
///
///
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;
}
///
/// 获取颜色
///
///
///
///
///
///
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);
}
///
/// Num Lock是否打开
///
///
public static bool NumLockIsOn()
{
return (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
}
///
/// CapsLock是否打开
///
///
public static bool CapsLockIsOn()
{
return (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
}
///
/// ScrollLock是否打开
///
///
public static bool ScrollLockIsOn()
{
return (((ushort)GetKeyState(0x91)) & 0xffff) != 0; ;
}
///
/// 得到当前鼠标位置
///
///
///
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();
///
/// 获取指定窗口的设备场景
///
/// 将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC
/// 指定窗口的设备场景句柄,出错则为0
[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);
///
/// 在指定的设备场景中取得一个像素的RGB值
///
/// 一个设备场景的句柄
/// 逻辑坐标中要检查的横坐标
/// 逻辑坐标中要检查的纵坐标
/// 指定点的颜色
[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);
///
/// 取得键状态的API函数
///
///
///
[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
}
}