Resize window size C#

感情迁移 提交于 2020-01-23 01:21:25

问题


Is it possible to resize a running application's window size from within another application? I want that when the application that I am building starts, another application (let's say itunes)'s width be reduced to its 2/3 so that the remaining 1/3 be occupied by my application. The two application should be running altogether and accessible by the user. Please help if possible.


回答1:


You can use SetWindowPos to resize another process's window.

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
        int x, int y, int width, int height, uint uFlags);

    private const uint SHOWWINDOW = 0x0040;

    private void resizeItunes()
    {
        System.Diagnostics.Process[] itunesProcesses = 
            System.Diagnostics.Process.GetProcessesByName("iTunes");

        if (itunesProcesses.Length > 0)
        {
            SetWindowPos(itunesProcesses[0].MainWindowHandle, this.Handle,
                0, 0, Screen.GetWorkingArea(this).Width * 2 / 3,
                Screen.GetWorkingArea(this).Height, SHOWWINDOW);
        }
    }



回答2:


You need to get the Windows' handle so use the FindWindow function at http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx then pass the handle to the window using SendMessage.

You need to SendMessage at http://msdn.microsoft.com/en-us/library/ms644950.aspx or PostMessage at http://msdn.microsoft.com/en-us/library/ms644944(VS.85).aspx with WM_SIZE (0x0005) and specify the size.



来源:https://stackoverflow.com/questions/8004256/resize-window-size-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!