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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using RichCreator.Utility.Structs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
 
namespace RichCreator.Utility.Utilitys
{
    public class WindowUtils
    {
        /// <summary>
        /// 得到wegame窗口
        /// </summary>
        /// <returns></returns>
        public static bool GetWeGameRect(out ZTRectangle windowRect)
        {
            return GetWindowRect(out windowRect, "WeGame", "TWINCONTROL");
        }
 
        /// <summary>
        /// 得到DNF窗口
        /// </summary>
        /// <param name="windowRect"></param>
        /// <returns></returns>
        public static bool GetDnfRect(out ZTRectangle windowRect)
        {
            //地下城与勇士
            return GetWindowRect(out windowRect, "地下城与勇士", "地下城与勇士");
        }
 
        /// <summary>
        /// 设置Dnf为顶层窗体
        /// </summary>
        /// <returns></returns>
        public static bool SetDnfToTop()
        {
            WindowInfo info = GetWindowInfo("地下城与勇士", "地下城与勇士");
            if (info.WindowHandle != IntPtr.Zero)
            {
                SetForegroundWindow(info.WindowHandle);
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 设置本程序为提层
        /// </summary>
        /// <returns></returns>
        public static bool SetSelfToTop()
        {
            WindowInfo info = GetWindowInfo("富豪自动创造机");
            if (info.WindowHandle != IntPtr.Zero)
            {
                SetForegroundWindow(info.WindowHandle);
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// WeGame窗口是否顶端
        /// </summary>
        /// <returns></returns>
        public static bool WeGameWindowIsTop()
        {
            WindowInfo info = GetForegroundWindowInfo();
            if (info == default(WindowInfo))
            {
                return false;
            }
            if (info.WindowName.Equals("WeGame", StringComparison.OrdinalIgnoreCase) && info.ClassName.Equals("TWINCONTROL", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// Dnf窗口是否顶端
        /// </summary>
        /// <returns></returns>
        public static bool DnfWindowIsTop()
        {
            WindowInfo info = GetForegroundWindowInfo();
            if (info == default(WindowInfo))
            {
                return false;
            }
            if (info.WindowName.Equals("地下城与勇士", StringComparison.OrdinalIgnoreCase) && info.ClassName.Equals("地下城与勇士", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 获取窗口矩形
        /// </summary>
        /// <returns></returns>
        public static bool GetWindowRect(out ZTRectangle windowRect,string windowName,string className)
        {
            bool hasWindow = false;
            windowRect = ZTRectangle.Empty;
            var list = WindowUtils.GetAllDesktopWindows();
            foreach (var item in list)
            {
                if (item.WindowName.Equals(windowName, StringComparison.OrdinalIgnoreCase) &&
                    item.ClassName.Equals(className, StringComparison.OrdinalIgnoreCase))
                {
                    hasWindow = true;
                    Rect r = new Rect();
                    GetWindowRect(item.WindowHandle, ref r);
                    if (r.top <= 0 &&
                        r.bottom <= 0 &&
                        r.left <= 0 &&
                        r.right <= 0)
                    {
                        continue;
                    }
 
                    windowRect = new ZTRectangle(r.left, r.top, r.right, r.bottom);
                    return true;
                }
            }
            return hasWindow;
        }
 
        /// <summary>
        /// 获取某窗口信息
        /// </summary>
        /// <param name="windowName"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public static WindowInfo GetWindowInfo(string windowName, string className)
        {
            var list = WindowUtils.GetAllDesktopWindows();
            foreach (var item in list)
            {
                if (item.WindowName.Equals(windowName, StringComparison.OrdinalIgnoreCase) &&
                    item.ClassName.Equals(className, StringComparison.OrdinalIgnoreCase))
                {
 
                    return item;
                }
            }
            return new WindowInfo() { WindowHandle = IntPtr.Zero, WindowName = windowName, ClassName = className };
        }
 
 
        /// <summary>
        /// 获取某窗口信息
        /// </summary>
        /// <param name="windowName"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public static WindowInfo GetWindowInfo(string windowName)
        {
            var list = WindowUtils.GetAllDesktopWindows();
            foreach (var item in list)
            {
                if (item.WindowName.Equals(windowName, StringComparison.OrdinalIgnoreCase))
                {
 
                    return item;
                }
            }
            return new WindowInfo() { WindowHandle = IntPtr.Zero, WindowName = windowName, ClassName = string.Empty  };
        }
 
        /// <summary>
        ///获取当前顶端窗口
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
 
 
        /// <summary>
        /// 设置某窗口为顶端窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
 
        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄       
        private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
 
        [DllImport("user32.dll")]
        private extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref Rect lpRect);
 
        [DllImport("User32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern bool UpdateWindow(IntPtr hWnd);
 
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
 
        //用来遍历所有窗口 
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
 
        //获取窗口Text 
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 
        //获取窗口类名 
        [DllImport("user32.dll")]
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 
        /// <summary>
        /// 窗体方格信息
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        internal struct Rect
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
 
 
        
 
        //自定义一个类,用来保存句柄信息,在遍历的时候,随便也用空上句柄来获取些信息,呵呵 
        public struct WindowInfo
        {
            public IntPtr WindowHandle;
            public string WindowName;
            public string ClassName;
 
 
 
            public static bool operator ==(WindowInfo one, WindowInfo two)
            {
                if (one.WindowHandle == two.WindowHandle && one.WindowName == two.WindowName && one.ClassName == two.ClassName)
                {
                    return true;
                }
                return false;
            }
 
            public static bool operator !=(WindowInfo one, WindowInfo two)
            {
                if (one.WindowHandle == two.WindowHandle && one.WindowName == two.WindowName && one.ClassName == two.ClassName)
                {
                    return false;
                }
                return true;
            }
 
        }
 
        /// <summary>
        /// 获取所有窗体信息
        /// </summary>
        /// <returns></returns>
        public static WindowInfo[] GetAllDesktopWindows()
        {
            //用来保存窗口对象 列表
            List<WindowInfo> wndList = new List<WindowInfo>();
 
            //enum all desktop windows 
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
 
                //get hwnd 
                wnd.WindowHandle = hWnd;
 
                //get window name  
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.WindowName = sb.ToString();
 
                //get window class 
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.ClassName = sb.ToString();
 
                //add it into list 
                wndList.Add(wnd);
                return true;
            }, 0);
 
            return wndList.ToArray();
        }
 
 
        /// <summary>
        /// 得到顶层窗口信息
        /// </summary>
        /// <returns></returns>
        public static WindowInfo GetForegroundWindowInfo()
        {
            IntPtr foregroundWindow = GetForegroundWindow();
            if (foregroundWindow == IntPtr.Zero)
            {
                return default(WindowInfo);
            }
            
 
            WindowInfo wnd = new WindowInfo();
            StringBuilder sb = new StringBuilder(256);
 
            //get hwnd 
            wnd.WindowHandle = foregroundWindow;
 
            //get window name  
            GetWindowTextW(foregroundWindow, sb, sb.Capacity);
            wnd.WindowName = sb.ToString();
 
            //get window class 
            GetClassNameW(foregroundWindow, sb, sb.Capacity);
            wnd.ClassName = sb.ToString();
            
            return wnd;
        }
    }
}