Не Запускается Окно Interfaceform

22.12.2013
3
0
#1
Компилирую в VS2005,С++/CLI.
Не запускается интерфейсное окно состояний InterfaceForm:Form.

C++:
#include "stdafx.h"
#include <msclr/lock.h>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace System::Threading;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections;
using namespace msclr;


ref class Refill
{
public: int x, y;
property int X {int get() { return x; } }
property int Y {int get() { return y; } }

public: Refill(int X, int Y)
{ x = X; y = Y; }
};

delegate void delEv();

ref class Car //класс машины
{
public:
event delEv ^ev;
private:
int num; //номер машины
int speed; //скорость машины
int r; //радиус кольца
String ^name; //имя машины
int x, y; //текущая позиция машины
bool great, life; //жизнь потока - признак движения по большому/малому кругу
Thread ^t1; //поток
bool run;
bool moving;
int indikator, max_indikator; //индикатор наполнения бензобака
public:
ArrayList ^aL;
bool eat;
private:
Refill ^refill;
int i;
int fuel;
int maxFuel;
int dx, dy;
//lock ^l;

public: 
Car(int N, String ^Name, int X, int Y, bool greatNew, Refill ^R)
{
num = N;
name = Name;
speed = 2;
x = X;
y = Y;
great = greatNew;
run = false;
life = false;
moving = false;
refill = R;
maxFuel = 100;
fuel = maxFuel;
eat = false;
Start();// 
}

~Car()
{
life = false;
}	// Деструктор

void Start()		// Стартовать
{
if (!run)
{
run = true;
life = true;
t1 = gcnew Thread(gcnew ThreadStart( this, &Car::Move));
t1->Start();
}
}

void Suspend()  // Приостановить
{
moving = true;
/*if (run)
{
run = false;
t1.Suspend();
}*/
}

void Resume()	// Возобновить
{
lock l(this);
{
moving = false;
Monitor::Pulse(this);
}
/*if (!run)
{
run = true;
t1.Resume();
}*/
}

property int N
{
int get()
{
return num;
}
}
property int X			//свойство координаты X центра машины
{
int get()
{
return x;
}
}
property int Y			//свойство координаты Y центра машины
{
int get()
{
return y;
}
}


int counter;	  // время существования
void Move()		//движение
{

while (life)
{
counter++;
if (great) OnCirclebigroad();
if (!great) OnCirclesmallroad();
ev();
Thread::Sleep(100);
lock l(this);
{
if (moving)
Monitor::Wait(this);
}
}
}

void OnCirclebigroad() //движение по большому кругу
{
r = 160;
x = 170 + (int)Math::Round(r * Math::Cos(counter * 0.1 * speed));
y = 170 + (int)Math::Round(r * Math::Sin(counter * 0.1 * speed));

Console::WriteLine("X=" + x + "Y=" + y + "R=" + r);

fuel--;

if (fuel < 30)
{
if (!eat)
{
eat = true;
x = 400;
y = 150;
lock l(refill);
{
Thread::Sleep(1300);
fuel = maxFuel;
eat = false;
}
}
great = true;
}
}

void OnCirclesmallroad() //движение по малому кругу
{
r = 120;
x = 170 + (int)Math::Round(r * Math::Cos(counter * 0.1 * -1 * speed));
y = 170 + (int)Math::Round(r * Math::Sin(counter * 0.1 * -1 * speed));

Console::WriteLine("X=" + x + "Y=" + y + "R=" + r);

fuel--;

if (fuel < 30)
{
if (!eat)
{
eat = true;
x = 400;
y = 150;
lock l(refill);
{
Thread::Sleep(1300);
fuel = maxFuel;
eat = false;
}
}
great = false;
}
}

void Finish()
{
life = false;
}
};

ref class Cars : Form
{
public: 
ArrayList ^aL;
int n;
int static carsCount = 2; // кол-во машин (начальное значение 2 )
private: 
Refill ^refill;

public: Cars()
{
refill = gcnew Refill(370, 100);
}

void Start()		 // Стартовать
{
for (int i = 0; i < aL->Count; i++)
{
dynamic_cast<Car^>(aL[i])->Start();
}
}
void Suspend()	 // Возобновить
{
for (int i = 0; i < aL->Count; i++)
{
dynamic_cast<Car^>(aL[i])->Suspend();
}

}

void Resume()	 // Приостановить
{
for (int i = 0; i < aL->Count; i++)
{
dynamic_cast<Car^>(aL[i])->Resume();
}
}

virtual void OnPaint(PaintEventArgs ^e) override //рисование
{

e->Graphics->DrawEllipse(gcnew Pen(Color::Black, 40), 20, 20, 320, 320);
e->Graphics->DrawEllipse(gcnew Pen(Color::Black, 40), 65, 65, 230, 230);
e->Graphics->FillRectangle(gcnew SolidBrush(Color::Brown), refill->X, refill->Y, 70, 170);
for (int i = 0; i < aL->Count; i++)
{
//рисование машины
e->Graphics->DrawRectangle(gcnew Pen(Color::Red, 4), dynamic_cast<Car^>(aL[i])->X, dynamic_cast<Car^>(aL[i])->Y, 20, 20);
e->Graphics->DrawString(dynamic_cast<Car^>(aL[i])->N.ToString(), Font, gcnew SolidBrush(Color::White), dynamic_cast<Car^>(aL[i])->X + 4, dynamic_cast<Car^>(aL[i])->Y + 4);
}
}
};

ref class User : Cars				 // Класс пользователя машин
{
public: bool run;
public: 
Form ^pChildForm, ^Interface;
Button ^pBut, ^pBut2, ^pBut3;
TextBox ^Text0;
private: Refill ^refill;
Random ^rand;

public: User()
{

refill = gcnew Refill(400, 150);
run = true;
//---------------		  
pBut = gcnew Button();
pBut->Location = Point(32, 24);
pBut->Name = "pBut";
pBut->Size = *gcnew System::Drawing::Size(120, 30);
pBut->Text = "Suspend/Resume";
pBut->Click += gcnew EventHandler(this, &User::OnBut);
//---------------
pBut2 = gcnew Button();
pBut2->Location = Point(120, 24);
pBut2->Name = "pBut";
pBut2->Size = *gcnew System::Drawing::Size(120, 30);
pBut2->Text = "Delete";
pBut2->Click += gcnew EventHandler(this, &User::Delete);
//---------------
pBut3 = gcnew Button();
pBut3->Location = Point(210, 24);
pBut3->Name = "pBut";
pBut3->Size = *gcnew System::Drawing::Size(120, 30);
pBut3->Text = "Add a car";
pBut3->Click += gcnew EventHandler(this, &User::Add);
ClientSize = *gcnew System::Drawing::Size(600, 450);
//---------------
pChildForm = gcnew Form();
pChildForm->Location = Point(700, 700);
pChildForm->Size = *gcnew System::Drawing::Size(800, 450);
pChildForm->Text = "User";
pChildForm->Show();
pChildForm->Controls->Add(pBut);
pChildForm->Controls->Add(pBut2);
pChildForm->Controls->Add(pBut3);
Text0 = gcnew TextBox();
Text0->Text = "1";
Text0->Location = Point(200, 60);
Text0->Name = "T0";
Text0->Size = *gcnew System::Drawing::Size(80, 20);
pChildForm->ClientSize = *gcnew System::Drawing::Size(350, 100);
pChildForm->Controls->Add(Text0);
//-----------------Interface--------------
Interface = gcnew Form(); // Создать дочернее окно (форму)
Interface->Location = Point(700, 100);// и разместить его.
Interface->Text = "Interface"; // Задать заголовок дочернего окна
Interface->Show(); // Показать дочернее окно 
Interface->ClientSize = *gcnew System::Drawing::Size(800, 200);

n = carsCount;
this->DoubleBuffered = true;
aL = gcnew ArrayList(n);
for (int i = 0; i < aL->Capacity; i++)
{
if (i % 2 == 1)
{
Car ^c = gcnew Car(i, "Машина", 165, 330, true, refill);
c->ev += gcnew delEv(this, &User::HandlerEv);
aL->Add(c);
c->Start();
}
else
{
Car ^c = gcnew Car(i, "Машина", 312, 96, false, refill);
c->ev += gcnew delEv(this, &User::HandlerEv);
aL->Add(c);
c->Start();
}
}
Text = "Cars";
}

void HandlerEv() // Обработать событие
{

Interface->Invalidate();
this->Invalidate();

}


void OnBut(Object ^obj, EventArgs ^e) // Обработчик кнопки
{

Console::WriteLine("OkMouse");
if (run)
{
run = false;
Suspend();
}
else
if (!run)
{
run = true;
Resume();
}
Interface->Invalidate();
}

void Add(Object ^obj, EventArgs ^e)
{
try
{
Random ^rand = gcnew Random();
for (int i = 0; i < Convert::ToInt32(Text0->Text); i++)
{
if (rand->Next(10) % 2 == 1)
{
Car ^c = gcnew Car(carsCount, "Машина", 165, 330, true, refill);
c->ev += gcnew delEv(this, &User::HandlerEv);
aL->Add(c);
c->Start();

}
else
{
Car ^c = gcnew Car(carsCount, "Машина", 312, 96, false, refill);
c->ev += gcnew delEv(this, &User::HandlerEv);
aL->Add(c);
c->Start();
}
carsCount++;
}
Update();
}
catch (Exception ^ex)
{
}
}

void Delete(Object ^obj, EventArgs ^e)
{
try
{
for (int i = 0; i < Convert::ToInt32(Text0->Text); i++)
{
if (aL->Count != 0)
{
aL->RemoveAt(aL->Count - 1);
Update();
carsCount--;
}
}
}
catch (Exception ^ex)
{
}
}	  
};

/*ref class InterfaceForm : Form
{
protected: User ^_user;

InterfaceForm(User ^user)
{
_user = nullptr;
_user = user;
this->DoubleBuffered = true;
}

virtual void OnPaint(PaintEventArgs ^e) override
{
if (_user == nullptr)
return;

int rzm = 10;
int count = _user->aL->Count;
for (int i = 0; i < count; i++)
{
Car ^car = safe_cast<Car^>(_user->aL[i]);
if (car->eat)
{
e->Graphics->DrawString("На заправке",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 130));

}
e->Graphics->DrawString("Машина №" + i,
gcnew System::Drawing::Font("Arial", 10),
gcnew SolidBrush(Color::Red),
Point(rzm, 100));

if (_user->run)
{
e->Graphics->DrawString("Машина запущена",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 60));
rzm = rzm + 130;
}
else
{
e->Graphics->DrawString("Машина остановлена",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 60));
rzm = rzm + 130;
}
}
}
};*/
ref class InterfaceForm : Form
{
protected: User ^user;

InterfaceForm(User ^user)
{
this->user = user;
this->DoubleBuffered = true;
}

virtual void OnPaint(PaintEventArgs ^e) override
{
if (user == nullptr)
return;

int rzm = 10;
int count = user->aL->Count;
for (int i = 0; i < count; i++)
{
Car ^car = safe_cast<Car^>(user->aL[i]);
if (car->eat)
{
e->Graphics->DrawString("На заправке",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 130));
}
e->Graphics->DrawString("Машина №" + i,
gcnew System::Drawing::Font("Arial", 10),
gcnew SolidBrush(Color::Red),
Point(rzm, 100));

if (user->run)
{
e->Graphics->DrawString("Машина запущена",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 60));
rzm = rzm + 130;
}
else
{
e->Graphics->DrawString("Машина остановлена",
gcnew System::Drawing::Font("Arial", 8),
gcnew SolidBrush(Color::Red),
Point(rzm, 60));
rzm = rzm + 130;
}
}
}
};

void main() //самая главная функция 
{
Cars ^aL = gcnew Cars();
Application::Run(gcnew User());
}
Пытаюсь создать объект окна состояний(InterfaceForm) в конструкторе главного объекта(User),но не компилируется.

C++:
ref class User : Cars				 // Класс пользователя машин
{
public: bool run;
public: 
Form ^pChildForm, ^Interface;
Button ^pBut, ^pBut2, ^pBut3;
TextBox ^Text0;
InterfaceForm ^interfaceForm;
private: Refill ^refill;
Random ^rand;

public: User()
{
interfaceForm = gcnew InterfaceForm(this);