如何将无边框表单对齐到屏幕边缘? [英] How do I snap a borderless Form to the edges of a screen?

查看:25
本文介绍了如何将无边框表单对齐到屏幕边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将表单的边框样式设置为无时,我无法再将窗口对齐到屏幕的边缘.我怎么能两者兼得?

When I set the form's border style to none, I can no longer snap the window to the edges of the screen. How can I have both?

推荐答案

Aero Snap 需要一个有边框的窗口,没有后门.您可以使用这篇文章中的代码模拟捕捉.

Aero Snap requires a window with a border, no back-door. You could emulate snapping with the code in this post.

这与用户从 Aero Snap 获得的交互式反馈相去甚远.获得无边框窗口的方法不止一种,另一种方法是拦截 WM_NCCALCSIZE 消息.Windows 发送的一条消息,让应用有机会覆盖窗口的客户区大小.这很容易做到,将 FormBorderStyle 属性设置回 Sizable 并将此代码粘贴到您的 Form 类中:

That's still a far cry from the interactive feedback the user gets from Aero Snap. There is more than one way to get a borderless window, another way is by intercepting the WM_NCCALCSIZE message. A message that Windows sends to give an app the opportunity to override the client area size of a window. That's very easy to do, set the FormBorderStyle property back to Sizable and paste this code into your Form class:

    protected override void WndProc(ref Message m) {
        const int WM_NCCALCSIZE = 0x83;
        if (m.Msg == WM_NCCALCSIZE && m.WParam.ToInt32() == 1) {
            m.Result = new IntPtr(0xF0);   // Align client area to all borders
            return;
        }
        base.WndProc(ref m);
    }

请注意,您可能已经重写了此方法以使窗口变大.只需使用此代码更新即可.

Beware that you probably already have overridden this method to make the window sizable. Just update it with this code.

每次这样的 hack 都会产生另一个问题,你的窗口的客户区现在会太大.边框和窗口标题的大小更大.解决这个问题很棘手,Aero 在于边框大小,您必须确保 DPI 的自动缩放仍然正常工作.将 FormBorderStyle 设置回 None 并使 Form 的构造函数看起来像这样:

Every hack like this produces yet another problem, the client area of your window will now be too large. Larger by the size of the borders and the window caption. Fixing this is tricky, Aero lies about border sizes and you have to ensure that auto-scaling for DPI still works correctly. Set FormBorderStyle back to None and make the constructor of the Form look like this:

    public Form1() {
        InitializeComponent();
        var designSize = this.ClientSize;
        this.FormBorderStyle = FormBorderStyle.Sizable;
        this.Size = designSize;
    }

请记住,即使窗口现在打开了边框样式标志,您仍然不会看到阴影.很难修复,CS_DROPSHADOW 已经够用了.

Keep in mind that even though the window now has the border style flag turned on, you still won't get a drop-shadow. Hard to fix, CS_DROPSHADOW is as good as it gets.

这篇关于如何将无边框表单对齐到屏幕边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆