【问题标题】:SWT drag and drop custom Control displayed next to cursorSWT拖放自定义控件显示在光标旁边
【发布时间】:2012-02-10 11:40:37
【问题描述】:

在 SWT 中拖动时,如何在光标旁边显示一些自定义控件或小部件?

喜欢按钮、树或表格而不是图像。

如果不可能 - 我如何将这样的按钮渲染到图像中?

【问题讨论】:

    标签: java user-interface drag-and-drop swt


    【解决方案1】:

    我找到了使用 GC (canvas) 类将任意 Control (widget) 渲染到 Image 中的方法。方法如下:

    dragSource.addDragListener(new DragSourceListener() {
        @Override
        public void dragStart(DragSourceEvent dragSourceEvent) {
            // getting control widget - Composite in this case
            Composite composite = (Composite) ((DragSource) dragSourceEvent.getSource()).getControl();
            // Getting dimensions of this widget
            Point compositeSize = composite.getSize();
            // creating new GC
            GC gc = new GC(composite);
            // Creating new Image
            Image image = new Image(Display.getCurrent(),compositeSize.x,compositeSize.y);
            // Rendering widget to image
            gc.copyArea(image,0,0);
            // Setting widget to DnD image
            dragSourceEvent.image = image;
        }
    ... other overriden methods ...
    }
    

    【讨论】:

    • 记得把你的资源放到dragFinished。 :)
    【解决方案2】:

    您可以使用跟随鼠标位置的未修饰的 Shell,并且在该 Shell 中具有您要显示的小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多