c ++在图片框上用鼠标点击绘制内容

时间:2017-10-18 18:05:42

标签: winforms c++-cli

我有两个picurebox的功能。我想在图片框上用鼠标点击画一些东西。

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);
}

我想在另一个函数中使用一个函数。

1 个答案:

答案 0 :(得分:2)

在代码的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个。