C# 面板移动和调整对象大小

C# Panel move and resize object

我在面板中绘制时遇到问题。

我写了一段代码来移动面板中的对象并调整其大小, 但是当我移动或调整对象大小时,您可以看到面板发生了什么。

这是我的代码:

    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

这是因为你永远不会删除你画的东西。 在重新绘制新的手柄或移动对象或任何您想删除旧手柄的条件之前,请尝试此

panelView.Invalidate();

这个函数,MSDN says,"Invalidates the entire surface of the control and causes the control to be redrawn."

如果你想做非持久绘图(这是你在代码中所做的)你需要调用graphPanel.Refresh() 之前 你画画。

如果你想做持久绘图,你需要在Paint事件中使用它的e.Grpahics参数来做。

非持久绘图适用于橡皮筋显示矩形或跟随鼠标的直线。

它会随时消失,例如当 minimizing/maximizing 表格。因此,如果您需要最终状态持续存在,您可能需要在 MouseUp 事件中通过调用 Paint 事件来跟进它(通过执行 Invalidate() 并将其绘制在那里......