简体   繁体   中英

How to change the color of underline of a CTabItem?

I would like to change the color of the underline of an active CTabItem. The line is black and I want another color, see picture below.

在此处输入图片说明

I agree with @greg-449 that normally you should not mess with CTabFolderRenderer but in some cases you have to do that. Luckily, you don't have to write again the entire renderer. This is the code in the original SWT renderer that draws the line:

            // 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);
                }
            }

The interesting part here is gc.drawLine(...) . You can let the original renderer draw everything and then you can draw on top of it you own line with a different color.

I just recomputed the arguments. I did cut some corners, and this will not work when text uses ellipses, but it can be a good starting point.

Note: this code might break with the next version of SWT. You have to update it whenever you update SWT.

Here is a snippet where the items have different colors:

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();
}

在此处输入图片说明

The standard tab folder renderer org.eclipse.swt.custom.CTabFolderRenderer does not support changing this color.

You can write your own renderer and install it using CTabFolder.setRenderer but this is quite hard work.

I think this line is only shown if the tab itself has focus, making a control in the tab have focus stops it being drawn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM