调试的时候发现程序走完InitializeComponent() 之后并没有回到调用的地方而是跳到了formsizechange函数的地方然后看堆栈的顺序好像是system导致了窗体的变化然后调用了formsizechange函数
看不懂。 有好心人可以解释一下吗~formsizechange 在最后面 
没有回到这里从这里跳出窗体的代码(有点长):#pragma once
#include "World.h"
World world; //全局变量
namespace WolfandSheep { using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing; /// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
} protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}

 protected: 
private: System::Windows::Forms::PictureBox^  pictureBox1;
private: System::Windows::Forms::Timer^  timer1;
private: System::Windows::Forms::Label^  label1;
private: System::Windows::Forms::Label^  label2;
private: System::Windows::Forms::Label^  label3;
private: System::Windows::Forms::NumericUpDown^  Wolfcounts;
private: System::Windows::Forms::NumericUpDown^  Sheepcounts;
private: System::Windows::Forms::NumericUpDown^  numGridSize;
private: System::Windows::Forms::Button^  btninit;
private: System::Windows::Forms::Button^  btnstart;
private: System::Windows::Forms::Button^  btnstop;
private: System::Windows::Forms::Button^  btnclose;
private: System::Windows::Forms::GroupBox^  groupBox1;
private: 
    private: System::Windows::Forms::Panel^  panel1;
private:System::ComponentModel::IContainer^  components;
/// <summary>
/// 必需的设计器变量。
/// </summary>
array<PictureBox^,2>^ gPic; 
ImageList^ gImageList;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->Wolfcounts = (gcnew System::Windows::Forms::NumericUpDown());
this->Sheepcounts = (gcnew System::Windows::Forms::NumericUpDown());
this->numGridSize = (gcnew System::Windows::Forms::NumericUpDown());
this->btninit = (gcnew System::Windows::Forms::Button());
this->btnstart = (gcnew System::Windows::Forms::Button());
this->btnstop = (gcnew System::Windows::Forms::Button());
this->btnclose = (gcnew System::Windows::Forms::Button());
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
        this->groupBox1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Wolfcounts))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Sheepcounts))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numGridSize))->BeginInit();

this->SuspendLayout();
// 
// panel1
// 
this->panel1->Location = System::Drawing::Point(0, 1);
this->panel1->Margin = System::Windows::Forms::Padding(4);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(890, 594);
this->panel1->TabIndex = 0;
// 
// pictureBox1
// 
this->pictureBox1->Location = System::Drawing::Point(56, 42);
this->pictureBox1->Margin = System::Windows::Forms::Padding(4);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(776, 501);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
// 
// timer1
// 
this->timer1->Interval = 1000;
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
// 
// label1
// 
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"宋体", 10.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
static_cast<System::Byte>(134)));
this->label1->Location = System::Drawing::Point(33, 56);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(85, 19);
this->label1->TabIndex = 1;
this->label1->Text = L"狼的数量";
// 
// label2
// 
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"宋体", 10.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
static_cast<System::Byte>(134)));
this->label2->Location = System::Drawing::Point(34, 107);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(85, 19);
this->label2->TabIndex = 2;
this->label2->Text = L"羊的数量";
// 
// label3
// 
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"宋体", 10.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
static_cast<System::Byte>(134)));
this->label3->Location = System::Drawing::Point(34, 153);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(85, 19);
this->label3->TabIndex = 3;
this->label3->Text = L"网格数量";
// 
// Wolfcounts
// 
this->Wolfcounts->Location = System::Drawing::Point(135, 53);
this->Wolfcounts->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) {99, 0, 0, 0});
this->Wolfcounts->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) {1, 0, 0, 0});
this->Wolfcounts->Name = L"Wolfcounts";
this->Wolfcounts->Size = System::Drawing::Size(84, 25);
this->Wolfcounts->TabIndex = 4;
this->Wolfcounts->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) {5, 0, 0, 0});
this->Wolfcounts->ValueChanged += gcnew System::EventHandler(this, &Form1::Wolfcounts_ValueChanged);
// 
// Sheepcounts
// 
this->Sheepcounts->Location = System::Drawing::Point(135, 104);
this->Sheepcounts->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) {99, 0, 0, 0});
this->Sheepcounts->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) {1, 0, 0, 0});
this->Sheepcounts->Name = L"Sheepcounts";
this->Sheepcounts->Size = System::Drawing::Size(84, 25);
this->Sheepcounts->TabIndex = 5;
this->Sheepcounts->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) {20, 0, 0, 0});
this->Sheepcounts->ValueChanged += gcnew System::EventHandler(this, &Form1::Sheepcounts_ValueChanged);
// 
// numGridSize
// 
this->numGridSize->Location = System::Drawing::Point(135, 149);
this->numGridSize->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) {30, 0, 0, 0});
this->numGridSize->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) {5, 0, 0, 0});
this->numGridSize->Name = L"numGridSize";
this->numGridSize->Size = System::Drawing::Size(84, 25);
this->numGridSize->TabIndex = 6;
this->numGridSize->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) {10, 0, 0, 0});
this->numGridSize->ValueChanged += gcnew System::EventHandler(this, &Form1::numGridSize_ValueChanged);
// 
// btninit
// 
this->btninit->Location = System::Drawing::Point(42, 280);
this->btninit->Name = L"btninit";
this->btninit->Size = System::Drawing::Size(176, 39);
this->btninit->TabIndex = 7;
this->btninit->Text = L"初始化";
this->btninit->UseVisualStyleBackColor = true;
this->btninit->Click += gcnew System::EventHandler(this, &Form1::btninit_Click);
// 
// btnstart
// 
this->btnstart->Location = System::Drawing::Point(42, 347);
this->btnstart->Name = L"btnstart";
this->btnstart->Size = System::Drawing::Size(177, 40);
this->btnstart->TabIndex = 8;
this->btnstart->Text = L"开始";
this->btnstart->UseVisualStyleBackColor = true;
this->btnstart->Click += gcnew System::EventHandler(this, &Form1::btnstart_Click);
// 
// btnstop
// 
this->btnstop->Location = System::Drawing::Point(42, 414);
this->btnstop->Name = L"btnstop";
this->btnstop->Size = System::Drawing::Size(177, 41);
this->btnstop->TabIndex = 9;
this->btnstop->Text = L"暂停";
this->btnstop->UseVisualStyleBackColor = true;
this->btnstop->Click += gcnew System::EventHandler(this, &Form1::btnstop_Click);
// 代码太长了下面接~

解决方案 »

  1.   

    // btnclose
    // 
    this->btnclose->Location = System::Drawing::Point(42, 483);
    this->btnclose->Name = L"btnclose";
    this->btnclose->Size = System::Drawing::Size(177, 42);
    this->btnclose->TabIndex = 10;
    this->btnclose->Text = L"结束";
    this->btnclose->UseVisualStyleBackColor = true;
    this->btnclose->Click += gcnew System::EventHandler(this, &Form1::btnclose_Click);
    // 
    // groupBox1
    // 
    this->groupBox1->Controls->Add(this->btnclose);
    this->groupBox1->Controls->Add(this->btnstop);
    this->groupBox1->Controls->Add(this->btnstart);
    this->groupBox1->Controls->Add(this->btninit);
    this->groupBox1->Controls->Add(this->numGridSize);
    this->groupBox1->Controls->Add(this->Sheepcounts);
    this->groupBox1->Controls->Add(this->Wolfcounts);
    this->groupBox1->Controls->Add(this->label3);
    this->groupBox1->Controls->Add(this->label2);
    this->groupBox1->Controls->Add(this->label1);
    this->groupBox1->Location = System::Drawing::Point(890, 1);
    this->groupBox1->Name = L"groupBox1";
    this->groupBox1->Size = System::Drawing::Size(266, 593);
    this->groupBox1->TabIndex = 11;
    this->groupBox1->TabStop = false;
    this->groupBox1->Text = L"控制面板";
    // 
    // Form1
    // 
    this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System::Drawing::Size(1155, 594);
    this->Controls->Add(this->groupBox1);
    this->Controls->Add(this->pictureBox1);
    this->Controls->Add(this->panel1);
    this->Name = L"Form1";
    this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
    this->Text = L"WolfandSheep";
    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    this->SizeChanged += gcnew System::EventHandler(this, &Form1::Form1_SizeChanged);
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Wolfcounts))->EndInit();
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Sheepcounts))->EndInit();
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numGridSize))->EndInit();
    this->groupBox1->ResumeLayout(false);
    this->groupBox1->PerformLayout();
    this->ResumeLayout(false); }
    #pragma endregionprivate: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
     //初始化界面
     gImageList = gcnew ImageList;
     gImageList->ImageSize = System::Drawing::Size(48,48);/*wolf.png*//*grass.png*//*sheep.png*/
     gImageList->Images->Add(Image::FromFile("Wolf.jpg"));
     gImageList->Images->Add(Image::FromFile("Grass.png"));
     gImageList->Images->Add(Image::FromFile("Sheep.jpg"));
     //gImageList->Images->Add( Image::FromFile( "F:\C_project\WAS\614\WolfandSheep\WolfandSheep\Wolf.jpg" ) ); //索引0代表狼
     //gImageList->Images->Add( Image::FromFile( "F:\C_project\WAS\614\WolfandSheep\WolfandSheep\Grass.jpg" ) );//索引1代表草
     //gImageList->Images->Add( Image::FromFile( "F:\C_project\WAS\614\WolfandSheep\WolfandSheep\Sheep.jpg" ) );//索引2代表羊
     RandGrid();
     }
    #pragma region
     //初始化狼和羊
    void RandGrid()
    {
    int size=10;
    int gridsize=(int)numGridSize->Value;
    this->pictureBox1->Controls->Clear();//清除原有图片
    //设置方格大小
    if(this->panel1->Size.Height<this->panel1->Size.Width)
    {
    size=this->panel1->Size.Height/gridsize;
    this->pictureBox1->Size = System::Drawing::Size(this->panel1->Size.Height,this->panel1->Size.Height);
    this->pictureBox1->Location = System::Drawing::Point((this->panel1->Size.Width-this->panel1->Size.Height)/2,0);
    }
    else
    {
    size=this->panel1->Size.Width/gridsize;
    this->pictureBox1->Size = System::Drawing::Size(this->panel1->Size.Width,this->panel1->Size.Width);
    this->pictureBox1->Location = System::Drawing::Point(0,(this->panel1->Size.Height-this->panel1->Size.Width)/2);
    }
    gPic=gcnew array<PictureBox^,2>(gridsize,gridsize);
    Random^ rand=gcnew Random();
    for(int i=0;i<gridsize;i++)
    {
    for(int j=0;j<gridsize;j++)
    {
    gPic[i,j]=gcnew PictureBox();  //设置图片框的属性
    gPic[i,j]->Location = System::Drawing::Point(j*size,i*size);
    gPic[i,j]->Size = System::Drawing::Size(size, size);
    gPic[i,j]->SizeMode = PictureBoxSizeMode::StretchImage;
    gPic[i,j]->BorderStyle=BorderStyle::FixedSingle;
    this->pictureBox1->Controls->Add(gPic[i,j]);
    }
    world.setGridSize(gridsize);
    int sheep=0;
    while(sheep<(int)Sheepcounts->Value)
    {
    int x = rand->Next() % gridsize;
    int y = rand->Next() % gridsize;
    if (world.getAt(x,y)==NULL)
    {
    sheep++;
    Sheep *s= new Sheep(&world,x,y); //实例化羊对象
    }
    }
    int wolf=0;
    while (wolf <(int)Wolfcounts->Value)
    {
    int x =rand->Next() % gridsize;
    int y =rand->Next() % gridsize;
    if (world.getAt(x,y)==NULL)
    {
    wolf++;
    Wolf *wf = new Wolf(&world,x,y); //实例化狼对象
    }
    }
    }
    display();
    }
    void display()
    {
    for (int i=0; i<world.getGridsize(); i++)
    {
    for ( int j=0; j<world.getGridsize(); j++)
    {
    gPic[i,j]->Image=this->gImageList->Images[world.Display(i,j)];
    }
    }
    }
    #pragma endregion 
    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
     world.SimulateOneStep();
     display();
     }
    private: System::Void btnstart_Click(System::Object^  sender, System::EventArgs^  e) {
     timer1->Enabled=true;
     }
    private: System::Void btninit_Click(System::Object^  sender, System::EventArgs^  e) {
     RandGrid();
     }
    private: System::Void btnstop_Click(System::Object^  sender, System::EventArgs^  e) {
     timer1->Enabled=false;  }
    private: System::Void btnclose_Click(System::Object^  sender, System::EventArgs^  e) {
     this->Close();
     }private: System::Void numGridSize_ValueChanged(System::Object^  sender, System::EventArgs^  e) {
     Wolfcounts->Maximum=numGridSize->Value*numGridSize->Value-1;
     Sheepcounts->Maximum=Wolfcounts->Maximum;
     }
    private: System::Void Wolfcounts_ValueChanged(System::Object^  sender, System::EventArgs^  e) {
     if(Wolfcounts->Value+Sheepcounts->Value>numGridSize->Value*numGridSize->Value)
     {  MessageBox::Show( "狼与羊的数量之和不能超过网格数!", "狼的数量有误",
     MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
     }
     Wolfcounts->Value=1;
     }
    private: System::Void Sheepcounts_ValueChanged(System::Object^  sender, System::EventArgs^  e) {
     if(Sheepcounts->Value+Wolfcounts->Value>numGridSize->Value*numGridSize->Value)
     {  MessageBox::Show( "狼与羊的数量之和不能超过网格数!", "羊的数量有误",
     MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
     Sheepcounts->Value=1;
     }
     }
    private: System::Void Form1_SizeChanged(System::Object^  sender, System::EventArgs^  e) {
     RandGrid();
     }
    };
    }调用的堆栈: