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;
|
}
|
}
|
}
|