简体   繁体   English

使用Visual Studio 2010继承Windows窗体类

[英]Inheriting Windows Form Classes with Visual Studio 2010

I'm using Visual Studio 2010 to create an application. 我正在使用Visual Studio 2010创建应用程序。 I have a class derived from the System.Windows.Forms.Form control. 我有一个派生自System.Windows.Forms.Form控件的类。 This class, called CViewport, is a standard for all forms in my application( ie. I intend to derive other forms from this class). 此类称为CViewport,是我的应用程序中所有形式的标准(即,我打算从此类中派生其他形式)。

After deriving a class from CViewport, I go to the designer to edit the 2nd generation form and in the designer the form is not displayed correctly. 从CViewport派生一个类之后,我去设计器编辑第二代表单,并且在设计器中该表单无法正确显示。 It appears transparent if I'm not mistaken...the previous window shines through the client area of the form as if the backbuffer has not been drawn to by the designer... 如果我没记错的话,它看起来是透明的...上一个窗口在表单的客户区域闪闪发光,就好像设计师没有绘制后缓冲...

...Also, after changing the size of the 2nd gen form, the designer resets the size to arbitrary dimensions... ...此外,在更改了第二代表格的大小后,设计人员将大小重置为任意尺寸...

Why might this be happening? 为什么会这样呢? Here is Form derived class: 这是Form派生类:

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

        }
    }
}

here is CViewport.Designer.cs: 这是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
    }
}

here is 2nd gen form derived from CViewport 这是从CViewport衍生的第二代表格

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

here is CMainViewport.Designer.cs: 这是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
    }
}
  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);

The designer runs the code in the base class to provide the WYSIWYG view. 设计器在基类中运行代码以提供WYSIWYG视图。 That's normally the Form class, it doesn't have any bugs. 那通常是Form类,没有任何错误。 Now it is your CViewPort class. 现在是您的CViewPort类。 It has bugs. 它有错误。 In one those event handlers that we cannot see. 在其中一种我们看不到的事件处理程序中。

Like the Paint event handler, strong lead to "is not displayed correctly". 类似于Paint事件处理程序,强烈导致“未正确显示”。 You have a handler for Resize, strong lead to "resets the size to arbitrary dimensions". 您有一个“调整大小”处理程序,可以很强地导致“将大小重置为任意尺寸”。 Having these events also run at design time is what is getting you into trouble. 在设计时运行这些事件也会使您陷入麻烦。 And you are doing it wrong, a base class for a Form should override OnPaint() and OnResize() so the order in which code runs is predictable and controllable. 而且您做错了,Form的基类应该重写OnPaint()和OnResize(),因此代码的运行顺序是可预测和可控制的。

Having code run at design-time requires deeper insight in the way Winforms works, getting guidance from a book is rather important. 在设计时运行代码需要对Winforms的工作方式有更深入的了解,因此从书中获得指导非常重要。 You are not there yet, you didn't realize that posting the code for these event handlers was even relevant. 您还没有到那儿,您还没有意识到发布这些事件处理程序的代码甚至相关。 There's a keep-out-of-trouble approach that gives you time to read the book, add this line of code to every event handler you added to your CViewPort class: 有一种避免麻烦的方法,可以让您有时间阅读本书,将以下代码行添加到添加到CViewPort类的每个事件处理程序中:

  if (this.DesignMode) return;

The answer posted by Hans Passant was very helpful...the problem though, was that I was setting the DockStyle property of the base class to 'Fill' in the base form's constructor. 汉斯·帕桑特(Hans Passant)发布的答案非常有帮助...不过,问题是,我在基本表单的构造函数中将基类的DockStyle属性设置为“填充”。 Removing this line of code fixed the problem... 删除此行代码可解决问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用visual studio 2010在Windows窗体中创建线条? - Create lines in windows form with visual studio 2010? 如何将XtraReport连接到Visual Studio 2010上的Windows窗体 - How do I connect XtraReport to windows form on Visual Studio 2010 在Visual Studio 2010中识别Windows应用程序窗体的helpcontextid - identify helpcontextid in visual studio 2010 for windows application form Windows Form Application C锐视觉工作室2010 - Windows Form Application C sharp visual studio 2010 配置系统无法在Visual Studio 2010 Windows窗体中初始化 - configuration system failed to initialize in visual studio 2010 windows form Visual Studio 2010 - 发布Windows窗体或WPF应用程序 - Visual Studio 2010 - Publishing a Windows Form or WPF application 在Visual Studio中的子类中继承和运行单元测试 - Inheriting and running Unit Tests in sub classes in Visual studio Visual Studio设计师可以显示继承通用类型的类吗? - Can visual studio designer show classes inheriting generic types? 在Visual Studio 2010 Express中部署Windows应用程序 - Deploying windows application in Visual studio 2010 Express 带有Visual Studio 2005解决方案的Windows服务未随Visual Studio 2010打开 - Windows Service with Visual Studio 2005 solution not opening with Visual Studio 2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM