c ++用鼠标在图片框上单击绘制一些东西

c++ draw something with mouseclick on picturebox

我有两个图片框功能。我想用鼠标点击图片框画点东西

private: System::Void pictureBox1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
        int Curx = e->X;
        int Cury = e->Y;
}

private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
            e->Graphics->DrawEllipse(Pens::Blue, 200,200, 1, 1);
}

我想在另一个中使用一个功能。

在定义了图片框的代码的 private 部分中,为位置添加两个变量 x 和 y,如下所示:

private: System::Windows::Forms::PictureBox^ pictureBox1;
int mousex;
int mousey;

设置您的 MouseClick 事件以将坐标保存到这些变量并通过调用 Refresh():

强制重绘
private: System::Void pictureBox1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
{
    mousex = e->X;
    mousey = e->Y;
    pictureBox1->Refresh();
}

Paint 事件中,在 mousexmousey 中保存的坐标处绘制椭圆:

private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
{
   e->Graphics->DrawEllipse(Pens::Blue, mousex, mousey, 60, 60);
}

调整椭圆的宽度和高度,目前各为 60,供您选择。