C++在picturebox上用鼠标点击绘制东西

问题描述:

我有两个picurebox函数。我想用picturebox上的鼠标点击绘制一些东西。C++在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); 
} 

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

+0

这不是C#代码,请尽量避免放置多个编程语言标记。 –

+0

@SpencerWieczorek它看起来像使用.NET扩展的C++,使它成为一个奇怪的鸭子。 –

+0

@MarkRansom它是,.NET标签需要在那里。 –

在已定义的图片框代码的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); 
} 

调整椭圆的宽度和高度,每个椭圆的宽度和高度可以根据您的选择进行调整。