WPF C# Using User32.dll SetwindowPos Height is Not work

HoneyBee 186 Reputation points
2022-06-23T15:29:43.953+00:00

I referenced User32.dll in C#
Call the SetWindowPos function.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);

My monitor resolution is 2560 * 1440 .

By the way
If you do GetWindowRect

2560 * 1080 is returned
Actually in SetWindowPos
Even if you set the values ​​of 2560 and 1440
In the end, it is reflected as 2560, 1080.

What should I do?

My original source is atteched214462-airspacepopupcs.txt.

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2022-06-23T16:09:18.973+00:00

    If I do this test, the window is resized to the size of my screen (- borders, WS_OVERLAPPEDWINDOW style needs to be removed for FullScreen)

                IntPtr hWnd = (new WindowInteropHelper(this)).Handle;  
                SetWindowPos(hWnd, (IntPtr)HWND_TOPMOST,  
                          0, 0,  
                          GetSystemMetrics(SM_CXSCREEN),  
                          GetSystemMetrics(SM_CYSCREEN),  
                          SWP_FRAMECHANGED);  
    

    with

        [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
        public static extern int GetSystemMetrics(int nIndex);  
    
        public const int SM_CXSCREEN = 0;  
        public const int SM_CYSCREEN = 1;  
    
        [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);  
    
        public const int SWP_NOSIZE = 0x0001;  
        public const int SWP_NOMOVE = 0x0002;  
        public const int SWP_NOZORDER = 0x0004;  
        public const int SWP_NOREDRAW = 0x0008;  
        public const int SWP_NOACTIVATE = 0x0010;  
        public const int SWP_FRAMECHANGED = 0x0020;  /* The frame changed: send WM_NCCALCSIZE */  
        public const int SWP_SHOWWINDOW = 0x0040;  
        public const int SWP_HIDEWINDOW = 0x0080;  
        public const int SWP_NOCOPYBITS = 0x0100;  
        public const int SWP_NOOWNERZORDER = 0x0200;  /* Don't do owner Z ordering */  
        public const int SWP_NOSENDCHANGING = 0x0400;  /* Don't send WM_WINDOWPOSCHANGING */  
        public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;  
        public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;  
        public const int SWP_DEFERERASE = 0x2000;  
        public const int SWP_ASYNCWINDOWPOS = 0x4000;  
    
        public const int HWND_TOPMOST = -1;
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.