0

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

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 并尝试拖出表单。注意我故意超载重绘机制来产生黑色区域。

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

4

1 回答 1

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

PS 200 按钮对于任何 GUI 来说都太多了 :)

恕我直言

已编辑:在这里,我发布了我设法修复 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 };
于 2010-10-16T18:35:07.600 回答