簡體   English   中英

SWT.Tracker鼠標從左到右,從上到下跳

[英]SWT.Tracker mouse is jumping from left to right and top to bottom

我嘗試制作一個可以使用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();
          }
        });

當我開始向左向右拖動時,光標會跳到組合的右邊緣。 當我開始向頂部拖動到底部時,光標跳到組合的底部邊緣。 我查看了該類的文檔,但找不到任何解決此問題的設置。 有人有指針嗎?

解決方法是:跟蹤鼠標到達控件邊緣的位置並顯示適當的光標,然后相應地確定在Tracker構造函數中設置哪種樣式。 保持任何邊緣或角落的修剪區域,以便用戶輕松進入Tracker

例如,在X軸和Y軸上,您可以分別調用修剪xTrim=2yTrim=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();
                                            }
                                        };

如果要始終向右延伸,請將創建跟蹤器的行更改為:

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

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

AppWorks的解決方案效果出色。 在外殼上添加偵聽器以設置光標從控件邊界退出時的位置也是一個好主意,以便光標返回其原始箭頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM