0

我想更改活动 CTabItem 下划线的颜色。这条线是黑色的,我想要另一种颜色,见下图。

在此处输入图像描述

4

2 回答 2

1

我同意@greg-449 的观点,通常你不应该乱来,CTabFolderRenderer但在某些情况下你必须这样做。幸运的是,您不必再次编写整个渲染器。这是原始 SWT 渲染器中绘制线条的代码:

            // draw a Focus rectangle
            if (parent.isFocusControl()) {
                Display display = parent.getDisplay();
                if (parent.simple || parent.single) {
                    gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
                    gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
                    gc.drawFocus(xDraw-1, textY-1, extent.x+2, extent.y+2);
                } else {
                    gc.setForeground(display.getSystemColor(BUTTON_BORDER));
                    gc.drawLine(xDraw, textY+extent.y+1, xDraw+extent.x+1, textY+extent.y+1);
                }
            }

这里有趣的部分是gc.drawLine(...). 您可以让原始渲染器绘制所有内容,然后您可以在其上绘制您自己的不同颜色的线。

我只是重新计算了论点。我确实偷工减料,当文本使用省略号时这将不起作用,但它可能是一个很好的起点。

注意:此代码可能会在 SWT 的下一个版本中中断。每次更新 SWT 时都必须更新它。

这是一个片段,其中项目具有不同的颜色:

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

    final int tabFolderStyle = SWT.NONE;
    final CTabFolder tabFolder = new CTabFolder(shell, SWT.NONE);
    tabFolder.setSimple(false);
    final CTabItem tabItem1 = new CTabItem(tabFolder, SWT.NONE);
    tabItem1.setText("Tab1");
    tabItem1.setData("color", display.getSystemColor(SWT.COLOR_CYAN));
    final CTabItem tabItem2 = new CTabItem(tabFolder, SWT.NONE);
    tabItem2.setText("Tab2");
    tabItem2.setData("color", display.getSystemColor(SWT.COLOR_YELLOW));


    tabFolder.setRenderer(new org.eclipse.swt.custom.CTabFolderRenderer(tabFolder){
        protected void draw (int part, int state, Rectangle bounds, GC gc) {
            super.draw(part, state, bounds, gc);
            if (part >= 0 && part == tabFolder.getSelectionIndex()) {
                int itemIndex = part;
                CTabItem item = parent.getItem(itemIndex);
                int x = bounds.x;
                int y = bounds.y;
                int height = bounds.height;
                int width = bounds.width;
                boolean onBottom = (tabFolderStyle & SWT.BOTTOM) != 0;

                Point extent = gc.textExtent(item.getText(), SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC);
                int textY = y + (height - extent.y) / 2;
                textY += onBottom ? -1 : 1;


                Rectangle trim = computeTrim(itemIndex, SWT.NONE, 0, 0, 0, 0);
                int xDraw = x - trim.x;

                gc.setForeground((Color) item.getData("color"));
                gc.drawLine(xDraw, textY+extent.y+1, xDraw+extent.x+1, textY+extent.y+1);
            }
        }
    });

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

在此处输入图像描述

于 2013-11-07T09:52:14.347 回答
0

标准选项卡文件夹渲染器org.eclipse.swt.custom.CTabFolderRenderer不支持更改此颜色。

您可以编写自己的渲染器并使用它进行安装,CTabFolder.setRenderer但这是一项非常艰巨的工作。

我认为这条线仅在选项卡本身具有焦点时才显示,使选项卡中的控件具有焦点会阻止它被绘制。

于 2013-11-06T21:52:07.237 回答