アクティブな CTabItem の下線の色を変更したいと考えています。線は黒で、別の色が欲しいです。下の写真を参照してください。
2 に答える
私は@ 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();
}
標準のタブ フォルダー レンダラーorg.eclipse.swt.custom.CTabFolderRenderer
は、この色の変更をサポートしていません。
独自のレンダラーを作成してインストールすることもできますCTabFolder.setRenderer
が、これは非常に大変な作業です。
この行は、タブ自体にフォーカスがある場合にのみ表示されると思います。タブのコントロールにフォーカスを当てると、描画が停止します。