首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWT.Tracker鼠标从左到右,从上到下跳跃

SWT.Tracker鼠标从左到右,从上到下跳跃
EN

Stack Overflow用户
提问于 2013-10-23 16:20:44
回答 3查看 356关注 0票数 2

我尝试创建一个可以使用SWT.Tracker调整大小的组合,下面是我的示例代码

代码语言:javascript
复制
        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();
          }
        });

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

EN

回答 3

Stack Overflow用户

发布于 2015-09-22 22:55:15

下面是解决方案:当鼠标到达控件的边缘时,跟踪鼠标的位置并显示适当的光标,然后相应地决定在Tracker构造函数中设置哪种样式。保持任何边或角的修剪区域,以便用户可以轻松地输入您的Tracker

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

声明监听器,更改游标,并相应地调用Tracker

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
Listener    releaseTrackerListener  = new Listener()
                                        {

                                            @Override
                                            public void handleEvent(Event even)
                                            {

                                                resizeAction = 0;
                                                tracker.dispose();
                                            }
                                        };
票数 3
EN

Stack Overflow用户

发布于 2013-10-25 00:06:33

如果要始终向右向下延伸,请将创建跟踪器的位置线更改为:

代码语言:javascript
复制
Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE | SWT.RIGHT | SWT.DOWN);

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

票数 1
EN

Stack Overflow用户

发布于 2015-09-23 23:00:25

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19536427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档