我想在某个面板上/中绘制多个填充椭圆。绘制单个不是问题,我正在使用:
Color aColor = Color::FromArgb( 255, 0, 0 );
SolidBrush^ aBrush = gcnew SolidBrush(aColor);
Rectangle rect = Rectangle(x, y, 10, 10);
e->Graphics->FillEllipse(aBrush, rect);
它绘制了一个由矩形框起来的红色椭圆,并用红色填充。 (假设我将给出 x
和 y
)。我遇到的问题是,当我想在随机位置绘制多个这样的椭圆时。所以我需要传递随机 x
和 y
(使用 rand()
% somenumber)但我不确定,我如何传递这些变量进入 panel1_paint
函数并在两个数字随机化后绘制它们。另外,我不希望最后一个椭圆在绘制新椭圆时消失。唯一的方法是使用全局变量?
有什么想法吗?
好吧,我按照建议尝试在面板内使用循环,我明白了:
for(int i=0; i<ile_przeszkod; i++){
int x = rand() % 690; int y = rand() % 690;
Color aColor = Color::FromArgb( 255, 0, 0 );
SolidBrush^ aBrush = gcnew SolidBrush(aColor);
Rectangle rect = Rectangle(x, y, 10, 10);
e->Graphics->FillEllipse(aBrush, rect);
MessageBox::Show("x: "+x+ " y: " +y);
}
ile_przeszkod 表示我想绘制多少个,消息框会告诉我它随机化了哪些数字,所以我确信省略号不会重叠。问题是,在“使”面板 1“无效”之后,我只看到 1 个椭圆。 :/我应该怎么做才能同时看到它们?
最佳答案
所有的 x, y
坐标都是随机的,所以它们不依赖于其他一些决定程序,所以不需要传递给 panel1_paint
而你可以运行 lpop 并生成随机数以将它们用作您的 x
、y
坐标。
关于C++/CLI Visual C++ 2010 Express - 绘制多个椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8466314/