在运行时在面板内放置按钮

时间:2016-06-27 13:37:39

标签: winforms

我正在开发一个窗口应用程序。我在表格和表格上有一个静态面板一些菜单,如面板enter image description here按钮等。 在运行时我已经在静态面板上拖动了一个面板&在此面板中添加一个按钮。现在,我想在此按钮外面绘制一个边框,但是绘制边框时,边框从静态面板获取其位置,而不是在运行时创建的面板。

请给我一个解决方案。为了这 。 我的绘制边框代码是:

 private void sDrawControlBorder(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//
        g = control.Parent.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();

    }

0 个答案:

没有答案