@@ -17,5 +17,104 @@ public class Win32APIHelper
1717 [ DllImport ( "user32.dll" , SetLastError = true ) ]
1818 [ return : MarshalAs ( UnmanagedType . Bool ) ]
1919 public static extern bool GetCursorPos ( out Point lpPoint ) ;
20+
21+ #region 窗口类
22+ /// <summary>
23+ /// 获取窗口标题
24+ /// </summary>
25+ /// <param name="hWnd"></param>
26+ /// <param name="lpString"></param>
27+ /// <param name="nMaxCount"></param>
28+ /// <returns></returns>
29+ [ DllImport ( "user32" , SetLastError = true ) ]
30+ public static extern int GetWindowText ( IntPtr hWnd , StringBuilder lpString , int nMaxCount ) ;
31+ /// <summary>
32+ /// 获取窗口类名
33+ /// </summary>
34+ /// <param name="hWnd"></param>
35+ /// <param name="lpString"></param>
36+ /// <param name="nMaxCount"></param>
37+ /// <returns></returns>
38+ [ DllImport ( "user32.dll" ) ]
39+ public static extern int GetClassName ( IntPtr hWnd , StringBuilder lpString , int nMaxCount ) ;
40+ /// <summary>
41+ /// 获取当前焦点窗口句柄
42+ /// </summary>
43+ /// <returns></returns>
44+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , ExactSpelling = true ) ]
45+ public static extern IntPtr GetForegroundWindow ( ) ;
46+
47+ /// <summary>
48+ /// 获取窗口位置
49+ /// </summary>
50+ /// <param name="hWnd"></param>
51+ /// <param name="lpRect"></param>
52+ /// <returns></returns>
53+ [ DllImport ( "user32.dll" ) ]
54+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
55+ public static extern bool GetWindowRect ( IntPtr hWnd , ref RECT lpRect ) ;
56+
57+ [ StructLayout ( LayoutKind . Sequential ) ]
58+ public struct RECT
59+ {
60+ public int Left ; //最左坐标
61+ public int Top ; //最上坐标
62+ public int Right ; //最右坐标
63+ public int Bottom ; //最下坐标
64+ }
65+ /// <summary>
66+ /// 窗口信息结构
67+ /// </summary>
68+ public struct WindowInfo
69+ {
70+ /// <summary>
71+ /// 窗口宽度
72+ /// </summary>
73+ public int Width ;
74+ /// <summary>
75+ /// 窗口高度
76+ /// </summary>
77+ public int Height ;
78+ /// <summary>
79+ /// 窗口标题
80+ /// </summary>
81+ public string Title ;
82+ /// <summary>
83+ /// 窗口类名
84+ /// </summary>
85+ public string ClassName ;
86+ /// <summary>
87+ /// 是否全屏
88+ /// </summary>
89+ public bool IsFullScreen ;
90+ }
91+ /// <summary>
92+ /// 获取当前焦点窗口信息
93+ /// </summary>
94+ /// <returns></returns>
95+ public static WindowInfo GetFocusWindowInfo ( )
96+ {
97+ var result = new WindowInfo ( ) ;
98+ //获取当前焦点窗口句柄
99+ IntPtr intPtr = GetForegroundWindow ( ) ;
100+ //获取窗口大小
101+ RECT rect = new RECT ( ) ;
102+ GetWindowRect ( intPtr , ref rect ) ;
103+ result . Width = rect . Right - rect . Left ;
104+ result . Height = rect . Bottom - rect . Top ;
105+ //获取窗口标题
106+ StringBuilder title = new StringBuilder ( 256 ) ;
107+ GetWindowText ( intPtr , title , title . Capacity ) ;
108+ result . Title = title . ToString ( ) ;
109+ //获取窗口类名
110+ StringBuilder className = new StringBuilder ( 256 ) ;
111+ GetClassName ( intPtr , className , className . Capacity ) ;
112+ result . ClassName = className . ToString ( ) ;
113+ //判断全屏
114+ result . IsFullScreen = result . Width >= SystemParameters . PrimaryScreenWidth && result . Height >= SystemParameters . PrimaryScreenHeight ;
115+
116+ return result ;
117+ }
118+ #endregion
20119 }
21120}
0 commit comments