From 137a110e819b996fa55fc4f96e64cfed913ee52d Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Fri, 4 Jun 2021 22:00:46 +0800 Subject: [PATCH] Add BringWindowToTop to User32 --- CHANGES.md | 1 + .../com/sun/jna/platform/win32/User32.java | 12 +++++++++++ .../sun/jna/platform/win32/User32Test.java | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index f258b9a91e..e07e7bade6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ Features * [#1337](https://github.com/java-native-access/jna/pull/1337): Add `REG_NOTIFY_THREAD_AGNOSTIC` to `c.s.j.p.win32.WinNet` and update `REG_LEGAL_CHANGE_FILTER` - [@Dani-Hub](https://github.com/Dani-Hub). * [#1338](https://github.com/java-native-access/jna/pull/1338): Add `RegNotifyChangeKeyValue` to `c.s.j.p.win32.Advapi32` - [@Dani-Hub](https://github.com/Dani-Hub). * [#1340](https://github.com/java-native-access/jna/issues/1340): Added `CM_Get_DevNode_Registry_Property` to `c.s.j.p.win32.Cfgmgr32` and corresponding util in `c.s.j.p.win32.Cfgmgr32Util` - [@dbwiddis](https://github.com/dbwiddis). +* [#1352](https://github.com/java-native-access/jna/pull/1352): Add `BringWindowToTop` to `c.s.j.p.win32.User32` - [@kahgoh](https://github.com/kahgoh) Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/User32.java b/contrib/platform/src/com/sun/jna/platform/win32/User32.java index 8179155716..4c86920638 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/User32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/User32.java @@ -359,6 +359,18 @@ public interface User32 extends StdCallLibrary, WinUser, WinNT { */ boolean EnumThreadWindows(int dwThreadId, WNDENUMPROC lpEnumFunc, Pointer data); + + /** + * Brings the specified window to the top of the Z order. If the window is a top-level window, it is activated. If + * the window is a child window, the top-level parent window associated with the child window is activated. + * + * @param hWnd + * A handle to the window to bring to the top of the Z order. + * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. + * To get extended error information, call GetLastError. + */ + boolean BringWindowToTop(HWND hWnd); + /** * The FlashWindowEx function flashes the specified window. It does not * change the active state of the window. diff --git a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java index 7c2b890777..0715e27fb5 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java @@ -63,6 +63,8 @@ import com.sun.jna.platform.win32.WinUser.MONITORINFO; import com.sun.jna.platform.win32.WinUser.MONITORINFOEX; +import javax.swing.JFrame; + /** * @author dblock[at]dblock[dot]org */ @@ -439,6 +441,25 @@ public void testGetActiveWindow() { assertNull("GetActiveWindow result should be null (there is no active window)", result); } + @Test + public void testBringWindowToTop() { + boolean result = User32.INSTANCE.BringWindowToTop(null); + assertFalse("BringWindowToTop(null) result should be false", result); + + final JFrame w = new JFrame("Frame to bring to top"); + try { + w.setVisible(true); + + HWND hwnd = new HWND(); + hwnd.setPointer(Native.getComponentPointer(w)); + + result = User32.INSTANCE.BringWindowToTop(hwnd); + assertTrue("Couldn't bring frame to top", result); + } finally { + w.dispose(); + } + } + @Test public void testSendMessage() { DesktopWindow explorerProc = getWindowByProcessPath("explorer.exe");