[英]Inheriting Windows Form Classes with Visual Studio 2010
我正在使用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
}
}
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);
设计器在基类中运行代码以提供WYSIWYG视图。 那通常是Form类,没有任何错误。 现在是您的CViewPort类。 它有错误。 在其中一种我们看不到的事件处理程序中。
类似于Paint事件处理程序,强烈导致“未正确显示”。 您有一个“调整大小”处理程序,可以很强地导致“将大小重置为任意尺寸”。 在设计时运行这些事件也会使您陷入麻烦。 而且您做错了,Form的基类应该重写OnPaint()和OnResize(),因此代码的运行顺序是可预测和可控制的。
在设计时运行代码需要对Winforms的工作方式有更深入的了解,因此从书中获得指导非常重要。 您还没有到那儿,您还没有意识到发布这些事件处理程序的代码甚至相关。 有一种避免麻烦的方法,可以让您有时间阅读本书,将以下代码行添加到添加到CViewPort类的每个事件处理程序中:
if (this.DesignMode) return;
汉斯·帕桑特(Hans Passant)发布的答案非常有帮助...不过,问题是,我在基本表单的构造函数中将基类的DockStyle属性设置为“填充”。 删除此行代码可解决问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.