asmrobot
2019-11-25 2aeab450471cb80b59002da7da80faf251a0c4f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
    }
}