设计时拖放

发布于 2024-08-05 07:04:42 字数 2369 浏览 3 评论 0原文

我目前正在开发一个项目,我的部分是设计一个设计界面,最终用户可以在运行时添加/删除/移动控件。

我遵循了本教程 “托管 Windows 窗体设计器,作者:Tim Dawson”,几乎已经实现了我需要的所有功能。

为那些不想阅读教程的人提供的简短故事:我实现了IDesignerHost, IContainerISelectionServiceIToolboxService 和其他一些接口,用于创建我的设计界面。我没有使用 System.ComponentModel.Design.DesignSurface 已经在框架中,主要是因为我需要一个真正自定义的设计界面。

问题:

我想允许用户拖动&将新的ControlIToolboxService 拖放到IDesignerHost/IContainer。在本教程中,您将单击工具箱中的Control,然后单击设计图面以添加该控件。

我发现:

  • 有一个内置功能 自动进行拖拽操作掉落自 IToolboxServiceSystem.ComponentModel.Design.DesignSurface 但如果您从无到有实现 IDesignerHost,它显然不起作用。
  • 当您使用 Control.DoDragDrop(ToolboxItem) 方法,启动拖放掉落:

    1. IToolboxService.SerializeToolboxItem(ToolboxItem) 被调用来序列化该项目
    2. IToolboxService.IsToolboxItem(对象)
      IToolboxService.IsSupported(对象) 被调用来评估是否
      可以允许序列化 ToolboxItem 放置在设计表面上
    3. 当您放下控件时:设计界面会调用 IToolboxService.DeserializeToolboxItem(object serializedObject) 来反序列化所放下的控件。
  • IToolboxService.SetCursor() 被调用以了解您是否定义自定义光标,还是让标准 Windows 光标。

问题:

我在“我发现的内容”中实现了上面提到的所有内容,但是拖动& drop is buggy :

  • 我拖动,一切都很好,但是当 悬停在设计表面上,我的 光标在之间稍微闪烁 标准光标和 “DragDropEffects.Copy”样式。
  • 当我放下时,什么也没有发生,当我的光标离开设计表面时(放下后),就会创建新的 Control 并将其添加到我放下它的位置。

有没有人尝试过做我正在做的事情,如果是的话,你是如何做到的? 有人有任何指针/链接/好的建议吗?

谢谢=)

I'm currently working of a project where my part is to design a Design Surface where the end user can add/remove/move controls at run-time.

I followed this tutorial "Hosting Windows Forms Designers, by Tim Dawson", and almost have implemented all the features I need.

Short story for those who don't want to read the tutorial : I implemented IDesignerHost, IContainer, ISelectionService, IToolboxService and some other interface, to create my design surface. I didn't use the System.ComponentModel.Design.DesignSurface already in the framework, mainly beacause I need a really custom design surface.

Question :

I want to allow user to drag & drop new Control from the IToolboxService to the IDesignerHost/IContainer. In this tutorial, you clic on a Control in the toolbox, the click on the design surface to add the control.

What i've found :

  • There is a built-in feature that
    automagically does drag & drop from
    IToolboxService to
    System.ComponentModel.Design.DesignSurface but it is clearly not working if you implement IDesignerHost from nothing.
  • When you use the
    Control.DoDragDrop(ToolboxItem)
    method, to initiate a drag & drop :

    1. IToolboxService.SerializeToolboxItem(ToolboxItem)
      is called to serialize the item
    2. IToolboxService.IsToolboxItem(object)
      and
      IToolboxService.IsSupported(object)
      are called to evaluate if the
      serialized ToolboxItem can be allowed
      to be droped on the design surface
    3. When you drop the control : IToolboxService.DeserializeToolboxItem(object serializedObject) is called by the design surface to deserialize the controldropped.
  • IToolboxService.SetCursor() is called to know if you define a custom cursor, or let the standard windows cursor.

Problem :

I implemented all mentionned above, in the "What i've found", but the drag & drop is buggy :

  • I drag, everything's fine, but when
    hovering the design surface, my
    cursor blink a little between
    standard cursor and the
    "DragDropEffects.Copy" style.
  • When I drop, nothing happens, and when my cursor leave the design surface (after I dropped), then the new Control is created and added where I dropped it.

Has anyone ever tried to do what I'm doing, and if so, how did you manage it ?
Is there anyone that has any pointer/link/good advices ?

Thank you =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-08-12 07:04:42

有谁有吗
指针/链接/好的建议?

您的问题似乎已通过 Microsoft Connect 问题 消耗/未消耗的鼠标事件得到解决由 2.0 设计师培养。虽然这个问题很容易理解,但显然从 .NET Framework 1.1 (2003) 到 2.0 (2006) 的升级意味着拖拽和渲染方面的变化。删除行为 - 鉴于您提到的文章源于 2003 年,这很可能是相关的。具体来说,您应该向前滚动到第 问题 2 部分,引用:

你现在会注意到常数
拖动时闪烁
形式。这部分是由于
2.0 中新的拖放行为。

我现在无法自己深入研究这个问题,但您也可以从 自定义拖放放入设计器,它解决了有关拖动和拖动的行为变化。 Visual Studio 版本 2003 和 2005 之间下降;这很可能是相关的,并且已接受的答案之一实际上具体引用了上面的 Microsoft Connect 问题,而且它还提供了更多可能解决方案的提示。

Is there anyone that has any
pointer/link/good advices?

Your issue seems to be addressed by the Microsoft Connect issue Mouse Events Consumed / Not Raised in 2.0 Designers. This issue is all but easy to grasp though, but apparently the upgrade from .NET Framework 1.1 (2003) to 2.0 (2006) implied a change in drag & drop behavior - given that the article you mentioned stems from 2003 this could well be related. Specifically you should scroll ahead to section Issue 2, citation:

You will now notice constant
flickering when dragging over the
form. This is partially caused by the
new drag-drop behavior in 2.0.

I'm unable to dig deeper into this right now myself but you might also be able to figure something from Customize Drag & Drop in the Designer, which addresses changed behavior regarding drag & drop between Visual Studio versions 2003 and 2005; quite likely this might be related and one of the accepted answers does in fact specifically reference the Microsoft Connect issue above, plus it offers more hints towards possible solutions too.

温柔戏命师 2024-08-12 07:04:42

我一天前就解决了这个问题。这是我为我的项目(如您的项目)找到的解决方案。

  1. 我拖动,一切都很好,但是当悬停在设计表面时,我的光标在标准光标和“DragDropEffects.Copy”样式之间闪烁了一下。

这是我的工具箱服务。

class EasyAccordionToolboxService : AccordionControl, IToolboxService
{
    EasyAccordionControlElement _SelectedElement;
    public Control DesignPanel { get; set; }

    public EasyAccordionToolboxService()
    {
        AddAccordions();
        AllowItemSelection = true;
        base.ElementClick += EasyAccordionToolboxService_ElementClick;
    }

    private void EasyAccordionToolboxService_ElementClick(object sender, ElementClickEventArgs e)
    {
        if (e.Element.Level == 1) this._SelectedElement = e.Element as EasyAccordionControlElement;
    }

    public ToolboxItem GetSelectedToolboxItem()
    {
        if(_SelectedElement != null)
        {
            var selectedToolboxItem = this._SelectedElement.ToolBoxItem;
            this._SelectedElement = null;
            return selectedToolboxItem;
        }
        else
        {
            return null;
        }
    }

        public bool SetCursor()
    {
        if(this._SelectedElement == null)
        {
            this.DesignPanel.Cursor = Cursors.Default;
        }
        else
        {
            this.DesignPanel.Cursor = Cursors.Hand;
        }

        return true;
    }

有“DesignPanel”属性和“SetCursor”方法。 DesignPanel 属性是根设计器视图的控件。

  1. 当我放下时,什么也没有发生,当我的光标离开设计表面时(在我放下之后),就会创建新的控件并将其添加到我放下它的位置。

EasyAccordionToolBoxService 中有一个从 IToolboxService 实现的 GetSelectedToolboxItem 方法。 GetSelectedToolboxItem 就是您的答案。

最后,

        designer = new EasyDesignSurface(propertyGrid);

        var rootForm = (Form)designer.CreateComponent(typeof(Form));
        rootForm.Name = "rootForm";
        rootForm.Text = "BELGE";
        rootForm.TopLevel = false;
        rootForm.Size = new Size(600, 600);
        designer.ComponentContainer.Add(rootForm);

        var view = (Control)designer.View;
        view.Dock = DockStyle.Fill;
        designPanel.Controls.Add(view);

        easyAccordionToolboxService1.DesignPanel = view;
        designer.AddService(typeof(IToolboxService), easyAccordionToolboxService1);

Me to worked on this questions for one day ago. This is my solution that found for my project like yours.

  1. I drag, everything's fine, but when hovering the design surface, my cursor blink a little between standard cursor and the "DragDropEffects.Copy" style.

This is my toolboxservice.

class EasyAccordionToolboxService : AccordionControl, IToolboxService
{
    EasyAccordionControlElement _SelectedElement;
    public Control DesignPanel { get; set; }

    public EasyAccordionToolboxService()
    {
        AddAccordions();
        AllowItemSelection = true;
        base.ElementClick += EasyAccordionToolboxService_ElementClick;
    }

    private void EasyAccordionToolboxService_ElementClick(object sender, ElementClickEventArgs e)
    {
        if (e.Element.Level == 1) this._SelectedElement = e.Element as EasyAccordionControlElement;
    }

    public ToolboxItem GetSelectedToolboxItem()
    {
        if(_SelectedElement != null)
        {
            var selectedToolboxItem = this._SelectedElement.ToolBoxItem;
            this._SelectedElement = null;
            return selectedToolboxItem;
        }
        else
        {
            return null;
        }
    }

        public bool SetCursor()
    {
        if(this._SelectedElement == null)
        {
            this.DesignPanel.Cursor = Cursors.Default;
        }
        else
        {
            this.DesignPanel.Cursor = Cursors.Hand;
        }

        return true;
    }

There are "DesignPanel" property and "SetCursor" methods. DesignPanel property is control referred to view of root designer.

  1. When I drop, nothing happens, and when my cursor leave the design surface (after I dropped), then the new Control is created and added where I dropped it.

There is GetSelectedToolboxItem method in EasyAccordionToolBoxService implemented from IToolboxService. GetSelectedToolboxItem is your answer.

Finally,

        designer = new EasyDesignSurface(propertyGrid);

        var rootForm = (Form)designer.CreateComponent(typeof(Form));
        rootForm.Name = "rootForm";
        rootForm.Text = "BELGE";
        rootForm.TopLevel = false;
        rootForm.Size = new Size(600, 600);
        designer.ComponentContainer.Add(rootForm);

        var view = (Control)designer.View;
        view.Dock = DockStyle.Fill;
        designPanel.Controls.Add(view);

        easyAccordionToolboxService1.DesignPanel = view;
        designer.AddService(typeof(IToolboxService), easyAccordionToolboxService1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文