使用Visual Studio 2010继承Windows窗体类

本文关键字:Windows 窗体 继承 2010 Visual Studio 使用 | 更新日期: 2023-09-27 17:57:45

我正在使用Visual Studio 2010创建一个应用程序。我有一个从System.Windows.Forms.Form控件派生的类。这个名为CViewport的类是我的应用程序中所有表单的标准(即,我打算从这个类派生其他表单)。

从CViewport派生出一个类后,我去设计器编辑第二代表单,但在设计器中表单没有正确显示。如果我没有弄错的话,它看起来是透明的。。。上一个窗口在表单的客户端区域中闪闪发光,就好像设计器没有绘制后台缓冲区一样。。。

此外,在更改第二代表单的大小后,设计者将大小重置为任意尺寸。。。

为什么会发生这种情况?这是Form派生类:

namespace MyApp
{
    public partial class CViewport : Form
    {
        public CViewport()
        {
            InitializeComponent();
        }
    }
}

这是CViewport.Designer.cs:

namespace MyApp
{
    partial class CViewport
    {
        /// <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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GDEViewport));
            this.SuspendLayout();
            // 
            // CViewport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 600);
            this.ControlBox = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "GDEViewport";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Activated += new System.EventHandler(this.ViewportForm_Activated);
            this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnFormClosing);
            this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
            this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
            this.Resize += new System.EventHandler(this.MainViewport_UserResized);
            this.ResumeLayout(false);
        }
        #endregion
    }
}

这是从CViewport 派生的第二代表单

namespace MyApp
{
    public partial class CMainViewport: CViewport
    {
        public CMainViewport()
        {
            InitializeComponent();
        }
    }
}

这是CMainViewport.Designer.cs:

namespace MyApp
{
    partial class CMainViewport
    {
        /// <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.SuspendLayout();
            // 
            // CMainViewport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1218, 573);
            this.ControlBox = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.MaximizeBox = true;
            this.MinimizeBox = true;
            this.MinimumSize = new System.Drawing.Size(800, 600);
            this.Name = "TestForm";
            this.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.ShowIcon = true;
            this.ShowInTaskbar = true;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "TestForm";
            this.PauseRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_PauseRendering);
            this.ResumeRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_ResumeRendering);
            this.SystemResume += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemResume);
            this.SystemSuspend += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemSuspend);
            this.UserResized += new System.EventHandler<System.EventArgs>(this.MainViewport_UserResized);
            this.Activated += new System.EventHandler(this.ViewportForm_Activated);
            this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
            this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
            this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
            this.Resize += new System.EventHandler(this.ViewportForm_Resize);
            this.ResumeLayout(false);
        }
        #endregion
    }
}

使用Visual Studio 2010继承Windows窗体类

  this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
  this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
  this.Resize += new System.EventHandler(this.MainViewport_UserResized);

设计器在基类中运行代码以提供所见即所得视图。这通常是Form类,它没有任何错误。现在它是您的CViewPort类。它有虫子。在一个我们看不到的事件处理程序中。

与Paint事件处理程序一样,强引导到"未正确显示"。您有一个用于Resize的处理程序,强引导"将大小重置为任意尺寸"。让这些事件也在设计时运行会让你陷入麻烦。如果你做得不对,Form的基类应该覆盖OnPaint()和OnResize(),这样代码运行的顺序是可预测和可控的。

在设计时运行代码需要对Winforms的工作方式有更深入的了解,从书中获得指导非常重要。你还没有到那里,你没有意识到发布这些事件处理程序的代码甚至是相关的。有一种避免麻烦的方法可以让你有时间阅读这本书,将这行代码添加到你添加到CViewPort类的每个事件处理程序中:

  if (this.DesignMode) return;

Hans Passant发布的答案非常有用。。。但问题是,我在基窗体的构造函数中将基类的DockStyle属性设置为"Fill"。删除这行代码解决了问题。。。