0

我试图让我的用户交换两个 DevExpress 图表控件(尽管我相信几乎任何控件都应该工作......),方法是将一个拖到另一个之上。我已经为我的 TabControl 完成了此操作(以允许交换/移动选项卡),但由于某种原因,我似乎在这里遗漏了一些东西,这阻止了我对 ChartControl 做同样的事情。

它“应该”在图表控件上绘制一个灰色框,并允许用户将其拖动到他们喜欢的任何地方,但我只是得到一个带有条纹的黑色圆圈。

这是我到目前为止编写的代码,希望你们中的一个人能够发现错误,我可以停止拉扯我的头发!:)

private void ChartControlMouseMove(object sender, MouseEventArgs e)
{
    // Handle Mouse move only if left button is pressed.
    if (e.Button == MouseButtons.Left)
    {
        var chartControl = (ChartControl)sender;

        // If the mouse moves outside the rectangle, start the drag.
        if (!rectDragBoxFromMouseDown.Equals(Rectangle.Empty)
            & !rectDragBoxFromMouseDown.Contains(e.X, e.Y))
        {
            Invalidate();
            DoDragDrop(chartControl, DragDropEffects.Move);
            CalcRectDragBox(e.X, e.Y);
            Invalidate();
        }
    }
}

private void ChartControlMouseDown(object sender, MouseEventArgs e)
{
    CalcRectDragBox(e.X, e.Y);
}

private void CalcRectDragBox(int x, int y)
{
    // Remember the point where the mouse down occurred. The DragSize indicates
    // the size that the mouse can move before a drag event should be started.
    var dragSize = SystemInformation.DragSize;
    // Create a rectangle using the DragSize, with the mouse position being
    // at the center of the rectangle.
    rectDragBoxFromMouseDown = new Rectangle(
        new Point(x - (dragSize.Width/2), y - (dragSize.Height/2)), dragSize);
}

private void ChartControlDragOver(object sender, DragEventArgs e)
{
    var chartControl = (ChartControl)sender;

    // get the control we are hovering over.
    var hitInformation = chartControl.CalcHitInfo(chartControl.PointToClient(new Point(e.X, e.Y)));
    if ((hitInformation != null))
    {
        //ChartHitInfo hoverTab = hitInformation;

        if (e.Data.GetDataPresent(typeof(ChartControl)))
        {
            e.Effect = DragDropEffects.Move;
            var dragTab = (ChartControl)e.Data.GetData(typeof(ChartControl));

            if (dragTab != chartControl)
            {
                for (int i = 0; i < layoutControlGroupDashboard.Items.Count; i++)
                {
                    var layoutControlItem = layoutControlGroupDashboard.Items[i] as LayoutControlItem;
                    if (layoutControlItem != null && layoutControlItem.Control == chartControl)
                    {
                        for (int j = 0; j < layoutControlGroupDashboard.Items.Count; j++)
                        {
                            var controlItem = layoutControlGroupDashboard.Items[j] as LayoutControlItem;
                            if (controlItem != null && controlItem.Control == dragTab)
                            {
                                if (!_ignoreNextDrag)
                                {
                                    _ignoreNextDrag = true;
                                    layoutControlGroupDashboard.BeginInit();
                                    var layoutControlItemi = layoutControlGroupDashboard.Items[i] as LayoutControlItem;
                                    if (layoutControlItemi != null)
                                    {
                                        Control tempControlI =
                                            layoutControlItemi.Control;
                                        var layoutControlItemj = layoutControlGroupDashboard.Items[j] as LayoutControlItem;
                                        if (layoutControlItemj != null)
                                        {
                                            layoutControlItemi.BeginInit();
                                            layoutControlItemj.BeginInit();
                                            Control tempControlJ =
                                                layoutControlItemj.Control;


                                            layoutControlItemi.Control =
                                                null;
                                            layoutControlItemj.Control =
                                                null;
                                            layoutControlItemi.Control =
                                                tempControlJ;
                                            layoutControlItemj.Control =
                                                    tempControlI;
                                            layoutControlItemi.EndInit();
                                            layoutControlItemj.EndInit();
                                        }
                                    }

                                    layoutControlGroupDashboard.EndInit();
                                    break;
                                }
                                else
                                {
                                    _ignoreNextDrag = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

同样,这个想法是允许用户交换控件只需单击单击拖动周围的东西......希望这只是我错过的一些简单的东西,但我一辈子都看不到它!

编辑:这是我尝试过的(首先将我的图表添加到面板......)

            Panel panel = new Panel();
            panel.Name = Guid.NewGuid().ToString();
            panel.Controls.Add(chartControl);

            var dashboardItem = new LayoutControlItem(layoutControlDashboard, panel)
                                    {
                                        Padding = new DevExpress.XtraLayout.Utils.Padding(0),
                                        Spacing = new DevExpress.XtraLayout.Utils.Padding(0),
                                        SizeConstraintsType = SizeConstraintsType.Custom
                                    };
4

1 回答 1

1

这是修改后的 ChartControlDragOver 方法,在将 ChartControl 放置在 LayoutControl 中时有效:

private void ChartControlDragOver(object sender, DragEventArgs e) {
var chartControl = (ChartControl)sender;

// get the control we are hovering over.
var hitInformation = chartControl.CalcHitInfo(chartControl.PointToClient(new Point(e.X, e.Y)));
if ((hitInformation != null)) {
    //ChartHitInfo hoverTab = hitInformation;

    if (e.Data.GetDataPresent(typeof(ChartControl))) {
        e.Effect = DragDropEffects.Move;
        var dragTab = (ChartControl)e.Data.GetData(typeof(ChartControl));
        if (dragTab == chartControl) return;
        InsertType insertType = InsertType.Left;
        Point hitPoint = chartControl.Parent.PointToClient(new Point(e.X, e.Y));
        if (dragTab.Bounds.Left < hitPoint.X && dragTab.Bounds.Right > hitPoint.X) {
            if (dragTab.Bounds.Top > hitPoint.Y)
                insertType = InsertType.Top;
            else if (dragTab.Bounds.Bottom < hitPoint.Y)
                insertType = InsertType.Bottom;
        } else if (dragTab.Bounds.Right < hitPoint.X)
            insertType = InsertType.Right;
        else if (dragTab.Bounds.Left > hitPoint.X)
            insertType = InsertType.Left;
        LayoutControl layout = (LayoutControl)chartControl.Parent;
        layout.GetItemByControl(dragTab).Move(layout.GetItemByControl(chartControl), insertType);
    }
} else {
    e.Effect = DragDropEffects.None;
}

}

于 2012-06-23T11:02:33.667 回答