C# Windows API 去掉窗口标题栏 以及边框

写这篇博客的目的,就是防止忘记找不到。

实际项目中使用,测试有效。

调用

IntPtr CurrentAppHandle = GetForegroundWindow();
HandleRef ParentHandle = new HandleRef(this, CurrentAppHandle);

// 初始化窗口风格
IntPtr Style = GetWindowLongPtr(CurrentAppHandle, GWL_STYLE);
uint tempStyle = (uint)Style.ToInt32() & ~WS_CAPTION & ~WS_SYSMENU & ~WS_SIZEBOX;
WinAPI.SetWindowLongPtr(ParentHandle, GWL_STYLE, new IntPtr(tempStyle));

WindowsAPI

    /// <summary>
    /// 检索有关指定窗口的信息。该函数还将以指定的偏移量将值检索到额外的窗口内存中。
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="nIndex"></param>
    /// <returns></returns>
    public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
    {
        if (IntPtr.Size == 8)
            return GetWindowLongPtr64(hWnd, nIndex);
        else
            return GetWindowLongPtr32(hWnd, nIndex);
    }
    [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
    private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
    private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);

    /// <summary>
    /// 这种静态方法是必需的,因为早期操作系统不支持
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="nIndex"></param>
    /// <param name="dwNewLong"></param>
    /// <returns></returns>
    public static IntPtr SetWindowLongPtr(HandleRef hWnd, int nIndex, IntPtr dwNewLong)
    {
        if (IntPtr.Size == 8)
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        else
            return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
    }

    [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
    private static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
    private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong);

    /// <summary>
    /// 获取置顶窗口
    /// </summary>
    /// <returns></returns>

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

 

 

参数

    /// <summary>
    /// 设置一个新的窗口样式
    /// </summary>
    public const int GWL_STYLE = -16;

    /// <summary>
    /// 标题
    /// </summary>
    public const uint WS_CAPTION = 0x00C00000;     /* WS_BORDER | WS_DLGFRAME  */

    /// <summary>
    /// 系统菜单
    /// </summary>
    public const uint WS_SYSMENU = 0x00080000;


    /// <summary>
    /// 设置调整窗口大小的厚的结构的窗口。
    /// </summary>
    public const uint WS_THICKFRAME = 0x00040000;

    /// <summary>
    /// 创建一个可调边框的窗口,与 WS_THICKFRAME 风格相同
    /// </summary>
    public const uint WS_SIZEBOX = WS_THICKFRAME;

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

化虚为实

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

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

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

打赏作者

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

抵扣说明:

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

余额充值