Paint or Hide Control Box for Borderless Form while maximize and minimize

佐手、 提交于 2019-12-02 04:42:59

问题


I've attached a small example about the issue. How can I hide the control box completely during Maximize and Minimize the Borderless Form

using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;

namespace TalkTime
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private const int WM_NCPAINT = 0x0085;
    protected override void WndProc(ref Message m)
    {

        int message = m.Msg;
        switch (m.Msg)
        {
            case WM_NCPAINT:
                {

                    Thread.Sleep(100);

                    return;
                }
        }
        base.WndProc(ref m);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style |= 0x20000;
            return cp;
        }
    }
}
}

I put the thread to show where is the problem exactly.

The black rectangle which is I guess related to the controlbox and form name will appear before the form while I want to hide it completely while maximizing and minimizing.


回答1:


I can confirm the issue. When restoring a border-less Form from minimized state, a ghost of a title-bar shows at top-left of the window for a very short time.

Reproducing the issue

To reproduce the problem, it's enough to create a border-less form by setting FormBorderStyle property to None and then minimize and restore it in a timer. Start the program by showing the form and look at top-left of the window, after restore.

using System;
using System.Windows.Forms;
class Form1 : Form
{
    public Form1()
    {
        var timer = new Timer() { Interval = 1000 };
        this.Text = "Some Text";
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        timer.Tick += (x, y) =>
        {
            if (this.WindowState != FormWindowState.Minimized)
                this.WindowState = FormWindowState.Minimized;
            else
                this.WindowState = FormWindowState.Normal;
        };
        timer.Start();
    }
}

Workaround

Here is the workaround which I used to remove that flicker. It's enough to add the event handler to above Form1 class and register it for Activated event this.Activated += Form1_Activated;.

private void Form1_Activated(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
        this.Hide();
    this.BeginInvoke(new Action(() =>
    {
        if (this.WindowState != FormWindowState.Minimized && !Visible)
            this.Show();
    }));
}


来源:https://stackoverflow.com/questions/38881787/paint-or-hide-control-box-for-borderless-form-while-maximize-and-minimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!