2.1 修改指定窗口标题
//修改指定窗口标题
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowText(IntPtr hWnd, string text);
/// <summary>
/// 设置窗口标题
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="newtext">新标题</param>
private void SetWindowText(int Hwnd, string newtext)
{
SetWindowText((IntPtr)Hwnd, newtext);
}
//****************************调用***************************
int Hwnd = 3281078;
SetWindowText(Hwnd, "123");
2.2 判断窗口是否存在
//判断窗口是否存在
[DllImport("user32", EntryPoint = "IsWindow")]
private static extern bool IsWindow(IntPtr hWnd);
/// <summary>
/// 判断窗口是否存在
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <returns>存在返回 true 不存在返回 false</returns>
public bool IsWindow(int Hwnd)
{
if (IsWindow((IntPtr)Hwnd))
{
return true;
}
return false;
}
//****************************调用***************************
int Hwnd = 2100400;
if (IsWindow(Hwnd))
{
//如果存在
}
2.3 判断窗口状态
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsZoomed(IntPtr hWnd);
//获得顶层窗口
[DllImport("user32", EntryPoint = "GetForegroundWindow")]
private static extern IntPtr GetForegroundwindow();
/// <summary>
/// 获得顶层窗口
/// </summary>
/// <returns>返回 窗口句柄</returns>
public int GetForeGroundWindow()
{
return (int)GetForegroundwindow();
}
/// <summary>
/// 得到窗口状态
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="flag">
/// 操作方式
/// 1:判断窗口是否最小化
/// 2:判断窗口是否最大化
/// 3:判断窗口是否激活
/// </param>
/// <returns>满足条件返回 true</returns>
public bool GetWindowState(int Hwnd, int flag)
{
switch (flag)
{
case 1:
return IsIconic((IntPtr)Hwnd);
case 2:
return IsZoomed((IntPtr)Hwnd);
case 3:
if (Hwnd != GetForeGroundWindow())
{
return false;
}
break;
}
return true;
}
2.4 获取窗口大小及左上角坐标
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsZoomed(IntPtr hWnd);
//获得顶层窗口
[DllImport("user32", EntryPoint = "GetForegroundWindow")]
private static extern IntPtr GetForegroundwindow();
/// <summary>
/// 获得顶层窗口
/// </summary>
/// <returns>返回 窗口句柄</returns>
public int GetForeGroundWindow()
{
return (int)GetForegroundwindow();
}
/// <summary>
/// 得到窗口状态
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="flag">
/// 操作方式
/// 1:判断窗口是否最小化
/// 2:判断窗口是否最大化
/// 3:判断窗口是否激活
/// </param>
/// <returns>满足条件返回 true</returns>
public bool GetWindowState(int Hwnd, int flag)
{
switch (flag)
{
case 1:
return IsIconic((IntPtr)Hwnd);
case 2:
return IsZoomed((IntPtr)Hwnd);
case 3:
if (Hwnd != GetForeGroundWindow())
{
return false;
}
break;
}
return true;
}
2.5 获取窗口客户区大小
//得到窗口客户区大小
[DllImport("user32.dll")]
private static extern int GetClientRect(IntPtr hwnd, out Rect lpRect);
/// <summary>
/// 一个窗口区域的结构体
/// </summary>
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
/// <summary>
/// 得到窗口客户区大小 - 客户区因为是相对而言,他的左上角坐标永远都是0,0
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="Z">宽度</param>
/// <param name="W">高度</param>
public void GetClientRect(int Hwnd, out int Weight, out int High)
{
int X = 0, Y = 0, Z = 0, W = 0;
Rect rect = new Rect();
GetClientRect((IntPtr)Hwnd, out rect);
X = rect.Left;
Y = rect.Right;
Z = rect.Top;
W = rect.Bottom;
Weight = Y - X;
High = W - Z;
}
//****************************调用***************************
int Hwnd = 1510624;
int Z = 0, W = 0;
GetClientRect(Hwnd, out Z, out W);
2.6 获取窗口标题
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd,//窗口句柄
StringBuilder lpString,//标题
int nMaxCount //最大值
);
/// </summary>
/// 得到指定窗口标题
/// <summary>
/// <param name="hWnd">句柄</param>
/// <returns>找不到返回""</returns>
public string GetTitleName(int hWnd)
{
StringBuilder title = new StringBuilder(256);
if (GetWindowText((IntPtr)hWnd, title, title.Capacity) == 0)
{
return "";