C# SetWindowPos函数

本文详细解释了C#中SetWindowPos函数的用法,包括其原型、参数含义以及如何通过实例操作窗口的位置、大小、Z顺序显示和隐藏。介绍了使用DllImport调用user32.dll库的常见场景和标志位组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在C#中,SetWindowPos函数用于设置窗口的位置和大小。

原型:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

参数说明:

  • hWnd: 要设置位置和大小的窗口的句柄(handle)。

  • hWndInsertAfter: 确定窗口的 Z 顺序,即窗口如何在 Z 顺序中放置。通常可以使用以下值之一:

    • IntPtr.Zero: 将窗口放在 Z 顺序的顶部。
    • new IntPtr(-1): 将窗口放在所有非顶层窗口的顶部。
    • new IntPtr(-2): 将窗口放在所有窗口的顶部。
    • 其他窗口句柄:将窗口放在指定窗口的顶部。
  • XY: 窗口的新位置的左上角的 X 和 Y 坐标。

  • cxcy: 窗口的新宽度和高度。

  • uFlags: 设置窗口位置和大小的标志。可以使用以下标志的组合来控制窗口的行为:

    • SWP_NOSIZE: 维持当前尺寸(忽略 cx 和 cy 参数)。

    • SWP_NOMOVE: 维持当前位置(忽略 X 和 Y 参数)。

    • SWP_NOZORDER: 维持当前 Z 顺序(忽略 hWndInsertAfter 参数)。

    • SWP_SHOWWINDOW: 如果窗口在调用时是隐藏的,显示窗口。

    • SWP_HIDEWINDOW: 隐藏窗口。

    • SWP_NOACTIVATE: 不激活窗口。仅适用于SWP_SHOWWINDOW标志。

    • SWP_DRAWFRAME: 在调整窗口大小时,重绘窗口边框(通常与SWP_FRAMECHANGED一起使用)。

    • SWP_NOOWNERZORDER: 不改变拥有者窗口的 Z 顺序。

    • SWP_NOSENDCHANGING: 防止窗口接收WM_WINDOWPOSCHANGING消息。

    • SWP_FRAMECHANGED: 使系统发送WM_NCCALCSIZE消息,即使窗口的尺寸没有改变。

    • SWP_NOCOPYBITS: 防止窗口重绘。

    • SWP_NOREPOSITION: 不重新定位窗口,即使大小或位置发生变化。

示例用法:

1、改变窗口大小:

using System;
using System.Runtime.InteropServices;

// 定义常量和方法签名
public class Win32
{
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    public const uint SWP_SHOWWINDOW = 0x40;
}

// 获取窗口句柄并调用 SetWindowPos 函数改变窗口大小
IntPtr hWnd = // 窗口句柄
int newX = // 新的 X 坐标
int newY = // 新的 Y 坐标
int newWidth = // 新的宽度
int newHeight = // 新的高度

Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, newX, newY, newWidth, newHeight, Win32.SWP_SHOWWINDOW);

其中窗口句柄可以使用FindWindow函数获取窗口句柄(后面同),如:

[DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr hWnd = Win32.FindWindow(null, "窗口标题"); // 替换为窗口的类名或标题

2、移动到屏幕的左上角:

using System;
using System.Runtime.InteropServices;

// 定义常量和方法签名
public class Win32
{
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOMOVE = 0x0002;
    public const int SWP_NOZORDER = 0x0004;
    public const int SWP_SHOWWINDOW = 0x0040;

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}

// 在代码中调用 SetWindowPos 函数将窗口移动到屏幕左上角
IntPtr hWnd = // 窗口句柄
Win32.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);

3、使其成为Topmost窗口并移动到屏幕的左上角:

using System;
using System.Runtime.InteropServices;

// 定义常量和方法签名
public class Win32
{
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOMOVE = 0x0002;
    public const int SWP_NOZORDER = 0x0004;
    public const int SWP_SHOWWINDOW = 0x0040;
    public const int HWND_TOPMOST = -1;
    public const int HWND_NOTOPMOST = -2;

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}

// 在代码中调用 SetWindowPos 函数将窗口移动到屏幕左上角并设置为Topmost
IntPtr hWnd = // 窗口句柄
Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);

4、显示窗口:

using System;
using System.Runtime.InteropServices;

// 定义常量和方法签名
public class Win32
{
    public const int SW_SHOWNORMAL = 1;

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

// 获取窗口句柄并调用 ShowWindow 函数显示窗口
IntPtr hWnd = // 窗口句柄
Win32.ShowWindow(hWnd, Win32.SW_SHOWNORMAL);

5、隐藏窗口:

using System;
using System.Runtime.InteropServices;

// 定义常量和方法签名
public class Win32
{
    public const int SW_HIDE = 0;

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

// 获取窗口句柄并调用 ShowWindow 函数隐藏窗口
IntPtr hWnd = // 窗口句柄
Win32.ShowWindow(hWnd, Win32.SW_HIDE);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿游也

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值