diff --git a/CHANGES.md b/CHANGES.md index bb9b649d0f..8ed38bf40f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Features * [#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). +* [#1354](https://github.com/java-native-access/jna/pull/1352): Add `GetParent` 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 4c86920638..a1e43aada1 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/User32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/User32.java @@ -2274,6 +2274,30 @@ LRESULT SendMessageTimeout(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam, */ HWND GetAncestor(HWND hwnd, int gaFlags); + /** + * Retrieves a handle to the specified window's parent or owner. + * + * To retrieve a handle to a specified ancestor, use the GetAncestor function. + * + * @param hwnd + * A handle to the window whose parent window handle is to be retrieved. + * @return Type: HWND
+ * If the window is a child window, the return value is a handle to the + * parent window. If the window is a top-level window with the WS_POPUP style, + * the return value is a handle to the owner window. + * + * If the function fails, the return value is NULL. To get extended error + * information, call GetLastError. + * + * This function typically fails for one of the following reasons: + * + */ + HWND GetParent(HWND hwnd); + /** * Retrieves the position of the mouse cursor, in screen coordinates. * 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 0715e27fb5..c3c8321a22 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java @@ -64,6 +64,7 @@ import com.sun.jna.platform.win32.WinUser.MONITORINFOEX; import javax.swing.JFrame; +import javax.swing.JWindow; /** * @author dblock[at]dblock[dot]org @@ -377,6 +378,35 @@ public void testGetAncestor() { assertNull("GetAncestor result should be null", result); } + @Test + public void testGetParent() { + HWND desktopWindow = User32.INSTANCE.GetDesktopWindow(); + assertNotNull("Failed to get desktop window HWND", desktopWindow); + + HWND result = User32.INSTANCE.GetParent(desktopWindow); + assertNull("GetParent result should be null", result); + + final JFrame parent = new JFrame("Parent"); + final JWindow child = new JWindow(parent); + + try { + parent.setVisible(true); + child.setVisible(true); + + HWND parentHwnd = new HWND(); + parentHwnd.setPointer(Native.getComponentPointer(parent)); + + HWND childHwnd = new HWND(); + childHwnd.setPointer(Native.getComponentPointer(child)); + + result = User32.INSTANCE.GetParent(childHwnd); + assertEquals("GetParent of child should be parent", parentHwnd, result); + } finally { + child.dispose(); + parent.dispose(); + } + } + @Test public void testGetCursorPos() { POINT cursorPos = new POINT();