3

我将一些CTabItem带有Images 的 s 添加到 a CTabFolder

CTabFolder tabFolder = new CTabFolder(someSection, SWT.BORDER);
ImageDescriptor deleteImageDesc = sharedImages.getImageDescriptor(ISharedImages.IMG_ETOOL_DELETE);
Image deleteImage = deleteImageDesc.createImage();
CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
tabItem.setImage(deleteImage);
// add more tabs...

选项卡项

然后我想创建一个ToolTip如果用户将鼠标移到deleteImage.

ToolTip deleteToolTip = new ToolTip(getShell(), SWT.BALOON);
deleteToolTip.setMessage("Delete");
tabFolder.addMouseTrackListener(new MouseTrackAdapter()
{
    @Override
    public void mouseHover(MouseEvent e)
    {
        toolTip.setLocation(tabFolder.toDisplay(e.x, e.y));
        toolTip.setVisible(doesAnyOfTabImagesContainPoint(mousePosition));
    }
});

要实现方法doesAnyOfTabImagesContainPoint,我需要确定每个deleteImage. 因为CTabItem不是Control我不能使用的方法toDisplay。我尝试通过手动确定deleteImage相对于tabFolder. 这会有所帮助,因为所持有的鼠标位置MouseEvent也相对于tabFolder.

private boolean doesAnyOfTabImagesContainPoint(Point p)
{
    for (CTabItem tabItem : tabFolder.getItems())
    {
        Image i = tabItem.getImage();
        Rectangle tabItemBounds = tabItem.getBounds();
        Rectangle imageBounds = i.getBounds();
        imageBounds.x += tabItemBounds.x;
        imageBounds.y += tabItemBounds.y;
        if (imageBounds.contains(p))
            return true;
    }
    return false;
}

正常工作的要求是Rectangle返回的i.getBounds()具有相对于 的正确位置tabItem。然而,它返回(0, 0, 16, 16)这是不可能的。

解决此问题的一种肮脏方法是仅添加一些常量:

imageBounds.x += bsTabBounds.x + 4;
imageBounds.y += bsTabBounds.y + 3;

但我想知道是否有更好的方法。我正在尝试研究如何CTabFolder定位标签的图像,但现在还没有成功。任何帮助,将不胜感激。提前致谢。

编辑:出于测试目的,这是我从中获得的提取图像,ISharedImages以查看其边框:删除.png

4

1 回答 1

1

试一下下面的代码。这是基于我在这里的回答。

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final TabFolder folder = new TabFolder(shell, SWT.NONE);

    TabItem item = new TabItem(folder, SWT.NONE);
    item.setImage(display.getSystemImage(SWT.ICON_ERROR));
    item.setText("Text");

    folder.addListener(SWT.MouseMove, event -> {
        TabFolder curFolder = (TabFolder) event.widget;
        Point eventLocation = new Point(event.x, event.y);
        TabItem theItem = curFolder.getItem(eventLocation);

        if (theItem == null)
            return;

        Image image = theItem.getImage();

        if (eventLocation.x >= curFolder.getClientArea().x + theItem.getBounds().x + image.getBounds().x
                && eventLocation.x <= curFolder.getClientArea().x + theItem.getBounds().x + image.getBounds().x + image.getBounds().width
                && eventLocation.y >= theItem.getBounds().y + image.getBounds().y
                && eventLocation.y <= theItem.getBounds().y + image.getBounds().y + image.getBounds().height)
        {
            System.out.println("show tooltip");
        }
    });


    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

我已经在 Windows 7 上对其进行了测试,它似乎工作正常。

于 2015-04-29T15:29:15.257 回答