在vs2005多文档设计过程中,被一个小问题给犯难了--MDI子窗体显示最大化后主窗体菜单栏左上角的图标显示不正常。一般情况下,只要设置Form的Icon属性就可以了,但在VS2005 .net设计中确不这样。
创建新工程后,新建两个窗体:MainWindow和MdiChildWnd,并且分别这两个构函数中设置Icon属性。在MainWindow中添加一个菜单项newToolStripMenuItem,点击这个菜单菜既弹出MdiChildWnd子窗体。其中Properties.Resources.App则是在资源中添加的Icon图标(只能是16×16格式)。
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//MainWindow.cs
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
this.Icon = Properties.Resources.App;
this.IsMdiContainer = true;
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
MdiChildWnd child = new MdiChildWnd();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;//注意:若想打开后立即最大化,只有在子窗体声明时设置,若在MdiChildWnd 的构造函数中或在OnLoad中设置,则不会起到效果。
child.Show();
}
}
点击运行,则点击菜单项后的结果与我们预想的结果有些差距,如下图1:
![](https://www.cnblogs.com/images/cnblogs_com/pinckyang/%E5%9B%BE%E7%89%871.png)
图1
图1显示的子窗体最大化后左上角菜单栏没有显示预想的小图标。下面我只来添加一段代码,让应用程序“认真”地把图标显示出来。下面是改写后的主窗体的代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//MainWindow.cs
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
this.Icon = Properties.Resources.App;
this.IsMdiContainer = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
MdiChildWnd child = new MdiChildWnd();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Activated += new EventHandler(child_Activated);//注意:child_Activated是在子窗体第一次被激活时触发的事件。不过,也可以用GotFocus事件,GotFucus事件是在获点焦点时触发。
child.Show();
}
void child_Activated(object sender, EventArgs e)
{
((Form)sender).Icon = Properties.Resources.App;
}
}
加入以上代码后正常运行,如图2
![](https://www.cnblogs.com/images/cnblogs_com/pinckyang/%E5%9B%BE%E7%89%873.png)
图2