diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java index 715975fe282..5d70af8380e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java @@ -1412,7 +1412,15 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { } else { OS.SetRect (focusRect, nmcd.left+4, nmcd.top+4, nmcd.right-4, nmcd.bottom-4); } - OS.DrawFocusRect(nmcd.hdc, focusRect); + // DrawFocusRect() inverts the pixels, which is an arbitrary color on a custom background. + long pen = OS.CreatePen(OS.PS_DOT, 1, foreground); + OS.SaveDC(nmcd.hdc); + OS.SelectObject(nmcd.hdc, pen); + OS.SelectObject(nmcd.hdc, OS.GetStockObject(OS.NULL_BRUSH)); + OS.SetBkMode(nmcd.hdc, OS.TRANSPARENT); + OS.Rectangle(nmcd.hdc, focusRect.left, focusRect.top, focusRect.right, focusRect.bottom); + OS.RestoreDC(nmcd.hdc, -1); + OS.DeleteObject(pen); } return new LRESULT (OS.CDRF_SKIPDEFAULT); } diff --git a/examples/org.eclipse.swt.snippets/Snippets.md b/examples/org.eclipse.swt.snippets/Snippets.md index 08cd32f387c..7014393f942 100644 --- a/examples/org.eclipse.swt.snippets/Snippets.md +++ b/examples/org.eclipse.swt.snippets/Snippets.md @@ -60,6 +60,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h - [create a tri-state button (toggle three states)](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet315.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet315.png "Preview for Snippet 315") - [create a non-rectangular button](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet294.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet294.png "Preview for Snippet 294") - [create buttons with wrapped titles](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet345.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet345.png "Preview for Snippet 345") +- [show the focus indication on a button with a custom background and foreground](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet395.java) ### **Canvas** - [paint a circle in a canvas](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet245.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet245.png "Preview for Snippet 245") diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet395.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet395.java new file mode 100644 index 00000000000..9296e8faeb3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet395.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Button example: focus indication on a button with a custom background and + * foreground. Tab between the controls to move the focus. On Windows the focus + * rectangle used to be XOR-inverted, which turned into an arbitrary + * complementary color on the saturated background below. + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet395 { + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("Focus rectangle on a styled Button"); + shell.setLayout(new GridLayout()); + + Text text = new Text(shell, SWT.BORDER); + text.setText("Tab out of this field"); + text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + + Button styled = new Button(shell, SWT.PUSH); + styled.setText("Styled default button"); + styled.setBackground(new Color(0x00, 0x71, 0xBC)); + styled.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); + styled.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + + Button plain = new Button(shell, SWT.PUSH); + plain.setText("Unstyled button"); + plain.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + + shell.setDefaultButton(styled); + shell.setSize(360, 200); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +}