C#窗口控制

这篇文章展示了如何使用C#来调用WindowsAPI函数,包括FindWindow,ShowWindow,SetForegroundWindow和GetWindowRect,这些函数分别用于查找窗口,控制窗口的显示状态,设置窗口到前景以及获取窗口的尺寸。这些功能对于系统级交互和用户界面管理至关重要。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WQ
{
    /// <summary>
    /// 窗口
    /// </summary>
    public class Window
    {
        // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa

        /// <summary>
        /// 获取一个顶层窗口的句柄,该顶层窗口的类名和标题与给定的字符串相匹配。此函数不搜索子窗口。
        /// </summary>
        /// <param name="lpClassName">窗口类名。如果为null,它会查找标题与lpWindowName参数匹配的任何窗口。</param>
        /// <param name="lpWindowName">窗口标题。如果为null,则所有窗口标题都匹配。</param>
        /// <returns>如果函数成功,返回值为该顶层窗口的句柄。如果函数失败,返回值为0x00000000。</returns>
        [DllImport("user32.dll")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);



        //  https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-showwindow?redirectedfrom=MSDN

        /// <summary>
        /// 激活并显示窗口。如果窗口最小化或最大化,系统会将其还原到其原始大小和位置。
        /// </summary>
        public const byte SW_SHOWNORMAL = 1;

        /// <summary>
        /// 激活窗口并将其最小化。
        /// </summary>
        public const byte SW_SHOWMINIMIZED = 2;

        /// <summary>
        /// 激活窗口并将其最大化。
        /// </summary>
        public const byte SW_SHOWMAXIMIZED = 3;

        /// <summary>
        /// 此值类似于SW_SHOWNORMAL,但窗口未激活。
        /// </summary>
        public const byte SW_SHOWNOACTIVATE = 4;

        /// <summary>
        /// 设置窗口的显示方式。
        /// </summary>
        /// <param name="hWnd">窗口的句柄</param>
        /// <param name="nCmdShow">窗口的显示方式</param>
        /// <returns>如果窗口以前可见,则返回值为非零。如果窗口之前已隐藏,则返回值为零。</returns>
        [DllImport("user32.dll")]
        public extern static int ShowWindow(IntPtr hWnd, int nCmdShow);



        //  https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setforegroundwindow?redirectedfrom=MSDN

        /// <summary>
        /// 将窗口设置到前台并激活。
        /// </summary>
        /// <param name="hWnd">窗口的句柄</param>
        /// <returns>如果窗口被带到前台,则返回值为非零。如果未将窗口带到前台,则返回值为零。</returns>
        [DllImport("user32.dll")]
        public extern static int SetForegroundWindow(IntPtr hWnd);



        //  https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect

        /// <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="lpRect">窗口的尺寸</param>
        /// <returns>如果函数成功,则返回值非零。如果函数失败,则返回值为零。</returns>
        [DllImport("user32.dll")]
        public extern static int GetWindowRect(IntPtr hWnd, ref RECT lpRect);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值