我在面板中绘制时遇到问题。
我编写了一个代码来移动和调整面板中的对象大小, 但是当我移动或调整对象大小时,您可以看到面板会发生什么。
这是我的代码:
private void DrawControlBorder(object sender)
{
Control control = (Control)sender;
//define the border to be drawn, it will be offset by DRAG_HANDLE_SIZE / 2
//around the control, so when the drag handles are drawn they will be seem
//connected in the middle.
Rectangle Border = new Rectangle(
new Point(control.Location.X - DRAG_HANDLE_SIZE / 2,
control.Location.Y - DRAG_HANDLE_SIZE / 2),
new Size(control.Size.Width + DRAG_HANDLE_SIZE,
control.Size.Height + DRAG_HANDLE_SIZE));
//define the 8 drag handles, that has the size of DRAG_HANDLE_SIZE
Rectangle NW = new Rectangle(
new Point(control.Location.X - DRAG_HANDLE_SIZE,
control.Location.Y - DRAG_HANDLE_SIZE),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle N = new Rectangle(
new Point(control.Location.X + control.Width / 2 - DRAG_HANDLE_SIZE / 2,
control.Location.Y - DRAG_HANDLE_SIZE),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle NE = new Rectangle(
new Point(control.Location.X + control.Width,
control.Location.Y - DRAG_HANDLE_SIZE),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle W = new Rectangle(
new Point(control.Location.X - DRAG_HANDLE_SIZE,
control.Location.Y + control.Height / 2 - DRAG_HANDLE_SIZE / 2),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle E = new Rectangle(
new Point(control.Location.X + control.Width,
control.Location.Y + control.Height / 2 - DRAG_HANDLE_SIZE / 2),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle SW = new Rectangle(
new Point(control.Location.X - DRAG_HANDLE_SIZE,
control.Location.Y + control.Height),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle S = new Rectangle(
new Point(control.Location.X + control.Width / 2 - DRAG_HANDLE_SIZE / 2,
control.Location.Y + control.Height),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
Rectangle SE = new Rectangle(
new Point(control.Location.X + control.Width,
control.Location.Y + control.Height),
new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
//get the form graphic
Graphics g = panelView.CreateGraphics();
//draw the border and drag handles
ControlPaint.DrawBorder(g, Border, Color.Gray, ButtonBorderStyle.Dotted);
ControlPaint.DrawGrabHandle(g, NW, true, true);
ControlPaint.DrawGrabHandle(g, N, true, true);
ControlPaint.DrawGrabHandle(g, NE, true, true);
ControlPaint.DrawGrabHandle(g, W, true, true);
ControlPaint.DrawGrabHandle(g, E, true, true);
ControlPaint.DrawGrabHandle(g, SW, true, true);
ControlPaint.DrawGrabHandle(g, S, true, true);
ControlPaint.DrawGrabHandle(g, SE, true, true);
g.Dispose();
//graphPanel.Dispose();
//bitmap.Dispose();
}
任何人都可以帮助我?
TNX
答案 0 :(得分:0)
这是因为你永远不会删除你画的内容。 在重新绘制新的抓取手柄或移动物体或任何您想要移除旧手柄的条件之前,请尝试使用
panelView.Invalidate();
此函数MSDN says,“使控件的整个表面无效并导致重绘控件。”
答案 1 :(得分:0)
如果您想要非持久性绘图(这是您在代码中执行的操作),则需要在之前调用graphPanel.Refresh()
画画。
如果您想要持续绘图,则需要使用Paint
参数在e.Grpahics
事件中执行此操作。
非持久性绘图适用于矩形的橡皮筋显示或鼠标后面的线条。
它会在任何时候消失,例如当最小化/最大化形式时。因此,如果您需要最终状态持续存在,则可能需要在MouseUp
事件中跟进,并调用Paint
事件(通过执行Invalidate()
并在那里绘制它...