我有一个Windows应用程序(我用C#编写),它以一个没有边框的最大化窗口开始.
当用户单击应用程序中的按钮时,我想恢复窗口(即,移除最大化状态),将其调整为特定大小并将其移动到屏幕的右下角.
我的问题是,在SetWindowPos()正确调整窗口大小的同时,调用并不总是将其放在屏幕的右下角.有时它确实如此,但有时候窗口被放置在屏幕的其他地方(几乎就像它"跳"一样,原因我忽略了).
我究竟做错了什么?
这是我的代码.请注意,我将-1作为第二个参数传递给SetWindowPos,因为我希望我的窗口位于每个其他窗口的顶部.
public void MoveAppWindowToBottomRight()
{
Process process = Process.GetCurrentProcess();
IntPtr handler = process.MainWindowHandle;
ShowWindow(handler, 9); // SW_RESTORE = 9
int x = (int)(System.Windows.SystemParameters.PrimaryScreenWidth - 380);
int y = (int)(System.Windows.SystemParameters.PrimaryScreenHeight - 250);
NativePoint point = new NativePoint { x = x, y = y };
ClientToScreen(handler, ref point);
SetWindowPos(handler, -1, point.x, point.y, 380, 250, 0);
}
[StructLayout(LayoutKind.Sequential)]
public struct NativePoint
{
public int x;
public int y;
}
Run Code Online (Sandbox Code Playgroud)