这是解决方案:当鼠标到达控件边缘时跟踪鼠标位置并显示正确的光标,然后相应地决定在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();
}
};