我正在为Eclipse插件实现自定义预览/工具提示。它在SWT中使用Shell来完成它,除去所有的碎片并在其中放置一个文本框。看起来不错。但是现在我需要在光标移出外壳窗口时处理外壳,并遇到一些问题:当光标移出它时,处理SWT外壳
将mousemoveListener附加到外壳是否有意义?首先,我是这么做的,但后来我意识到这个侦听器只捕获在shell中发生的鼠标移动事件。我将如何捕获鼠标离开外壳,以便我可以处理它?
感谢和问候, Krt_Malta
我正在为Eclipse插件实现自定义预览/工具提示。它在SWT中使用Shell来完成它,除去所有的碎片并在其中放置一个文本框。看起来不错。但是现在我需要在光标移出外壳窗口时处理外壳,并遇到一些问题:当光标移出它时,处理SWT外壳
将mousemoveListener附加到外壳是否有意义?首先,我是这么做的,但后来我意识到这个侦听器只捕获在shell中发生的鼠标移动事件。我将如何捕获鼠标离开外壳,以便我可以处理它?
感谢和问候, Krt_Malta
附加与MouseTrackAdapter一个MouseTrackListener作为侦听并重写mouseExit()方法。
作为另一种选择,您可以使用org.eclipse.jface.text
中的AbstractHoverInformationControlManager
,它处理所有讨厌的细节(例如,当您按Alt + Tab退出应用程序时,您的工具提示是否消失?)。事件处理是照顾好的,你可以专注于有趣的事情。一个例子:
import org.eclipse.jface.text.AbstractHoverInformationControlManager;
import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Example {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(200, 200);
final ExampleHoverInformationControlManager controlManager = new ExampleHoverInformationControlManager();
// This must be done for the control for which you want the mouse
// tracking to be enabled and you possibly want to show hovers.
controlManager.install(shell);
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
controlManager.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static class ExampleHoverInformationControlCreator extends
AbstractReusableInformationControlCreator {
@Override
protected IInformationControl doCreateInformationControl(Shell parent) {
return new DefaultInformationControl(parent);
}
}
private static class ExampleHoverInformationControlManager extends
AbstractHoverInformationControlManager {
protected ExampleHoverInformationControlManager() {
super(new ExampleHoverInformationControlCreator());
}
@Override
protected void computeInformation() {
MouseEvent e = getHoverEvent();
// Just a static example area for simplicity
if (e.x >= 0 && e.x < 100 && e.y >= 0 && e.y < 20) {
Rectangle area = new Rectangle(0, 0, 100, 20);
setInformation(
"This can be a string or something else, you control it", area); //$NON-NLS-1$
return;
}
// computeInformation must setInformation in all cases
setInformation(null, null);
}
}
}
我刚刚发现了一个更好的解决方案。它像这样运行。
Shell coverup = new Shell(SWT.NO_TRIM) ;
coverup.setBounds(parentComposite.getShell().getBounds());
coverup.moveAbove(Display.getCurrent().getActiveShell());
coverup.setAlpha(13);
coverup.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
popupShell = new Shell(coverup, SWT.NO_TRIM);
coverup.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent mouseEvent)
{
System.out.println("coverup - mouse moves");
coverup.close() ;
}
});
在英语里:
创建一个无形的外壳尺寸相同的应用程序/母贝。
用不可见的shell覆盖父shell。
连接鼠标输入监听器到覆盖外壳。
创建实际的弹出窗口,作为遮盖壳的子项,并在其上面 当鼠标进入弹出窗口时,激活遮盖。
这意味着无论在弹出的鼠标所在的位置,它都会进入coverup外壳 - 并且掩盖鼠标进入事件处理一切。
我们没有检测到鼠标退出弹出窗口时 - 我们正在检测鼠标何时进入环境。
附加奖励:将coverup的背景设置为浅灰色和低Alpha值,使整个应用程序微妙地变成灰色 - 因此用户知道的是禁用的。小问题:如果弹出式shell不完全包含在应用程序窗口中,弹出式shell跳出应用程序窗口,鼠标可以在不触发coverup shell的情况下转义。
除此之外 - 这真的很好!
谢谢,这真是太棒了!现在有一个技术问题: 严格来说,我将侦听器附加到shell内的文本框而不是shell,因为我没有收到由于某些原因而没有为shell生成的事件。因此,当我移动到文本框右侧的滚动条时,由于鼠标移出文本框,因此外壳正在放置。我该如何将听众连接到外壳? 感谢和问候, Krt_Malta – 2010-03-11 10:57:41
您可以直接将它连接到shell,shell也是一个小部件。 – pajton 2010-03-11 11:05:02