2

我尝试制作一个可以使用 SWT.Tracker 调整大小的复合材料,这里是我的示例代码

        final Composite b = new Composite(parent, SWT.NONE);
        b.setBounds(20, 20, 80, 80);
        b.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        b.addListener(SWT.MouseDown, new Listener() {
          public void handleEvent(Event e) {
            Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
            tracker.setStippled(true);
            Rectangle rect = b.getBounds();
            tracker.setRectangles(new Rectangle[] { rect });
            if (tracker.open()) {
              Rectangle after = tracker.getRectangles()[0];
              b.setBounds(after);
            }
            tracker.dispose();
          }
        });

当我开始向左向右拖动时,光标会跳到合成的右边缘。当我开始从顶部向底部拖动时,光标会跳到合成的底部边缘。我查看了该类的文档,但找不到任何设置来解决此问题。有人有指针吗?

4

3 回答 3

3

这是解决方案:当鼠标到达控件边缘时跟踪鼠标位置并显示正确的光标,然后相应地决定在Tracker构造函数中设置哪种样式。为任何边缘或角落保留一个修剪区域,以便用户轻松进入您的Tracker.

例如,在 X 轴和 Y 轴上,您可以分别调用 trimsxTrim=2和,yTrim=2这样,在 X 轴上它将是 2 像素宽,在 Y 轴上它将是 3 像素宽的线进入Tracker;

声明一个监听器,更改光标并Tracker相应地调用:

Listener    resizeAndMoveListener   = new Listener()
                                        {

                                            Point   point   = null;

                                            public void handleEvent(Event event)
                                            {
                                                mousePoint = MouseInfo.getPointerInfo().getLocation();
                                                sRect = shell.getBounds();
                                                compoRect = composite.getBounds();
                                                sRect = shell.getBounds();
                                                compoRect = composite.getBounds();
                                                topLeftX = sRect.x + compoRect.x + 8;
                                                topLeftY = sRect.y + compoRect.y + 31;

                                                topRightX = sRect.x + compoRect.x + compoRect.width + 7;
                                                topRightY = sRect.y + compoRect.y + 31;

                                                bottomLeftX = sRect.x + compoRect.x + 8;
                                                bottomLeftY = sRect.y + compoRect.y + 31 + compoRect.height;

                                                bottomRightX = sRect.x + compoRect.x + compoRect.width + 8;
                                                bottomRightY = sRect.y + compoRect.y + 31 + compoRect.height;

                                                xTrim = 2;
                                                yTrim = 3;
                                                switch (event.type)
                                                {
                                                case SWT.MouseDown:

                                                    // Top Left corner point    
                                                    if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
                                                            && mousePoint.y >= topLeftY && mousePoint.y <= topLeftY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZENE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP | SWT.LEFT;
                                                    }
                                                    // Top Edge
                                                    else if (mousePoint.x > topLeftX + xTrim && mousePoint.x < topRightX - xTrim && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEN);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP;
                                                    }
                                                    // Top Right corner Point

                                                    else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP | SWT.RIGHT;
                                                    }
                                                    // Right edge 
                                                    else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX
                                                            && mousePoint.y > topRightY + xTrim && mousePoint.y < bottomRightY - xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.RIGHT;
                                                    }
                                                    // Bottom Left corner
                                                    else if (mousePoint.x >= bottomLeftX && mousePoint.x <= bottomLeftX + xTrim
                                                            && mousePoint.y >= bottomLeftY - xTrim && mousePoint.y <= bottomLeftY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM | SWT.LEFT;
                                                    }
                                                    // Left Edge 
                                                    else if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
                                                            && mousePoint.y > topLeftY + xTrim && mousePoint.y < bottomLeftY - xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.LEFT;
                                                    }
                                                    // Bottom Right corner
                                                    else if (mousePoint.x >= bottomRightX - xTrim && mousePoint.x <= bottomRightX
                                                            && mousePoint.y >= bottomRightY - xTrim && mousePoint.y <= bottomRightY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM | SWT.RIGHT;
                                                    }

                                                    // Bottom Edge 
                                                    else if (mousePoint.x > bottomLeftX + xTrim && mousePoint.x < bottomRightX - xTrim
                                                            && mousePoint.y > bottomRightY - yTrim && mousePoint.y < bottomRightY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEN);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM;
                                                    }
                                                    else
                                                    {
                                                        shell.setCursor(CURSOR_ARROW);
                                                        resizeAction = 0;
                                                    }
                                                    if (resizeAction == 1)
                                                        resize(event, resizeStyle);

                                                    else if (event.button == 1)
                                                    {
                                                        // prepare for moving when mouse button is down and resizeAction ==0
                                                        point = new Point(event.x, event.y);

                                                    }

                                                    break;

                                                case SWT.MouseMove:

                                                    if (point == null)
                                                        break;

                                                    int x = point.x - event.x;

                                                    int y = point.y - event.y;

                                                    Control control = (Control) event.widget;

                                                    move(shell, control, x, y);

                                                    point = null;

                                                    break;

                                                }

                                            }

                                        };

定义调整大小,即Tracker

void resize(Event event, final int STYLE)


{

    Control control = (Control) event.widget;
    tracker = new Tracker(control.getParent(), SWT.RESIZE | STYLE);
    tracker.setStippled(true);
    Rectangle rect = control.getBounds();
    tracker.setRectangles(new Rectangle[] { rect });
    if (tracker.open())
    {
        Rectangle after = tracker.getRectangles()[0];
        control.setBounds(after);
    }
    tracker.dispose();
    resizeAction = 0;
}

不要忘记释放Tracker鼠标:

Listener    releaseTrackerListener  = new Listener()
                                        {

                                            @Override
                                            public void handleEvent(Event even)
                                            {

                                                resizeAction = 0;
                                                tracker.dispose();
                                            }
                                        };
于 2015-09-22T14:55:14.880 回答
1

如果您想始终向右下方延伸,请将创建跟踪器的行更改为:

Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE | SWT.RIGHT | SWT.DOWN);

您可以使用 SWT.UP、SWT.DOWN、SWT.LEFT 和 SWT.RIGHT。

于 2013-10-24T16:06:32.730 回答
1

AppWorks 的解决方案非常有效。在 shell 上添加一个侦听器来设置光标从控件边界退出时也是一个好主意,以便光标返回其原始箭头。

于 2015-09-23T15:00:24.607 回答