试图在现有面板上绘制形状已有一段时间,但目前尚无主意。有人可以帮我吗?它最终始终位于面板后面(和pictureBox /灰色之一/)。我尝试了3种不同的方法,但都没有成功。这是我的代码:
namespace DrawingOnFront
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel11_MouseClick(object sender, MouseEventArgs e)
{
DrawIt(90, 70);
}
private void DrawIt(int x, int y)
{
Rectangle Circle = new Rectangle(x,y,40,40);
SolidBrush Red = new SolidBrush(Color.Red);
Graphics g = this.CreateGraphics();
g.FillEllipse(Red, Circle);
/*
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int width = pictureBox1.Width /4;
int height = pictureBox1.Height /2;
int diameter = Math.Min(width, height);
g.FillEllipse(Red, x, y, width, height);
pictureBox1.Image = bmp;
*/
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Graphics g = e.Graphics)
{
Rectangle Circle = ClientRectangle;
Circle.Location = new Point(100, 60);
Circle.Size = new Size(40, 40);
using (SolidBrush Green = new SolidBrush(Color.Green))
{
g.FillEllipse(Green, Circle);
}
}
}
}
}
很抱歉这个基本的喇嘛问题,对于大多数人来说,这很容易,我仍在学习。提前谢谢。
答案 0 :(得分:1)
我上面的评论适用。这是一个如何在每个控件上绘制和分别的形式的示例:
我们最好有一个通用的绘制例程,可以从每个参与元素的Paint
事件中调用它,在我们的例子中是Panel
,PictureBox
和Form
诀窍是让所有嵌套元素绘制按其自身位置平移的圆。为此,我们将这些内容传递给绘图例程:
Graphics
对象。我们是从Paint
事件中获取的。Graphics.TranslateTransform
..:结果:
您可以看到外观,就好像我们在所有元素上绘制了一个圆圈一样,但实际上我们在每个元素上绘制了三个圆圈..:
private void canvasForm_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void draw(Control ctl, Graphics g)
{
Rectangle r = new Rectangle(200, 100, 75, 75);
if (ctl != canvasForm) g.TranslateTransform(-ctl.Left, -ctl.Top);
g.FillEllipse(Brushes.Green, r);
g.ResetTransform();
}
请注意,可以通过三个调用来创建相同的结果,一个FillRectangle
,一个DrawImage
和一个FillEllipse
:-)