C#Formborderstyle=none怎么改变窗体大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#Formborderstyle=none怎么改变窗体大小相关的知识,希望对你有一定的参考价值。

你也是搞软件开发的吗?我是学北大青鸟软件的学生,开学大二了,感觉学的东西太简单了,难的又不会。 C#问题,Formborderstyle=none后,怎么把鼠标放到窗体边缘按住鼠标左键就能拖动改变窗体大小啊? 请给点代码,谢谢!

参考技术A 刚刚测试了一下 左键在窗体上任何一个部位按下后都可以拖动窗体位置,而且还可以拖动边窗改变窗体大小 public partial class Server : Form public Server() InitializeComponent(); Point mouse_offset; private void Server_MouseDown(object sender, MouseEventArgs e) mouse_offset = new Point(-e.X, -e.Y); private void Server_MouseMove(object sender, MouseEventArgs e) if (e.Button == MouseButtons.Left) Point mousePos = Control.MousePosition; mousePos.Offset(mouse_offset.X, mouse_offset.Y); Location = mousePos; 参考技术B 以这样的窗体样式,你只能通过鼠标按下且鼠标移动这两个动作一起,通过代码来改变窗体的大小。本回答被提问者采纳

FormBorderStyle.None 去除 windows 8 原生打开效果

【中文标题】FormBorderStyle.None 去除 windows 8 原生打开效果【英文标题】:FormBorderStyle.None removes the native open effect of windows 8 【发布时间】:2014-07-08 21:21:47 【问题描述】:

我喜欢在 C# 中使我的表单无边框。所以我使用了这段代码:

FormBorderStyle = FormBorderStyle.None;

但它消除了 windows 8 的航空效果。窗体突然打开,就像眨眼一样。

如何恢复气动效果?

【问题讨论】:

Hmya,这取决于你想让自己陷入多少麻烦。已经有a good answer available。处理副作用是你要承担的责任。 那是在 C++ 中,我希望它在我的 C# Windows 窗体中 @MatinLotfaliee 所以?它仍然只是 Windows 消息。只需将其翻译成winforms。你只需要做一些工作才能让它工作:) 我明白了,但是我在想它是否可以用C#翻译,为什么没有翻译后的帖子?也许他们没能做到。 【参考方案1】:

我正在使用the C++ reference 回答我自己的问题 我写了一个继承自Form Class的类。应用程序中的每个表单都应该继承自此类而不是表单类。

 public class AeroForm : Form
 
    int _w = 100, _h = 100;
    bool aero = false;

    [StructLayout(LayoutKind.Sequential)]
    struct MARGINS  public int Left, Right, Top, Bottom;  

    [DllImport("dwmapi.dll", PreserveSig = false)]
    static extern void DwmExtendFrameIntoClientArea
                              (IntPtr hwnd, ref MARGINS margins);

    [DllImport("dwmapi.dll", PreserveSig = false)]
    static extern bool DwmIsCompositionEnabled();

    public AeroForm()
    
            aero = IsCompositionEnabled();
        

        public AeroForm(int width, int height)
            : this()
        
            _w = width;
            _h = height;
            Size = new Size(width, height);
        

        protected override void WndProc(ref Message m)
        
            const int WM_NCCALCSIZE = 0x0083;
            switch (m.Msg)
            
                case WM_NCCALCSIZE:
                    if (aero)
                        return;
                    break;
            
            base.WndProc(ref m);
        

        //this is for checking the OS's functionality.
        //Windows XP does not have dwmapi.dll
        //also, This corrupts the designer... 
        //so i used the Release/Debug configuration
        bool IsCompositionEnabled()
        
    #if !DEBUG
            return File.Exists(Environment.SystemDirectory + "\\dwmapi.dll")
                 && DwmIsCompositionEnabled();
    #else
            return false;
    #endif
        

        //this one is used for a shadow when aero is not available
        protected override CreateParams CreateParams
        
            get
            
                const int CS_DROPSHADOW = 0x00020000;
                const int WS_MINIMIZEBOX = 0x20000;
                const int CS_DBLCLKS = 0x8;
                CreateParams cp = base.CreateParams;
                if (!aero)
                    cp.ClassStyle |= CS_DROPSHADOW;
                cp.Style |= WS_MINIMIZEBOX;
                cp.ClassStyle |= CS_DBLCLKS;
                return cp;
            
        

        //this is for aero shadow and border configurations
        protected override void OnLoad(EventArgs e)
        
            base.OnLoad(e);
            if (aero)
            
                FormBorderStyle = FormBorderStyle.FixedSingle;
                ControlBox = false;
                MinimizeBox = false;
                MaximizeBox = false;
                Size = new Size(_w, _h);
                MARGINS _glassMargins = new MARGINS()
                
                    Top = 5,
                    Left = 5,
                    Bottom = 5,
                    Right = 5
                ;
                DwmExtendFrameIntoClientArea(this.Handle, ref _glassMargins);
            
            else
                FormBorderStyle = FormBorderStyle.None;
        

        //When you minimize and restore, the size will change.
        //this override is for preventing this unwanted resize.
        protected override void OnPaint(PaintEventArgs e)
        
            if (aero)
                Size = new Size(_w, _h);
            base.OnPaint(e);
        
    

使用 Show()Application.Run() 效果很好! 但是当使用 ShowDialog() 打开表单时,它有一些回归。关闭表单,您将看到在您的客户端内容隐藏后关闭的边框。

【讨论】:

@HansPassant:你能帮我解决 ShowDialog() 糟糕的结束效果吗?

以上是关于C#Formborderstyle=none怎么改变窗体大小的主要内容,如果未能解决你的问题,请参考以下文章

C#中form的窗体属性formborderstyle设置为none后就不能移动了

如何去除Winform中窗体的蓝色边框

c# 的窗体form的FormBorderStyle属性设为None 运行后怎么不能拖拽呀??

FormBorderStyle.None 去除 windows 8 原生打开效果

winform form窗体边框颜色怎么设置?

C# winform 窗体怎么隐藏标题栏,不显示标题栏啊