C# SetWindowPos函数

在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);

相关推荐

  1. 损失函数(目标函数

    2024-03-21 06:56:02       65 阅读
  2. 字符函数字符串函数

    2024-03-21 06:56:02       53 阅读
  3. Python函数——函数介绍

    2024-03-21 06:56:02       55 阅读
  4. 匿名函数函数

    2024-03-21 06:56:02       32 阅读
  5. linux | pause函数 、alarm函数、signal函数

    2024-03-21 06:56:02       50 阅读
  6. MySQL 条件函数/加密函数/转换函数

    2024-03-21 06:56:02       27 阅读
  7. split函数

    2024-03-21 06:56:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 06:56:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 06:56:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 06:56:02       82 阅读
  4. Python语言-面向对象

    2024-03-21 06:56:02       91 阅读

热门阅读

  1. 代码随想录Day29

    2024-03-21 06:56:02       41 阅读
  2. Vue3 v-model的使用

    2024-03-21 06:56:02       36 阅读
  3. 谈谈对跨站请求伪造(CSRF)的理解及其防御措施

    2024-03-21 06:56:02       46 阅读
  4. Linux查看8080端口是否启用

    2024-03-21 06:56:02       41 阅读
  5. 【Docker】Docker的常用命令知多少?

    2024-03-21 06:56:02       45 阅读
  6. @click 和 v-on:click 的区别

    2024-03-21 06:56:02       38 阅读
  7. 【积累】string和list

    2024-03-21 06:56:02       40 阅读
  8. 【积累】list

    2024-03-21 06:56:02       47 阅读
  9. 每日一题 第十六期 Codeforces Round 911 (Div. 2)

    2024-03-21 06:56:02       41 阅读
  10. dataGridView 绑定List 显示内容不刷新

    2024-03-21 06:56:02       44 阅读