C# 将windows窗体转换为windows Metro窗体

C# 将windows窗体转换为windows Metro窗体,c#,forms,microsoft-metro,windows-forms-designer,C#,Forms,Microsoft Metro,Windows Forms Designer,我已经用普通的windows格式编写了一个桌面应用程序,它运行得很好。后来我试着把它改成地铁的形式,因为它看起来很现代。在控件名上做了一些小改动后,我使用了旧范式中的大部分代码。但是表单大小调整功能块在启动应用程序时抛出“System.NullReferenceException”。但是在评论了调整大小事件之后,它工作得很好 private void Form1_Load(object sender, EventArgs e) { defaultSize = this.Size;

我已经用普通的windows格式编写了一个桌面应用程序,它运行得很好。后来我试着把它改成地铁的形式,因为它看起来很现代。在控件名上做了一些小改动后,我使用了旧范式中的大部分代码。但是表单大小调整功能块在启动应用程序时抛出“System.NullReferenceException”。但是在评论了调整大小事件之后,它工作得很好

private void Form1_Load(object sender, EventArgs e)
{
    defaultSize = this.Size;
    panel4Size = new Rectangle[] { new Rectangle(panel4.Location.X, panel4.Location.Y, panel4.Width, panel4.Height), new Rectangle(qusNo.Location.X, qusNo.Location.Y, qusNo.Width, qusNo.Height), new Rectangle(element1.Location.X, element1.Location.Y, element1.Width, element1.Height), new Rectangle(element2.Location.X, element2.Location.Y, element2.Width, element2.Height), new Rectangle(operation.Location.X, operation.Location.Y, operation.Width, operation.Height), new Rectangle(line.Location.X, line.Location.Y, line.Width, line.Height), new Rectangle(nextQus.Location.X, nextQus.Location.Y, nextQus.Width, nextQus.Height), new Rectangle(flp1.Location.X, flp1.Location.Y, flp1.Width, flp1.Height), new Rectangle(flp2.Location.X, flp2.Location.Y, flp2.Width, flp2.Height), new Rectangle(opt1.Location.X, opt1.Location.Y, opt1.Width, opt1.Height), new Rectangle(opt2.Location.X, opt2.Location.Y, opt2.Width, opt2.Height), new Rectangle(opt3.Location.X, opt3.Location.Y, opt3.Width, opt3.Height), new Rectangle(opt4.Location.X, opt4.Location.Y, opt4.Width, opt4.Height), new Rectangle(opt5.Location.X, opt5.Location.Y, opt5.Width, opt5.Height), new Rectangle(opt6.Location.X, opt6.Location.Y, opt6.Width, opt6.Height), new Rectangle(dopt1.Location.X, dopt1.Location.Y, dopt1.Width, dopt1.Height), new Rectangle(dopt2.Location.X, dopt2.Location.Y, dopt2.Width, dopt2.Height), new Rectangle(dopt3.Location.X, dopt3.Location.Y, dopt3.Width, dopt3.Height), new Rectangle(dopt4.Location.X, dopt4.Location.Y, dopt4.Width, dopt4.Height), new Rectangle(dopt5.Location.X, dopt5.Location.Y, dopt5.Width, dopt5.Height), new Rectangle(dopt6.Location.X, dopt6.Location.Y, dopt6.Width, dopt6.Height), new Rectangle(LaBackBt.Location.X, LaBackBt.Location.Y, LaBackBt.Width, LaBackBt.Height), new Rectangle(LaFinishBt.Location.X, LaFinishBt.Location.Y, LaFinishBt.Width, LaFinishBt.Height), new Rectangle(results.Location.X, results.Location.Y, results.Width, results.Height) };
    panel2Size = new Rectangle[] { new Rectangle(panel2.Location.X, panel2.Location.Y, panel2.Width, panel2.Height), new Rectangle(Easy.Location.X, Easy.Location.Y, Easy.Width, Easy.Height), new Rectangle(Hard.Location.X, Hard.Location.Y, Hard.Width, Hard.Height), new Rectangle(MdBackBt.Location.X, MdBackBt.Location.Y, MdBackBt.Width, MdBackBt.Height), new Rectangle(eelem1.Location.X, eelem1.Location.Y, eelem1.Width, eelem1.Height), new Rectangle(eelem2.Location.X, eelem2.Location.Y, eelem2.Width, eelem2.Height), new Rectangle(eelem3.Location.X, eelem3.Location.Y, eelem3.Width, eelem3.Height), new Rectangle(eelem4.Location.X, eelem4.Location.Y, eelem4.Width, eelem4.Height), new Rectangle(eelem5.Location.X, eelem5.Location.Y, eelem5.Width, eelem5.Height), new Rectangle(flp3.Location.X, flp3.Location.Y, flp3.Width, flp3.Height), new Rectangle(flp4.Location.X, flp4.Location.Y, flp4.Width, flp4.Height), new Rectangle(flp5.Location.X, flp5.Location.Y, flp5.Width, flp5.Height), new Rectangle(flp6.Location.X, flp6.Location.Y, flp6.Width, flp6.Height) };
    panel1Size = new Rectangle[] { new Rectangle(panel1.Location.X, panel1.Location.Y, panel1.Width, panel1.Height), new Rectangle(label1.Location.X, label1.Location.Y, label1.Width, label1.Height), new Rectangle(listView3.Location.X, listView3.Location.Y, listView3.Width, listView3.Height), new Rectangle(HmAdd.Location.X, HmAdd.Location.Y, HmAdd.Width, HmAdd.Height), new Rectangle(HmSub.Location.X, HmSub.Location.Y, HmSub.Width, HmSub.Height), new Rectangle(HmMul.Location.X, HmMul.Location.Y, HmMul.Width, HmMul.Height), new Rectangle(HmDiv.Location.X, HmDiv.Location.Y, HmDiv.Width, HmDiv.Height) };
}

private void reziseChildControl()
{
    Control[] controlP4 = new Control[] { panel4, qusNo, element1, element2, operation, line, nextQus, flp1, flp2, opt1, opt2, opt3, opt4, opt5, opt6, dopt1, dopt2, dopt3, dopt4, dopt5, dopt6, LaBackBt, LaFinishBt, results };
    Control[] controlP2 = new Control[] { panel2, Easy, Hard, MdBackBt, eelem1, eelem2, eelem3, eelem4, eelem5, flp3, flp4, flp5, flp6 };
    Control[] controlP1 = new Control[] { panel1, label1, listView3, HmAdd, HmSub, HmMul, HmDiv};
    for (int j = 0; j < controlP4.Length; j++)
    {
        resizeControls(panel4Size[j], controlP4[j]);
    }
    for (int j = 0; j < controlP2.Length; j++)
    {
        resizeControls(panel2Size[j], controlP2[j]);
    }
    for (int j = 0; j < controlP1.Length; j++)
    {
        resizeControls(panel1Size[j], controlP1[j]);
    }

    }
    private void Form1_Resize(object sender, EventArgs e)
    {
        reziseChildControl();
    }

    private void resizeControls(Rectangle originalContrl, Control control)
    {
        float xRatio = (float)(this.Width) / (float)(defaultSize.Width);
        float yRatio = (float)(this.Height) / (float)(defaultSize.Height);
        int newX = (int)(originalContrl.X * xRatio);
        int newY = (int)(originalContrl.Y * yRatio);
        int newWidth = (int)(originalContrl.Width * xRatio);
        int newHeight = (int)(originalContrl.Height * yRatio);
        control.Location = new Point(newX, newY);
        control.Size = new Size(newWidth, newHeight);
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{
defaultSize=this.Size;
panel4Size=new Rectangle[]{新矩形(panel4.Location.X,panel4.Location.Y,panel4.Width,panel4.Height),新矩形(qusNo.Location.X,qusNo.Location.Y,qusNo.Width,qusNo.Height),新矩形(element1.Location.X,element1.Location.Y,element1.Width,element1.Height),新矩形(element2.Location.X,element2.Location.Y,element2.Width,element2.Height),新矩形(operation.Location.X,operation.Location.Y,operation.Width,operation.Height),新矩形(line.Location.X,line.Width,line.Height),新矩形(nextQus.Location.X,nextQus.Location.Y,nextQus.Width,nextQus.Height),新矩形(flp1.Location.X,flp1.Location.Y,flp1.Width,flp1.Height),新矩形(flp2.Location.X,flp2.Location.Y,flp2.Width,flp2.Height),新矩形(opt1.Location.X,opt1.Location.Y,opt1.Width),新矩形(opt2.Location.X,opt2.Location.Y,opt2.Width,opt2.Height),新矩形(opt3.Location.X,opt3.Location.Y,opt3.Width,opt3.Height),新矩形(opt4.Location.X,opt4.Location.Y,opt4.Width,opt4.Height),新矩形(opt5.Location.X,opt5.Location.Y,opt5.Width,opt5.Height),新矩形(opt6.Location.X,opt6.Location.Y,opt6.Width,opt6.Height),新矩形(dopt1.Location.X,dopt1.Location.Y,dopt1.Width,dopt1.Height),新矩形(dopt2.Location.X,dopt2.Location.Y,dopt2.Width,dopt2.Height),新矩形(dopt3.Location.X,dopt4.Location.Y,dopt4.Width,dopt4.Height),新矩形(dopt5.Location.X,dopt5.Location.Y,dopt5.Width,dopt5.Height),新矩形(dopt6.Location.X,dopt6.Location.Y,dopt6.Width,dopt6.Height),新矩形(LaBackBt.Location.X,LaBackBt.Location.Y,LaBackBt.Width,LaBackBt.Height),新矩形(LaFinishBt.Location.X,LaFinishBt.Location.Y,LaFinishBt.Width,LaFinishBt.Height),新矩形(results.Location.X,results.Location.Y,results.Width,results.Height)};
panel2Size=new Rectangle[]{新矩形(panel2.Location.X,panel2.Location.Y,panel2.Width,panel2.Height),新矩形(Easy.Location.X,Easy.Location.Y,Easy.Width,Easy.Height),新矩形(MdBackBt.Location.X,MdBackBt.Location.Y,MdBackBt.Width,MdBackBt.Height),新矩形(eelem1.Location.X,eelem1.Location.Y,eelem1.Width,eelem1.Height),新矩形(eelem2.Location.X,eelem2.Location.Y,eelem2.Width,eelem2.Height),新矩形(eelem3.Location.X,eelem3.Location.Y,eelem3.Width),新矩形(eelem4.Location.X,eelem4.Location.Y,eelem4.Width,eelem4.Height),新矩形(eelem5.Location.X,eelem5.Location.Y,eelem5.Width,eelem5.Height),新矩形(flp3.Location.X,flp3.Location.Y,flp3.Width,flp3.Height),新矩形(flp4.Location.X,flp4.Location.Y,flp4.Width,flp4.Height),新矩形(flp5.Location.X,flp5.Location.Y,flp5.Width,flp5.Height),新矩形(flp6.Location.X,flp6.Location.Y,flp6.Width,flp6.Height)};
panel1Size=新矩形[]{新矩形(panel1.Location.X,panel1.Location.Y,panel1.Width,panel1.Height),新矩形(label1.Location.X,label1.Location.Y,label1.Width,label1.Height),新矩形(listView3.Location.X,listView3.Location.Y,listView3.Width,listView3.Height),新矩形(HmAdd.Location.X,HmAdd.Location.Y,HmAdd.Width,HmAdd.Height),新矩形(HmSub.Location.X,HmSub.Location.Y,HmSub.Width,HmSub.Height),新矩形(HmMul.Location.X,HmMul.Location.Y,HmMul.Height),新矩形(HmDiv.Location.X,HmDiv.Location.Y,HmDiv.Width,HmDiv.Height)};
}
私有void reziseChildControl()
{
Control[]controlP4=新控件[]{PANELEN4,qusNo,element1,element2,OPT,line,nextQus,flp1,flp2,opt1,opt2,opt3,opt4,opt5,opt6,dopt1,dopt2,dopt3,dopt4,dopt5,dopt6,LaBackBt,LaFinishBt,results};
Control[]controlP2=新控件[]{panel2,Easy,Hard,MdBackBt,eelem1,eelem2,eelem3,eelem4,eelem5,flp3,flp4,flp5,flp6};
Control[]controlP1=新控件[]{panel1,label1,listView3,HmAdd,HmSub,HmMul,HmDiv};
对于(int j=0;j
更新:引发NullReferenceException,因为Form1_Resize事件发生在Form1_Load事件之前。在我更改窗口布局(通过拖动或最大化窗口大小)之前,不应调用Form1_Resize事件。但在Form1 comp之前启动应用程序时自动调用Resize事件