How to set the height of a window using c#?

后端 未结 2 1065
我寻月下人不归
我寻月下人不归 2020-12-16 00:05

Is it possible to set the height of a window using the window handle or process handle?

I have the following so far, assume the application in question is notepad.

2条回答
  •  一整个雨季
    2020-12-16 00:51

    You should be able to use the Win32 SetWindowPos function (use for both position and size). Here's a link for how to do it in C#.

    Here's a quick sample. This will move notepad to (10,10) on the screen, and resize it to (450,450):

    class Program
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
    
        static void Main(string[] args)
        {
            Console.WriteLine("Start notepad and hit any key...");
            Console.ReadKey(true);
            Process[] processes = Process.GetProcessesByName("notepad");
    
            foreach (Process p in processes)
            {
                var handle = p.MainWindowHandle;
    
                SetWindowPos(handle, new IntPtr(SpecialWindowHandles.HWND_TOP), 10,10,450,450,SetWindowPosFlags.SWP_SHOWWINDOW);
    
                break;
    
            }
    
        }
    }
    
    public enum SpecialWindowHandles
    {
        HWND_TOP = 0,
        HWND_BOTTOM = 1,
        HWND_TOPMOST = -1,
        HWND_NOTOPMOST = -2
    }
    
    [Flags]
    public enum SetWindowPosFlags : uint
    {
        SWP_ASYNCWINDOWPOS = 0x4000,
    
        SWP_DEFERERASE = 0x2000,
    
        SWP_DRAWFRAME = 0x0020,
    
        SWP_FRAMECHANGED = 0x0020,
    
        SWP_HIDEWINDOW = 0x0080,
    
        SWP_NOACTIVATE = 0x0010,
    
        SWP_NOCOPYBITS = 0x0100,
    
        SWP_NOMOVE = 0x0002,
    
        SWP_NOOWNERZORDER = 0x0200,
    
        SWP_NOREDRAW = 0x0008,
    
        SWP_NOREPOSITION = 0x0200,
    
        SWP_NOSENDCHANGING = 0x0400,
    
        SWP_NOSIZE = 0x0001,
    
        SWP_NOZORDER = 0x0004,
    
        SWP_SHOWWINDOW = 0x0040,
    }
    

提交回复
热议问题