询问者
winform窗口最大化问题

问题
-
1、要将一个mdi子窗体设置为程序打开时即最大化,将窗体 WindowState 的 Maximized 设为 true ,启动时右边部分是一块空白,即好象只有子窗体标题栏最大化了,而窗体内容部分还是原来的大小。如果采用代码方式(childForm .MdiParent = this;childForm .WindowState = FormWindowState.Maximized;childForm .Show();)可以最大化,但子窗体左上角本来设置了个性图标的,却变成默认的图标了。
2、如果有两个子窗体,一个已经最大化了,再打开另一个子窗体,这个子窗体也会自动为最大化状态,即使后来打开的这个窗体的 FormBorderStyle 已设置为 FixedSingle 。难道 mdi 下所有的子窗体只允许有一种状态(要么全最大化,要么全最小化)?
全部回复
-
不是的。
窗体的初始可见状态如下:
Normal======正常
Minimized====最小化
Maximized====最大化
如果是SDI单文档窗口:只要设置WindowState值为Maximized就可以了。
如下面的代码:
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
如果是MDI多文档窗口:
即要设置MDI多文档子窗口的父窗口,并且要设置父窗口的为MID容器,
设置MDI容器的代码:
this.IsMdiContainer = true;
首先要建立一个MDI父窗体:
这里我要建立的父窗体为Form1 代码如下:
1. FORM1.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MDITEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}2. FORM1.DESIGNER.CS
namespace MDITEST
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(517, 436);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
}
}
3. PROGRAM.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MDITEST
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}其次要建立一个MDI子窗体:
右击项目,在弹出的菜单中选择“添加”,然后添加一个WINDOWS窗体,名字为FORM2.
MDI子窗体FORM2的代码如下:
FORM2.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MDITEST
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}FORM2.DESIGNER.CS
namespace MDITEST
{
partial class Form2
{
/// <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.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form2";
}
#endregion
}
}双击FORM1的设计窗口:
FORM1.CS 会自动添加如下代码:
private void Form1_Load(object sender, EventArgs e)
{
}FORM1.DESIGNER.CS 会添加如下代码:
this.Load += new System.EventHandler(this.Form1_Load);
上面的两处代码为FORM1窗口添加了一个处理事件。
即当加载窗口FROM1时发生的事件。
在private void Form1_Load(object sender, EventArgs e)中加入下面的代码:
Form2 FM2 = new Form2();
FM2.MdiParent = this;
FM2.Show();此时运行FORM1.EXE时FORM1最大化了,它的子窗体FORM2为NORMAL 状态。
这时在FORM2子窗体中加入如下代码即可。
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
上面的一行代码加在FORM2.DESIGNER.CS中:
FORM2.DESIGNER.CS 如下:
namespace MDITEST
{
partial class Form2
{
/// <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();
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form2";
this.Text = "Form2";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
}
}此时运行FORM1.EXE MDI父窗体和子窗体都最大化了。
如果说的有错误,请不指出,因本人水平有限。
谢谢。