【发布时间】: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 派生的类不...