如何使表格和HWND相互重新定位?(How can I make a Form and a HWND mutually reposition?)

编程入门 行业动态 更新时间:2024-10-26 18:19:36
如何使表格和HWND相互重新定位?(How can I make a Form and a HWND mutually reposition?)

我有一个System.Windows.Form和一个充当HWND的IntPtr 。

我希望他们每个人都把另一个放在移动中。 我很惊讶我在网上找不到“Hwnd get / set position c#”和许多变化,或许我忽略了明显的结果。

为了给出示例,考虑表单“窗口A”和Hwnd“窗口B”。 让我们也说我希望B的位置是A的位置+右边50像素。

I have a System.Windows.Form and a IntPtr acting as HWND.

I want each of them to place the other on move. I'm surprised I couldn't find anything on the web with "Hwnd get/set position c#" and many variations, perhaps I'm overlooking obvious results.

For the sake of the given examples, consider the Form "window A" and the Hwnd "window B". Let's also say I want B's position to be A's position + 50 pixels on the right.

最满意答案

更新:您可能还想查看WinForms的NativeWindow类 , 该类可用于包装本机HWWND并侦听发送到该窗口的窗口消息。

我想你需要Win32 API函数MoveWindow来设置窗口B( HWND )的位置(和尺寸)。 您可以通过P / Invoke从.NET调用此API函数。

为了检索窗口B的当前位置和大小,您可能还需要通过P / Invoke调用GetWindowRect 。


下面的代码可能不是开箱即用的,也许有更简单的解决方案,但它可能会给你一个起点,以及上面的链接:

// the following P/Invoke signatures have been copied from pinvoke.net: [DllImport("user32.dll", SetLastError = true)] internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } ... System.Windows.Form a = ...; IntPtr b = ...; RECT bRect; GetWindowRect(b, out bRect); MoveWindow(b, a.Location.X + 50, b.Location.Y, bRect.Right - bRect.Left, bRect.Bottom - bRect.Top, true);

Update: You might also want to check out WinForms' NativeWindow class, which can be used to wrap a native HWWND and listen to window messages sent to that window.

I suppose you'll need the Win32 API function MoveWindow to set the position (and dimensions) of your window B (the HWND one). You can call this API function from .NET via P/Invoke.

In order to retrieve the current position and size of window B, you may additionally need to call GetWindowRect, also via P/Invoke.


The following code might not work out of the box, and maybe there are simpler solutions, but it might give you a starting point, together with the above links:

// the following P/Invoke signatures have been copied from pinvoke.net: [DllImport("user32.dll", SetLastError = true)] internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } ... System.Windows.Form a = ...; IntPtr b = ...; RECT bRect; GetWindowRect(b, out bRect); MoveWindow(b, a.Location.X + 50, b.Location.Y, bRect.Right - bRect.Left, bRect.Bottom - bRect.Top, true);

更多推荐

本文发布于:2023-07-28 18:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   HWND   Form   mutually   reposition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!