Вот исправленный код с полной инициализацией компонентов интерфейса:
#pragma once
namespace graphicRedactor {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
public ref class MyForm : public System::Windows::Forms::Form
{
private:
Bitmap^ drawArea;
Graphics^ graphics;
bool isDrawing;
Point previousPoint;
Color currentColor;
String^ currentTool;
Point startPoint;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ newToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ openToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ saveToolStripMenuItem;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Panel^ toolPanel;
private: System::Windows::Forms::Button^ btnPen;
private: System::Windows::Forms::Button^ btnLine;
private: System::Windows::Forms::Button^ btnRectangle;
private: System::Windows::Forms::Button^ btnEllipse;
private: System::Windows::Forms::Button^ btnColor;
private: System::Windows::Forms::ColorDialog^ colorDialog1;
private: System::ComponentModel::Container ^components;
public:
MyForm(void)
{
InitializeComponent();
InitializeDrawArea();
isDrawing = false;
currentColor = Color::Black;
currentTool = "pen";
}
protected:
~MyForm()
{
if (components)
{
delete components;
}
}
private:
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->ClientSize = System::Drawing::Size(800, 600);
// Инициализация MenuStrip
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->newToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->saveToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
this->fileToolStripMenuItem->Text = L"File";
this->newToolStripMenuItem->Text = L"New";
this->openToolStripMenuItem->Text = L"Open";
this->saveToolStripMenuItem->Text = L"Save";
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(3) {
this->newToolStripMenuItem,
this->openToolStripMenuItem,
this->saveToolStripMenuItem
});
this->menuStrip1->Items->Add(this->fileToolStripMenuItem);
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Size = System::Drawing::Size(800, 24);
// Привязка обработчиков событий для меню
this->newToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::newToolStripMenuItem_Click);
this->openToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::openToolStripMenuItem_Click);
this->saveToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::saveToolStripMenuItem_Click);
// Инициализация панели инструментов
this->toolPanel = (gcnew System::Windows::Forms::Panel());
this->toolPanel->Location = System::Drawing::Point(0, 24);
this->toolPanel->Size = System::Drawing::Size(100, 576);
this->toolPanel->BackColor = System::Drawing::Color::LightGray;
// Инициализация кнопок
this->btnPen = (gcnew System::Windows::Forms::Button());
this->btnLine = (gcnew System::Windows::Forms::Button());
this->btnRectangle = (gcnew System::Windows::Forms::Button());
this->btnEllipse = (gcnew System::Windows::Forms::Button());
this->btnColor = (gcnew System::Windows::Forms::Button());
// Настройка кнопок
array<Button^>^ buttons = { btnPen, btnLine, btnRectangle, btnEllipse, btnColor };
array<String^>^ buttonTexts = { L"Pen", L"Line", L"Rectangle", L"Ellipse", L"Color" };
for (int i = 0; i < buttons->Length; i++) {
buttons[i]->Location = System::Drawing::Point(10, 10 + i * 40);
buttons[i]->Size = System::Drawing::Size(80, 30);
buttons[i]->Text = buttonTexts[i];
this->toolPanel->Controls->Add(buttons[i]);
}
// Привязка обработчиков событий для кнопок
this->btnPen->Click += gcnew System::EventHandler(this, &MyForm::btnPen_Click);
this->btnLine->Click += gcnew System::EventHandler(this, &MyForm::btnLine_Click);
this->btnRectangle->Click += gcnew System::EventHandler(this, &MyForm::btnRectangle_Click);
this->btnEllipse->Click += gcnew System::EventHandler(this, &MyForm::btnEllipse_Click);
this->btnColor->Click += gcnew System::EventHandler(this, &MyForm::btnColor_Click);
// Инициализация PictureBox
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox1->Location = System::Drawing::Point(100, 24);
this->pictureBox1->Size = System::Drawing::Size(700, 576);
this->pictureBox1->BackColor = System::Drawing::Color::White;
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
// Привязка обработчиков событий для PictureBox
this->pictureBox1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseDown);
this->pictureBox1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseMove);
this->pictureBox1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseUp);
// Инициализация ColorDialog
this->colorDialog1 = (gcnew System::Windows::Forms::ColorDialog());
// Добавление элементов управления на форму
this->Controls->Add(this->menuStrip1);
this->Controls->Add(this->toolPanel);
this->Controls->Add(this->pictureBox1);
this->Text = L"Graphic Editor";
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
// Остальные методы остаются без изменений
void InitializeDrawArea()
{
drawArea = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
pictureBox1->Image = drawArea;
graphics = Graphics::FromImage(drawArea);
graphics->Clear(Color::White);
}
// Здесь идут все остальные методы (обработчики событий) из предыдущего кода
// newToolStripMenuItem_Click, openToolStripMenuItem_Click, saveToolStripMenuItem_Click,
// pictureBox1_MouseDown, pictureBox1_MouseMove, pictureBox1_MouseUp,
// btnPen_Click, btnLine_Click, btnRectangle_Click, btnEllipse_Click, btnColor_Click
// и GetRectangle - без изменений
};
}