C#操作实例总结(二)—— 窗口操作

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 "";
    }
    return title.ToString();
}

//****************************调用***************************

string Hwnd_Title = GetTitleName(11535126);

2.7 获取窗口的父窗口句柄

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);

/// <summary>
/// 得到窗口上一级窗口的句柄
/// </summary>
/// <param name="ChildHwnd">子窗口句柄</param>
/// <returns> 返回 窗口句柄 找不到返回 0</returns>
public int GetChild_Host(int ChildHwnd)
{
    return (int)GetParent((IntPtr)ChildHwnd);
}

//****************************调用***************************

int Hwnd = 1180962;
MessageBox.Show(GetChild_Host(Hwnd).ToString());

2.8 获取窗口类名

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

/// </summary>
/// 得到指定窗口类名
/// <summary>
/// <param name="hWnd">句柄</param>
/// <returns>找不到返回""</returns>
public string GetClassName(int hWnd)
{
    StringBuilder lpClassName = new StringBuilder(128);
    if (GetClassName((IntPtr)hWnd, lpClassName, lpClassName.Capacity) == 0)
    {
        return "";
    }
    return lpClassName.ToString();
}

//****************************调用***************************

string Hwnd_Class = GetClassName(11535126);

2.9 获取鼠标指向的窗口句柄

[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public static extern bool GetCursorPos(out Point pt);
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
public static extern IntPtr WindowFromPoint(Point pt);


//鼠标位置的坐标
public Point GetCursorPosPoint()
{
    Point p = new Point();
    if (GetCursorPos(out p))
    {
        return p;
    }
    return default(Point);
}


/// </summary>
/// 找到句柄
/// <param name="p">指定位置坐标</param>
/// <returns></returns>
public int GetHandle(Point p)
{
    return (int)WindowFromPoint(p);
}

/// <summary>
/// 得到鼠标指向的窗口句柄
/// </summary>
/// <returns>找不到则返回-1</returns>
public int GetMoustPointWindwsHwnd()
{
    try
    {
        return GetHandle(GetCursorPosPoint());
    }
    catch (System.Exception ex)
    {

    }
    return -1;
}

//****************************调用***************************


int Hwnd = GetMoustPointWindwsHwnd();

2.10 由坐标获取窗口句柄

//根据坐标获取窗口句柄
[DllImport("user32")]
private static extern IntPtr WindowFromPoint(
    Point Point  //坐标
);

/// <summary>
/// 得到指定坐标的窗口句柄
/// </summary>
/// <param name="x">X坐标</param>
/// <param name="y">Y坐标</param>
/// <returns>找不到返回 0</returns>
public int FindPointWindow(int x, int y)
{
    Point p = new Point(x, y);
    IntPtr formHandle = WindowFromPoint(p);//得到窗口句柄
    return (int)formHandle;
}

//****************************调用***************************

int Hwnd = FindPointWindow(100, 200);

2.11 根据标题和类名查找窗口

[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);

/// </summary>
/// 根据标题和类名找句柄
/// <summary>
/// <param name="IpClassName">窗口类名 如果为"" 则只根据标题查找</param>
/// <param name="IpClassName">窗口标题 如果为"" 则只根据类名查找</param>
/// <returns>找不到则返回0</returns>
public int FindWindowHwnd(string IpClassName, string IpTitleName)
{
    if (IpTitleName == "" && IpClassName != "")
    {
        return (int)FindWindow(IpClassName, null);
    }
    else if (IpClassName == "" && IpTitleName != "")
    {
        return (int)FindWindow(null, IpTitleName);
    }
    else if (IpClassName != "" && IpTitleName != "")
    {
        return (int)FindWindow(IpClassName, IpTitleName);
    }
    return 0;
}

//****************************调用***************************

int Hwnd = FindWindowHwnd("#32770", "放大镜");

int Hwnd = FindWindowHwnd("", "放大镜");

int Hwnd = FindWindowHwnd("#32770", "");

2.12 根据窗口标题模糊查找窗口句柄

/// <summary>
/// 根据窗口标题模糊查找窗口句柄
/// </summary>
/// <param name="title">窗口标题</param>
/// <returns>返回 窗口句柄 找不到返回 0</returns>
public int FindWindow_ByTitle(string title)
{
    //按照窗口标题来寻找窗口句柄
    Process[] ps = Process.GetProcesses();
    string WindowHwnd = "";
    foreach (Process p in ps)
    {
        if (p.MainWindowTitle.IndexOf(title) != -1)
        {
            WindowHwnd = p.MainWindowHandle.ToString();
            return int.Parse(WindowHwnd);
        }
    }
    return 0;
}

//****************************调用***************************

int Hwnd = FindWindow_ByTitle("新建文本文档");
if (Hwnd != 0)
{
    //表示找到窗口
}

2.13 根据进程名查找窗口句柄

/// <summary>
/// 根据进程名获得窗口句柄 - 不需要带上进程后缀
/// </summary>
/// <param name="ProssName">进程名</param>
/// <returns>窗口句柄 多个用"|"隔开 找不到返回 ""</returns>
private string FindWindowEx_ByProcessName(string ProssName)
{
    string Hwnd = "";
    Process[] pp = Process.GetProcessesByName(ProssName);
    for (int i = 0; i < pp.Length; i++)
    {
        if (pp[i].ProcessName == ProssName)
        {
            if (Hwnd == "")
            {
                Hwnd = pp[i].MainWindowHandle.ToString();
            }
            else
            {
                Hwnd = Hwnd + "|" + pp[i].MainWindowHandle.ToString();
            }
        }
    }
    return Hwnd;
}

//****************************调用***************************

string HwndList = FindWindowEx_ByProcessName("notepad");
if (HwndList != "")
{
    string[] HwndSplit = HwndList.Split('|');
    //找到以后数组中每个元素为每个进程PID
}

2.14 移动窗口

[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

//得到窗口大小和坐标
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);

/// <summary>
/// 一个窗口区域的结构体
/// </summary>
public struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

/// <summary>
/// 得到窗口大小和坐标
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="LeftTop_X">左上角X</param>
/// <param name="LeftTop_Y">左上角Y</param>
/// <param name="Z">宽度</param>
/// <param name="W">高度</param>
public void GetWindowRect(int Hwnd, out int LeftTop_X, out int LeftTop_Y, out int Weight, out int High)
{
    int X = 0, Y = 0, Z = 0, W = 0;
    Rect rect = new Rect();
    GetWindowRect((IntPtr)Hwnd, out rect);
    X = rect.Left;
    Y = rect.Right;
    Z = rect.Top;
    W = rect.Bottom;
    LeftTop_X = X;
    LeftTop_Y = Z;
    Weight = Y - X;
    High = W - Z;
}

/// <summary>
/// 不改变尺寸移动窗口到指定位置
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="X">目的地左上角X</param>
/// <param name="Y">目的地左上角Y</param>
/// <returns>移动成功返回 true</returns>
public bool MoveWindow(int Hwnd, int X, int Y)
{
    int X_1 = 0, Y_1 = 0, Z_1 = 0, W_1 = 0;
    GetWindowRect(Hwnd, out X_1, out Y_1, out Z_1, out W_1);
    return MoveWindow((IntPtr)Hwnd, X, Y, Z_1, W_1, true);
}

/// <summary>
/// 改变尺寸移动窗口到指定位置
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="X">目的地左上角X</param>
/// <param name="Y">目的地左上角Y</param>
/// <param name="Width">新宽度</param>
/// <param name="Height">新高度</param>
/// <returns>移动成功返回 true</returns>
public bool MoveWindow(int Hwnd, int X, int Y, int Width, int Height)
{
    return MoveWindow((IntPtr)Hwnd, X, Y, Width, Height, true);
}

//****************************调用***************************

int Hwnd = 2688916;

//不改变窗口大小,将窗口移动到0,0

MoveWindow(Hwnd, 0, 0);

//改变窗口大小为800,600,将窗口移动到0,0
MoveWindow(Hwnd, 0, 0, 800, 600);

2.15 窗口绑架

[DllImport("user32.dll")]
private static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
                                        int X, int Y, int cx, int cy, uint uFlags);

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool ShowWindow(IntPtr hWnd, short State);


private const int HWND_TOP = 0x0;
private const int WM_COMMAND = 0x0112;
private const int WM_QT_PAINT = 0xC2DC;
private const int WM_PAINT = 0x000F;
private const int WM_SIZE = 0x0005;
private const int SWP_FRAMECHANGED = 0x0020;

public enum Cmd_SHOWWINDOWS : int
{
    SW_SHOWNORMAL = 1,
    SW_NORMAL = 1,
    SW_SHOWMINIMIZED = 2,
    SW_SHOWMAXIMIZED = 3,
    SW_MAXIMIZE = 3,
    SW_SHOWNOACTIVATE = 4,
    SW_SHOW = 5,
    SW_MINIMIZE = 6,
    SW_SHOWMINNOACTIVE = 7,
    SW_SHOWNA = 8,
    SW_RESTORE = 9,
    SW_SHOWDEFAULT = 10,
    SW_FORCEMINIMIZE = 11,
    SW_MAX = 11

};

//控制嵌入程序的位置和尺寸
private void ResizeControl(IntPtr handle)
{
    SendMessage(handle, WM_COMMAND, WM_PAINT, 0);
    PostMessage(handle, WM_QT_PAINT, 0, 0);
    SetWindowPos(
        handle,
        HWND_TOP,
        0 - 5,//设置偏移量,把原来窗口的菜单遮住(也可以不设置,具体自行修改这里的偏移)
        0 - 41,
        (int)this.Width,
        (int)this.Height + 36,
        //把上面4行替换成下面4行,效果就不一样
        //0,
        // 0,
        //(int)this.Width,
        //(int)this.Height,
        SWP_FRAMECHANGED);
    SendMessage(handle, WM_COMMAND, WM_SIZE, 0);
}

//****************************调用***************************

//被绑架的窗口句柄
int Hwnd = 3608064;
//最大化启动的程序
ShowWindow((IntPtr)Hwnd, (short)Cmd_SHOWWINDOWS.SW_MAXIMIZE);
//设置被绑架程序的父窗口,我这里的父窗口是一个panel控件
SetParent((IntPtr)Hwnd, panel1.Handle);
//改变尺寸
ResizeControl((IntPtr)Hwnd);

2.16 获取顶层窗口

//获得顶层窗口
[DllImport("user32", EntryPoint = "GetForegroundWindow")]
private static extern IntPtr GetForegroundwindow();

/// <summary>
/// 获得顶层窗口
/// </summary>
/// <returns>返回 窗口句柄</returns>
public int GetForeGroundWindow()
{
    return (int)GetForegroundwindow();
}

//****************************调用***************************

int Hwnd = GetForeGroundWindow();

2.17 设置窗口状态

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, SetWindowPosFlags flags);

private const int GWL_STYLE = (-16);
public const int WS_CAPTION = 0xC00000;
private int HWND_TOPMOST = -1;
private int HWND_NOTOPMOST = -2;
private int HWND_BOTTOM = 1;
private int HWND_TOP = 0;

[Flags]
public enum SetWindowPosFlags : uint
{
    // ReSharper disable InconsistentNaming

    /// <summary>
    ///     If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
    /// </summary>
    SWP_ASYNCWINDOWPOS = 0x4000,

    /// <summary>
    ///     Prevents generation of the WM_SYNCPAINT message.
    /// </summary>
    SWP_DEFERERASE = 0x2000,

    /// <summary>
    ///     Draws a frame (defined in the window's class description) around the window.
    /// </summary>
    SWP_DRAWFRAME = 0x0020,

    /// <summary>
    ///     Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
    /// </summary>
    SWP_FRAMECHANGED = 0x0020,

    /// <summary>
    ///     Hides the window.
    /// </summary>
    SWP_HIDEWINDOW = 0x0080,

    /// <summary>
    ///     Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
    /// </summary>
    SWP_NOACTIVATE = 0x0010,

    /// <summary>
    ///     Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
    /// </summary>
    SWP_NOCOPYBITS = 0x0100,

    /// <summary>
    ///     Retains the current position (ignores X and Y parameters).
    /// </summary>
    SWP_NOMOVE = 0x0002,

    /// <summary>
    ///     Does not change the owner window's position in the Z order.
    /// </summary>
    SWP_NOOWNERZORDER = 0x0200,

    /// <summary>
    ///     Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
    /// </summary>
    SWP_NOREDRAW = 0x0008,

    /// <summary>
    ///     Same as the SWP_NOOWNERZORDER flag.
    /// </summary>
    SWP_NOREPOSITION = 0x0200,

    /// <summary>
    ///     Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
    /// </summary>
    SWP_NOSENDCHANGING = 0x0400,

    /// <summary>
    ///     Retains the current size (ignores the cx and cy parameters).
    /// </summary>
    SWP_NOSIZE = 0x0001,

    /// <summary>
    ///     Retains the current Z order (ignores the hWndInsertAfter parameter).
    /// </summary>
    SWP_NOZORDER = 0x0004,

    /// <summary>
    ///     Displays the window.
    /// </summary>
    SWP_SHOWWINDOW = 0x0040,

    // ReSharper restore InconsistentNaming
}

//激活指定窗口
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

//最小化指定窗口
[DllImport("user32")]
private static extern bool CloseWindow(IntPtr hWnd);

//封锁窗口鼠标和键盘消息
[DllImport("user32")]
private static extern bool EnableWindow(IntPtr hWnd, bool bEndble);

//窗口操作函数
[DllImport("user32", EntryPoint = "ShowWindow")]
private static extern bool ShowWindow(IntPtr hWnd, int fLag);

private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;

private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_SHOWMINNOACTIVE = 7;
private const int SW_SHOWNA = 8;
private const int SW_RESTORE = 9;

/// <summary>
/// 设置窗口状态
/// </summary>
/// <param name="Hwnd">窗口句柄</param>
/// <param name="flag">操作方式
/// 1:最小化指定窗口- 不激活
/// 2:封锁窗口鼠标和键盘消息
/// 3: 解除封锁窗口鼠标和键盘消息
/// 4: 激活指定窗口 - 最小化情况下只能激活,不能显示
/// 5:去掉窗口边框 - 同时不可移动
/// 6:恢复窗口边框
/// 7: 将窗口置顶
/// 8:取消窗口置顶
/// 9: 激活指定窗口- 最小化情况能激活也能显示
/// 10: 隐藏指定窗口
/// 11: 显示指定窗口 - 不激活
/// 12: 显示指定窗口 - 并激活
/// 13: 最大化指定窗口 - 激活
/// 14: 最小化指定窗口 - 激活
/// </param>
/// <returns>失败返回 false</returns>
public bool SetWindowState(int Hwnd, int flag)
{
    switch (flag)
    {
    case 1:
        if (!CloseWindow((IntPtr)Hwnd))
        {
            return false;
        }
        break;
    case 2:
        if (!EnableWindow((IntPtr)Hwnd, false))
        {
            return false;
        }
        break;
    case 3:
        if (!EnableWindow((IntPtr)Hwnd, true))
        {
            return false;
        }
        break;
    case 4:
        if (!SetForegroundWindow((IntPtr)Hwnd))
        {
            return false;
        }
        break;
    case 5:
        //(有一些特殊窗口)这两句如果调换顺序执行,效果不一样
        SetWindowLong((IntPtr)Hwnd, GWL_STYLE, GetWindowLong((IntPtr)Hwnd, GWL_STYLE) & ~WS_CAPTION);
        SetWindowPos((IntPtr)Hwnd, HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_FRAMECHANGED);
        break;
    case 6:
        //(有一些特殊窗口)这两句如果调换顺序执行,效果不一样
        SetWindowLong((IntPtr)Hwnd, GWL_STYLE, GetWindowLong((IntPtr)Hwnd, GWL_STYLE) | WS_CAPTION);
        SetWindowPos((IntPtr)Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_FRAMECHANGED);
        break;
    case 7:
        SetWindowLong((IntPtr)Hwnd, GWL_STYLE, GetWindowLong((IntPtr)Hwnd, GWL_STYLE) );
        SetWindowPos((IntPtr)Hwnd, HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_FRAMECHANGED );
        break;
    case 8:
        SetWindowLong((IntPtr)Hwnd, GWL_STYLE, GetWindowLong((IntPtr)Hwnd, GWL_STYLE) );
        SetWindowPos((IntPtr)Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_FRAMECHANGED );
        break;
    case 9:
        if (!ShowWindow((IntPtr)Hwnd, SW_SHOWNORMAL))
        {
            return false;
        }
        break;
    case 10:
        if (!ShowWindow((IntPtr)Hwnd, SW_HIDE))
        {
            return false;
        }
        break;
    case 11:
        if (!ShowWindow((IntPtr)Hwnd, SW_SHOWNA))
        {
            return false;
        }
        break;
    case 12:
        if (!ShowWindow((IntPtr)Hwnd, SW_SHOW))
        {
            return false;
        }
        break;
    case 13:
        if (!ShowWindow((IntPtr)Hwnd, SW_MAXIMIZE))
        {
            return false;
        }
        break;
    case 14:
        if (!ShowWindow((IntPtr)Hwnd, SW_MINIMIZE))
        {
            return false;
        }
        break;
    }
    return true;
}

//****************************调用***************************

//最小化指定窗口
bool ret = SetWindowState(Hwnd, 1);

//封锁窗口键鼠消息
bool ret = SetWindowState(Hwnd, 2);

//解除封锁窗口键鼠消息
bool ret = SetWindowState(Hwnd, 3);

//激活指定窗口
bool ret = SetWindowState(Hwnd, 4);

//去掉窗口边框
bool ret = SetWindowState(Hwnd, 5);

//恢复窗口边框
bool ret = SetWindowState(Hwnd, 6);

//置顶窗口
bool ret = SetWindowState(Hwnd, 7);

//取消置顶窗口
bool ret = SetWindowState(Hwnd, 8);

//激活指定窗口
bool ret = SetWindowState(Hwnd, 9);

//隐藏指定窗口
bool ret = SetWindowState(Hwnd, 10);

//显示指定窗口 - 不激活
bool ret = SetWindowState(Hwnd, 11);

//显示指定窗口 - 激活
bool ret = SetWindowState(Hwnd, 12);

//最大化窗口 - 激活
bool ret = SetWindowState(Hwnd, 13);

//最小化窗口 - 激活
bool ret = SetWindowState(Hwnd, 14);
posted @ 2019-03-15 16:21  SunnyLux  阅读(3089)  评论(2编辑  收藏  举报