我有一个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);更多推荐
发布评论