使用 Visual Studio 2010 继承 Windows 窗体类

Posted

技术标签:

【中文标题】使用 Visual Studio 2010 继承 Windows 窗体类【英文标题】:Inheriting Windows Form Classes with Visual Studio 2010 【发布时间】:2014-05-24 19:37:36 【问题描述】:

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

从 CViewport 派生一个类后,我去设计器编辑第二代表单,在设计器中表单显示不正确。如果我没记错的话,它看起来是透明的……前一个窗口通过窗体的客户区闪耀,就好像设计者没有绘制后台缓冲区一样……

...另外,在更改 2nd gen 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
    

【问题讨论】:

基本表单中的代码将在设计时运行。您根本没有给出任何提示,闻起来像 OnPaintBackground 是 borken。使用 DesignMode 属性阻止代码运行。 @HansPassant 我不会覆盖 OnPaintBackground() 我会尝试...这是否也能解释为什么在设计器 UI 和设计器代码中明确设置窗口大小后会自动重置? 不要乱写代码。把时间花在提供更好的再现上。 Read this @HansPassant...我以为您是在说“基本代码”时要求设计器代码...所以 CViewport 类处理背景的绘制? CViewport 在设计器中正确显示...它从 CViewport 派生的类不... 【参考方案1】:
  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事件处理程序一样,强导致“显示不正确”。您有一个调整大小的处理程序,强烈导致“将大小重置为任意尺寸”。让这些事件在设计时也运行是让您陷入困境的原因。而且你做错了,Form 的基类应该覆盖 OnPaint() 和 OnResize() 以便代码运行的顺序是可预测和可控的。

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

  if (this.DesignMode) return;

【讨论】:

【参考方案2】:

Hans Passant 发布的答案非常有帮助……但问题是我在基表单的构造函数中将基类的 DockStyle 属性设置为“填充”。删除这行代码解决了问题...

【讨论】:

以上是关于使用 Visual Studio 2010 继承 Windows 窗体类的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual Studio 2010 中,如何从父项目继承预处理器定义?

在visual studio 2010中为新的QOBJECT文件生成MOC

在 Visual Studio 2010 中为新 QOBJECT 文件生成 MOC

Visual Studio 2010 未生成 SharePoint 2010 WebPart .g.cs 文件

Visual Studio - 继承控件的新“默认”属性值

从设计器中隐藏 WPF 控件的属性 (Visual Studio 2010)