📄 form1.h
字号:
private: System::Void rectangleToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
elementType = ElementType::RECTANGLE; }
private: System::Void circleToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
elementType = ElementType::CIRCLE; }
private: System::Void curveToolStripMenuItem_DoubleClick(System::Object^ sender, System::EventArgs^ e) {
elementType = ElementType::CURVE; }
private: System::Void blackToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
color = Color::Black; }
private: System::Void redToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
color = Color::Red; }
private: System::Void greenToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
color = Color::Green; }
private: System::Void blueToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
color = Color::Blue; }
private: System::Void colorToolStripMenuItem_DropDownOpening(System::Object^ sender, System::EventArgs^ e) {
blackToolStripMenuItem->Checked = color == Color::Black;
redToolStripMenuItem->Checked = color == Color::Red;
greenToolStripMenuItem->Checked = color == Color::Green;
blueToolStripMenuItem->Checked = color == Color::Blue;
}
private: System::Void elementToolStripMenuItem_DropDownOpening(System::Object^ sender, System::EventArgs^ e) {
lineToolStripMenuItem->Checked = elementType == ElementType::LINE;
rectangleToolStripMenuItem->Checked = elementType == ElementType::RECTANGLE;
circleToolStripMenuItem->Checked = elementType == ElementType::CIRCLE;
curveToolStripMenuItem->Checked = elementType == ElementType::CURVE;
}
// Current element type
ElementType elementType;
// Current drawing color
Color color;
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
Graphics^ g = e->Graphics;
sketch->Draw(g);
if(tempElement)
tempElement->Draw(g);
}
// Records when drawing an element is in progress
bool drawing;
// Records the initial mouse cursor position
Point firstPoint;
private: System::Void Form1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
if(e->Button == System::Windows::Forms::MouseButtons::Left)
{
if(mode == Mode::Normal)
drawing = true;
firstPoint = e->Location;
if(elementType == ElementType::TEXT && mode == Mode::Normal)
{
textDialog->TextString = L""; // Reset the text box string
if(textDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
text = textDialog->TextString;
tempElement = gcnew TextElement(color, firstPoint, text, textFont);
sketch->Add(tempElement);
Invalidate(tempElement->Bound); // The text element region
tempElement = nullptr;
Update();
}
drawing = false;
}
}
}
public:
TextDialog^ textDialog;
private: System::Void Form1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
if(drawing)
{
if(tempElement)
Invalidate(tempElement->Bound); // The old element region
switch(elementType)
{
case ElementType::LINE:
tempElement = gcnew Line(color, firstPoint, e->Location, penWidth);
break;
case ElementType::RECTANGLE:
tempElement = gcnew Rectangle(color, firstPoint, e->Location, penWidth);
break;
case ElementType::CIRCLE:
tempElement = gcnew Circle(color, firstPoint, e->Location, penWidth);
break;
case ElementType::CURVE:
if(tempElement)
safe_cast<Curve^>(tempElement)->Add(e->Location);
else
tempElement = gcnew Curve(color, firstPoint, e->Location, penWidth);
break;
}
Invalidate(tempElement->Bound); // The new element region
Update();
}
else if(mode == Mode::Normal)
{
Element^ element = sketch->HitElement(e->Location);
if(highlightedElement == element)
return;
if(highlightedElement)
{
Invalidate(highlightedElement->Bound);
highlightedElement->highlighted = false;
highlightedElement = nullptr;
}
if(element)
{
highlightedElement = element;
highlightedElement->highlighted = true;
Invalidate(highlightedElement->Bound);
}
Update();
}
else if(mode == Mode::Move && e->Button == System::Windows::Forms::MouseButtons::Left)
{ // Move the highlighted element
if(highlightedElement)
{
Invalidate(highlightedElement->Bound); // Region before move
highlightedElement->Move(e->X - firstPoint.X, e->Y - firstPoint.Y);
firstPoint = e->Location;
Invalidate(highlightedElement->Bound); // Region after move
Update();
}
}
}
private: System::Void Form1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
if(!drawing)
{
mode = Mode::Normal;
return;
}
if(tempElement)
{
sketch->Add(tempElement);
tempElement = nullptr;
Invalidate();
}
drawing = false;
}
// Temporary store for the element being drawn
Element^ tempElement;
// Stores the current sketch
Sketch^ sketch;
protected:
// The currently highlighted element
Element^ highlightedElement;
private: System::Void contextMenuStrip1_Opening(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
contextMenuStrip1->Items->Clear(); // Remove existing items
if(highlightedElement)
{
contextMenuStrip1->Items->Add(moveContextMenuItem);
contextMenuStrip1->Items->Add(deleteContextMenuItem);
contextMenuStrip1->Items->Add(sendToBackContextMenuItem);
}
else
{
contextMenuStrip1->Items->Add(lineContextMenuItem);
contextMenuStrip1->Items->Add(rectangleContextMenuItem);
contextMenuStrip1->Items->Add(circleContextMenuItem);
contextMenuStrip1->Items->Add(curveContextMenuItem);
contextMenuStrip1->Items->Add(textContextMenuItem);
contextMenuStrip1->Items->Add(contextSeparator);
contextMenuStrip1->Items->Add(blackContextMenuItem);
contextMenuStrip1->Items->Add(redContextMenuItem);
contextMenuStrip1->Items->Add(greenContextMenuItem);
contextMenuStrip1->Items->Add(blueContextMenuItem);
// Set checks for the menu items
blackContextMenuItem->Checked = color == Color::Black;
redContextMenuItem->Checked = color == Color::Red;
greenContextMenuItem->Checked = color == Color::Green;
blueContextMenuItem->Checked = color == Color::Blue;
lineContextMenuItem->Checked = elementType == ElementType::LINE;
rectangleContextMenuItem->Checked = elementType == ElementType::RECTANGLE;
circleContextMenuItem->Checked = elementType == ElementType::CIRCLE;
curveContextMenuItem->Checked = elementType == ElementType::CURVE;
textContextMenuItem->Checked = elementType == ElementType::TEXT;
}
}
private: System::Void deleteContextMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
if(highlightedElement)
{
sketch->Delete(highlightedElement);
Invalidate(highlightedElement->Bound);
highlightedElement = nullptr;
Update();
}
}
private: System::Void sendToBackContextMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
if(highlightedElement)
{
sketch->Add(sketch->Delete(highlightedElement));
highlightedElement->highlighted = false;
Invalidate(highlightedElement->Bound);
highlightedElement = nullptr;
Update();
}
}
private: System::Void moveContextMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
mode = Mode::Move;
}
Mode mode;
private: System::Void penWidthButton_Click(System::Object^ sender, System::EventArgs^ e) {
if(penDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
penWidthComboBox->SelectedIndex = safe_cast<int>(penDialog->PenWidth - 1.0f);
penWidthComboBox->Invalidate();
}
}
PenDialog^ penDialog;
// Current pen width
float penWidth;
private: System::Void penWidthComboBox_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
penWidth = safe_cast<float>(penWidthComboBox->SelectedIndex + 1);
penDialog->PenWidth = penWidth;
}
private: System::Void textToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
elementType = ElementType::TEXT;
}
// The current font for text elements
System::Drawing::Font^ textFont;
private: System::Void fontToolStripButton_Click(System::Object^ sender, System::EventArgs^ e) {
if(fontDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
textFont = fontDialog1->Font;
}
String^ text;
BinaryFormatter^ formatter;
private: System::Void saveToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Stream^ stream;
if(!sketch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -