绘制或隐藏控制框以实现无边界表单,同时最大化和最小化 [英] Paint or Hide Control Box for Borderless Form while maximize and minimize

查看:137
本文介绍了绘制或隐藏控制框以实现无边界表单,同时最大化和最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已附上一个有关此问题的小例子.如何在最大化和最小化无边界表单期间完全隐藏控制框

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.

推荐答案

我可以确认问题.从最小化状态还原无边界Form时,很短的时间在窗口的左上方显示标题栏的重影.

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.

再现问题

要重现此问题,只需将FormBorderStyle属性设置为None,然后在计时器中最小化和还原它,即可创建无边界表单.还原后,显示表单启动程序,然后在窗口的左上角查看.

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();
    }
}

解决方法

这是我用来消除闪烁的解决方法.将事件处理程序添加到上述Form1类并为Activated事件this.Activated += Form1_Activated;注册它就足够了.

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();
    }));
}

这篇关于绘制或隐藏控制框以实现无边界表单,同时最大化和最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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