[英]Control not drawn on panel
我正在尝试向面板添加控件。 在面板上的mouseDown下,该点被保存,而在mouseUp上,该点被保存。 但是在面板mouseUp上没有绘制任何内容。 怎么解决呢?
椭圆类:
class Ellipse : Control
{
private int x;
private int y;
private int width;
private int height;
public Ellipse(int x, int y, int width, int height)
{
setY(y);
setX(x);
setWidth(width);
setHeight(height);
}
public int getX() { return x;}
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public void setX(int newx) { x = newx; }
public void setY(int newy) { y = newy; }
public void setWidth(int newwidth) { width = newwidth; }
public void setHeight(int newheight) { height = newheight; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Call methods of the System.Drawing.Graphics object.
// Declare and instantiate a new pen.
System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua);
// Draw an aqua rectangle in the rectangle represented by the control.
e.Graphics.FillEllipse(Brushes.Black,x,y,width,height);
}
}
Form1班
private void panel_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
x = e.X;
y = e.Y;
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
xe = e.X;
ye = e.Y;
Item item;
Enum.TryParse<Item>(menuComboBoxShape.ComboBox.SelectedValue.ToString(), out item);
switch (item)
{
case Item.Pencil:
using (Graphics g = panel.CreateGraphics())
using (var pen = new Pen(System.Drawing.Color.Black)) //Create the pen used to draw the line (using statement makes sure the pen is disposed)
{
g.DrawLine(pen,new Point(x, y), new Point(xe, ye));
}
break;
case Item.Rectangle:
break;
case Item.Ellipse:
Ellipse el = new Ellipse(x,y,xe-x,ye-y);
panel.Controls.Add(el);
break;
default:
break;
}
}
您正在从Control继承Ellipse
类,但实际上您没有将其用作控件-您没有将其添加到Controls
集合中,因此实际上它是不可见的,不活动的,并且没有从表单接收任何事件。
从外部代码绘制控件看起来也很糟糕。 控件应自行绘制,并且应从外部代码设置其边界。
以下是可帮助您正确选择的代码段:
class Ellipse : Control
{
Point mDown { get; set; }
public Ellipse()
{
MouseDown += shape_MouseDown;
MouseMove += shape_MouseMove;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillEllipse(Brushes.Black, this.Bounds);
}
private void shape_MouseDown(object sender, MouseEventArgs e)
{
mDown = e.Location;
}
private void shape_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Location = new Point(e.X + Left - mDown.X, e.Y + Top - mDown.Y);
}
}
}
并以以下形式创建它:
el = new Ellipse();
el.Bounds = new Rectangle(0, 0, 100, 100);
Controls.Add(el);
更新资料
根据您更新的代码,我可以看到几个问题:
实际上,您不需要Ellipse
类的x, y, width, height
属性以及相应的getter / setter方法,因为它是Control
,并且具有自己的Location
和Width
, Height
公共属性。
您绘制椭圆不正确。 假设它应填满所有区域,则绘画应为e.Graphics.FillEllipse(Brushes.Black,0,0,Width,Height)
(此处我假设使用Control.Width
而不是您的width
,依此类推)。 否则,您将另外移动绘制的椭圆。
panel_MouseUp
有关椭圆创建的代码应类似于
var el = new Ellipse();
panel.Controls.Add(el);
el.Location = new Point(x, y);
el.Width = (xe - x);
el.Height = (ye - y);
或者,如果它应该是一个椭圆(现在每次都在创建一个新椭圆)-在mouseUp处理程序外部和处理程序内部创建一个椭圆形,只需更改其大小和位置即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.