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
{
///
/// 得到wegame窗口
///
///
public static bool GetWeGameRect(out ZTRectangle windowRect)
{
return GetWindowRect(out windowRect, "WeGame", "TWINCONTROL");
}
///
/// 得到DNF窗口
///
///
///
public static bool GetDnfRect(out ZTRectangle windowRect)
{
//地下城与勇士
return GetWindowRect(out windowRect, "地下城与勇士", "地下城与勇士");
}
///
/// 设置Dnf为顶层窗体
///
///
public static bool SetDnfToTop()
{
WindowInfo info = GetWindowInfo("地下城与勇士", "地下城与勇士");
if (info.WindowHandle != IntPtr.Zero)
{
SetForegroundWindow(info.WindowHandle);
return true;
}
return false;
}
///
/// 设置本程序为提层
///
///
public static bool SetSelfToTop()
{
WindowInfo info = GetWindowInfo("富豪自动创造机");
if (info.WindowHandle != IntPtr.Zero)
{
SetForegroundWindow(info.WindowHandle);
return true;
}
return false;
}
///
/// WeGame窗口是否顶端
///
///
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;
}
///
/// Dnf窗口是否顶端
///
///
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;
}
///
/// 获取窗口矩形
///
///
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;
}
///
/// 获取某窗口信息
///
///
///
///
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 };
}
///
/// 获取某窗口信息
///
///
///
///
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 };
}
///
///获取当前顶端窗口
///
///
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
///
/// 设置某窗口为顶端窗口
///
///
///
[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);
///
/// 窗体方格信息
///
[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;
}
}
///
/// 获取所有窗体信息
///
///
public static WindowInfo[] GetAllDesktopWindows()
{
//用来保存窗口对象 列表
List wndList = new List();
//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();
}
///
/// 得到顶层窗口信息
///
///
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;
}
}
}