我目前正在开发gui
内容在特定xml
文件中定义。我叫做Widgets
。这些小部件具有自己实现的逻辑,并具有定义的宽度和高度。 minimum
和maximum size
都是固定的,无法更改。小部件是UserControl
。
显示修复最大值的屏幕截图。和分钟。尺寸:
对于现在未知的原因,大小在运行时期间是不同的。如下面的屏幕截图所示:
SingeplayerWidget的SingleplayerWidget
我自己的源代码的设计器文件旁边是:
using System;
using System.Drawing;
using System.Windows.Forms;
using Racetrack.Controller;
namespace Racetrack.View.Widgets
{
public partial class SingleplayerWidget : UserControl
{
private GameController Controller;
public SingleplayerWidget()
{
InitializeComponent();
this.extendedTextBoxPlayername.SetPlaceholder("Choose a playername");
}
public SingleplayerWidget(GameController controller)
{
InitializeComponent();
this.Controller = controller;
this.extendedTextBoxPlayername.SetPlaceholder("Choose a playername");
this.colorDialogCarColor = new ColorDialog();
this.colorDialogCarColor.Color = Color.Black;
this.panelCarColor.BackColor = this.colorDialogCarColor.Color;
}
private void menuButtonChangeCarColor_Click(object sender, EventArgs e)
{
this.colorDialogCarColor.ShowDialog();
this.panelCarColor.BackColor = this.colorDialogCarColor.Color;
}
}
}
这是设计师的源代码:
namespace Racetrack.View.Widgets
{
partial class SingleplayerWidget
{
/// <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 Component 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.labelWidgetHeadline = new System.Windows.Forms.Label();
this.labelPlayername = new System.Windows.Forms.Label();
this.colorDialogCarColor = new System.Windows.Forms.ColorDialog();
this.labelCarColor = new System.Windows.Forms.Label();
this.panelCarColor = new System.Windows.Forms.Panel();
this.menuButtonChangeCarColor = new Racetrack.View.Forms.MenuButton();
this.extendedTextBoxPlayername = new Racetrack.View.Forms.ExtendedTextBox();
this.SuspendLayout();
//
// labelWidgetHeadline
//
this.labelWidgetHeadline.Anchor = System.Windows.Forms.AnchorStyles.None;
this.labelWidgetHeadline.AutoSize = true;
this.labelWidgetHeadline.Font = new System.Drawing.Font("Microsoft Sans Serif", 13.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelWidgetHeadline.Location = new System.Drawing.Point(14, 8);
this.labelWidgetHeadline.Name = "labelWidgetHeadline";
this.labelWidgetHeadline.Size = new System.Drawing.Size(188, 29);
this.labelWidgetHeadline.TabIndex = 2;
this.labelWidgetHeadline.Text = "Your Character";
//
// labelPlayername
//
this.labelPlayername.Anchor = System.Windows.Forms.AnchorStyles.None;
this.labelPlayername.AutoSize = true;
this.labelPlayername.Location = new System.Drawing.Point(17, 58);
this.labelPlayername.Name = "labelPlayername";
this.labelPlayername.Size = new System.Drawing.Size(87, 17);
this.labelPlayername.TabIndex = 3;
this.labelPlayername.Text = "Playername:";
//
// labelCarColor
//
this.labelCarColor.Anchor = System.Windows.Forms.AnchorStyles.None;
this.labelCarColor.AutoSize = true;
this.labelCarColor.Location = new System.Drawing.Point(17, 120);
this.labelCarColor.Name = "labelCarColor";
this.labelCarColor.Size = new System.Drawing.Size(69, 17);
this.labelCarColor.TabIndex = 5;
this.labelCarColor.Text = "Car color:";
//
// panelCarColor
//
this.panelCarColor.Anchor = System.Windows.Forms.AnchorStyles.None;
this.panelCarColor.Location = new System.Drawing.Point(20, 140);
this.panelCarColor.Name = "panelCarColor";
this.panelCarColor.Size = new System.Drawing.Size(80, 37);
this.panelCarColor.TabIndex = 8;
//
// menuButtonChangeCarColor
//
this.menuButtonChangeCarColor.Anchor = System.Windows.Forms.AnchorStyles.None;
this.menuButtonChangeCarColor.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.menuButtonChangeCarColor.FlatAppearance.BorderSize = 0;
this.menuButtonChangeCarColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.menuButtonChangeCarColor.Location = new System.Drawing.Point(119, 140);
this.menuButtonChangeCarColor.Name = "menuButtonChangeCarColor";
this.menuButtonChangeCarColor.Size = new System.Drawing.Size(75, 37);
this.menuButtonChangeCarColor.TabIndex = 9;
this.menuButtonChangeCarColor.Text = "change";
this.menuButtonChangeCarColor.UseVisualStyleBackColor = false;
this.menuButtonChangeCarColor.Click += new System.EventHandler(this.menuButtonChangeCarColor_Click);
//
// extendedTextBoxPlayername
//
this.extendedTextBoxPlayername.Anchor = System.Windows.Forms.AnchorStyles.None;
this.extendedTextBoxPlayername.Location = new System.Drawing.Point(19, 83);
this.extendedTextBoxPlayername.Name = "extendedTextBoxPlayername";
this.extendedTextBoxPlayername.Size = new System.Drawing.Size(180, 22);
this.extendedTextBoxPlayername.TabIndex = 4;
//
// SingleplayerWidget
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.menuButtonChangeCarColor);
this.Controls.Add(this.panelCarColor);
this.Controls.Add(this.labelCarColor);
this.Controls.Add(this.extendedTextBoxPlayername);
this.Controls.Add(this.labelPlayername);
this.Controls.Add(this.labelWidgetHeadline);
this.MaximumSize = new System.Drawing.Size(225, 325);
this.MinimumSize = new System.Drawing.Size(225, 325);
this.Name = "SingleplayerWidget";
this.Size = new System.Drawing.Size(225, 325);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label labelWidgetHeadline;
private System.Windows.Forms.Label labelPlayername;
private System.Windows.Forms.ColorDialog colorDialogCarColor;
private Forms.ExtendedTextBox extendedTextBoxPlayername;
private System.Windows.Forms.Label labelCarColor;
private System.Windows.Forms.Panel panelCarColor;
private Forms.MenuButton menuButtonChangeCarColor;
}
}
我的GameConfigurationView
有AutoSize = False
。我的Widgets
具有相同的属性值和源代码(我在问题长度中排除了设计器文件)。 我知道以后需要清理源代码。
正如您可以看到以下源代码我在初始化新窗口小部件时也分配了大小。这也不起作用。
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Racetrack.Controller;
using Racetrack.View.Widgets;
namespace Racetrack.View
{
public partial class GameConfigurationView : Form
{
protected GameController Controller { get; set; }
protected MapSelectionWidget MapSelectionWidget { get; set; }
protected SingleplayerWidget SingleplayerWidget { get; set; }
protected Point GuiBuilderNextLocation { get; set; }
protected Size GuiBuilderFinalSize { get; set; }
protected int GuiBuilderTallestElementInCurrentRow { get; set; }
protected int GuiBuilderWidthOfCurrentRow { get; set; }
public GameConfigurationView(GameController controller)
{
InitializeComponent();
this.Controller = controller;
this.GuiBuilder();
this.menuButtonStartGame.Width = this.Width;
this.Size = this.GuiBuilderFinalSize;
}
private void GameConfigurationView_FormClosed(object sender, FormClosedEventArgs e)
{
this.Controller.ShowStartView();
}
/// <summary>
/// Builds the gui based on the configuration of the selected GameVariant.
/// </summary>
protected void GuiBuilder()
{
XmlNodeList configuration = this.Controller.SelectedGameVariant.GameConfigurationView;
this.GuiBuilderNextLocation = new Point(0, 0);
this.GuiBuilderFinalSize = new Size(0, 0);
this.GuiBuilderTallestElementInCurrentRow = 0;
for (int i = 0; i < configuration.Count; i++)
{
this.BuildOneRowFromGameVariantConfiguration(configuration[i].SelectNodes("elements/element"));
}
}
protected void BuildOneRowFromGameVariantConfiguration(XmlNodeList widgets)
{
this.GuiBuilderNextLocation = new Point(
this.GuiBuilderNextLocation.X - 1,
this.GuiBuilderNextLocation.Y - 1
);
for (int i = 0; i < widgets.Count; i++)
{
this.GuiBuilderNextLocation = new Point(
this.GuiBuilderNextLocation.X - 1,
this.GuiBuilderNextLocation.Y
);
string elementType = widgets[i].SelectSingleNode("type").InnerText;
string elementWidth = widgets[i].SelectSingleNode("width").InnerText;
switch (elementType)
{
case "SingleplayerWidget":
this.AddSingleplayerWidgetToGui();
break;
case "MapSelectionWidget":
this.AddMapSelectionWidgetToGui();
break;
case "MultiplayerWidget":
break;
case "NetworkClientWidget":
break;
case "NetworkHostWidget":
break;
case "startbutton":
break;
}
}
// After the row reset & update
this.GuiBuilderNextLocation = new Point(
0,
this.GuiBuilderTallestElementInCurrentRow + this.GuiBuilderNextLocation.Y
);
if (this.GuiBuilderWidthOfCurrentRow > this.GuiBuilderFinalSize.Width)
{
this.GuiBuilderFinalSize = new Size(
this.GuiBuilderWidthOfCurrentRow,
this.GuiBuilderFinalSize.Height + this.GuiBuilderTallestElementInCurrentRow
);
}
else
{
this.GuiBuilderFinalSize = new Size(
this.GuiBuilderFinalSize.Width,
this.GuiBuilderFinalSize.Height + this.GuiBuilderTallestElementInCurrentRow
);
}
this.GuiBuilderTallestElementInCurrentRow = 0;
this.GuiBuilderWidthOfCurrentRow = 0;
}
protected void AddSingleplayerWidgetToGui()
{
// Add to gui
this.SingleplayerWidget = new SingleplayerWidget(this.Controller);
this.SingleplayerWidget.BackColor = SystemColors.ButtonHighlight;
this.SingleplayerWidget.Location = this.GuiBuilderNextLocation;
this.SingleplayerWidget.Name = "SingleplayerWidget";
this.SingleplayerWidget.Size = new Size(225, 325);
this.SingleplayerWidget.TabIndex = 1;
this.SingleplayerWidget.Show();
this.Controls.Add(this.SingleplayerWidget);
// Update next location
this.GuiBuilderNextLocation = new Point(
this.SingleplayerWidget.Width + this.GuiBuilderNextLocation.X,
this.GuiBuilderNextLocation.Y
);
if (this.SingleplayerWidget.Height > this.GuiBuilderTallestElementInCurrentRow)
{
this.GuiBuilderTallestElementInCurrentRow = this.SingleplayerWidget.Height;
}
this.GuiBuilderWidthOfCurrentRow += this.SingleplayerWidget.Width + 1;
}
protected void AddMapSelectionWidgetToGui()
{
// Add to gui
this.MapSelectionWidget = new MapSelectionWidget(this.Controller);
this.MapSelectionWidget.BackColor = SystemColors.ButtonHighlight;
this.MapSelectionWidget.Location = this.GuiBuilderNextLocation;
this.MapSelectionWidget.Name = "MapSelectionWidget";
this.MapSelectionWidget.Size = new Size(510, 325);
this.MapSelectionWidget.TabIndex = 0;
this.MapSelectionWidget.Show();
this.Controls.Add(this.MapSelectionWidget);
// Update next location
this.GuiBuilderNextLocation = new Point(
this.MapSelectionWidget.Width + this.GuiBuilderNextLocation.X,
this.GuiBuilderNextLocation.Y
);
if (this.MapSelectionWidget.Height > this.GuiBuilderTallestElementInCurrentRow)
{
this.GuiBuilderTallestElementInCurrentRow = this.MapSelectionWidget.Height;
}
this.GuiBuilderWidthOfCurrentRow += this.MapSelectionWidget.Width + 1;
}
}
}
感谢您的帮助:)。
答案 0 :(得分:0)
我设法使事情有效。
我做了什么?由于OOP,我实施了BaseWidget
Label(s)
,其中包含Configuration
,Widget
等每个BaseWidget
可用/需要的AutoScaleMode = Font
。此AutoSize = True
具有以下设置:
AutoSizeMode = GrowOnly
Size = 275; 193
SubWidget
SubWidget
虽然大小会在相应的Forms
在每个Anchor = AnchorStyles.None
中,我使用GameConfigurationView
AutoScaleMode = None
AutoSize = True
的定义如下:
AutoSizeMode = GrowOnly
=Sum(IIF(Fields!Program.Value = "FC", Fields!QuantityToShip.Value, 0))
=Count(IIF(Fields!Mgroup.Value,"DataSet1"=303,1,0))
老实说,我不确定现在哪些设置确实是必要的,但我有这些设置并且有效。由于我有更多信息,我编辑了这个答案。