扩展形式时会出现黑边?

时间:2010-10-15 19:55:16

标签: .net winforms form-control

创建一个.net 2.0 Windows窗体应用程序,在其窗体构造函数中添加一个splitContainer停靠的Fill:

public Form1()
{
    InitializeComponent();

    for (int i = 1; i <= 300; i++)
    {
        FlowLayoutPanel f = new FlowLayoutPanel();
        f.Dock = DockStyle.Fill;
        Button b = new Button();
        f.Controls.Add(b);
        splitContainer1.Panel2.Controls.Add(f);
    }
}

按F5。通过右下边缘抓住表格并快速拖动以展开表格。由于在所有控件中完成了工作,表单相当急剧。

我有一个带有表单的应用程序,其中有一些缓慢的控件同样变慢。不同的是,在我的应用程序中,当我用鼠标拖出表单时,我会在间隙中出现难看的黑色空间。窗口没有正确重新绘制。这个丑陋的黑色空间不会出现在上面的示例代码中。

任何想法在扩展缓慢形式时可能会导致黑色空间出现?

我尝试过双缓冲,但没有区别。

编辑:我已将表单拆解为基础,以便可以重现。启动一个名为WindowsFormsApplication_SampleFault的新C#Windows窗体应用程序。将下面的代码粘贴到Form1.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;
using System.Runtime.InteropServices;


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

            for (int i = 1; i <= 200; i++)
            {
                Button b = new Button();
                b.Dock = DockStyle.Fill;
                this.Controls.Add(b);
            } 
        }
        }
}

然后将其粘贴到Form1.Designer.cs

namespace WindowsFormsApplication_SampleFault
{
    partial class Form1
    {
            /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        /// 

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.SuspendLayout();
            // 
            // statusStrip1
            // 
            this.statusStrip1.Location = new System.Drawing.Point(0, 472);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(756, 22);
            this.statusStrip1.TabIndex = 6;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // frmOptions
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(756, 494);
            this.Controls.Add(this.statusStrip1);
            this.DoubleBuffered = true;
            this.ImeMode = System.Windows.Forms.ImeMode.Off;
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(750, 500);
            this.Name = "frmOptions";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.StatusStrip statusStrip1;

    }
}

按F5并尝试拖出表单。注意我故意重载重绘机制以产生黑色区域。

我认为表单设计器代码有问题,但我不确定是什么。

1 个答案:

答案 0 :(得分:0)

这种特殊情况可以通过WS_EX_COMPOSITED轻松修复。

const int WS_EX_COMPOSITED = 0x02000000;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= WS_EX_COMPOSITED;
        return cp;
    }
}

P.S。任何GUI都有200个按钮太多了:)

IMHO

编辑:在这里,我发布了我在Win7上修复黑盒子的工作。代码有点难看,但我希望这个想法很明确。

public Form1()
{
    InitializeComponent();

    for (int i = 1; i <= 200; i++)
    {
        Button b = new Button();
        b.Dock = DockStyle.Fill;
        this.Controls.Add(b);
    }
    _layoutWorker.Tick += new EventHandler(LayoutWorker_Tick);

}

private void LayoutWorker_Tick(object sender, EventArgs e)
{
    _layoutWorker.Stop();
    this.PerformLayout();
}

protected override void OnResize(EventArgs e)
{
    this.SuspendLayout();
    base.OnResize(e);
    this.ResumeLayout(false);
    _layoutWorker.Start();
}

private Timer _layoutWorker = new Timer { Enabled = false, Interval = 1 };