Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize other window or application c#

Tags:

c#

windows

I'm trying to resize a window to the resolution 1280x720 with c#. How can I make it? I've tried a lot of codes posted here in stack and the closest result I've got was this:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint);

        static void Main(string[] args)
        {
            Process[] processes = Process.GetProcessesByName("notepad");
            foreach (Process p in processes)
            {
                IntPtr handle = p.MainWindowHandle;
                RECT Rect = new RECT();
                if (GetWindowRect(handle, ref Rect))
                    MoveWindow(handle, Rect.left, Rect.right, Rect.right-Rect.left, Rect.bottom-Rect.top + 50, true);
            }
        }
    }
}

Thanks for your time.

like image 943
David Amaral Avatar asked Aug 27 '15 22:08

David Amaral


People also ask

How do I force an application window to resize?

Press Alt + Spacebar again to open the window menu, arrow down to Size, and press Enter . Press the up or down arrow key if you want to resize the window vertically or the left or right arrow key if you want to resize horizontally.

How to resize winforms?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.

How to resize a window in another program?

You need to get the window handle of the other program, use Process.MainWindowHandle or FindWindow. Having this, you can PInvoke SetWindowPos () to move, resize, change the Z-order or the min/max/restore state of the window.

How does the system change the size of a window?

The system changes the size of a window when the user chooses window menu commands, such as Size and Maximize, or when the application calls functions, such as the SetWindowPos function. When a window changes size, the system assumes that the contents of the previously exposed portion of the window are not affected and need not be redrawn.

How do I set the width and height of a window?

Use the Width or Height property to set the window width and height independently. This example resizes the Microsoft Excel application window to 6 inches wide by 4 inches high. This example resizes the Word application window to 7 inches wide by 6 inches high. Have questions or feedback about Office VBA or this documentation?

How to resize partition D to C drive in Windows 10?

Step 2: Right click D and select "Resize Partition". In the pop-up window, point your mouse on the left board and drag it toward the right side to shrink D so to get the unallocated space you need for C drive, or you can input the exact number. (here, free up 60GB from D drive). Then click “OK” to continue.


2 Answers

If you want the window to have a size of 1280x720 then use that size in the MoveWindow call.

  MoveWindow(handle, Rect.left, Rect.top, 1280, 720, true);
like image 54
shf301 Avatar answered Oct 26 '22 01:10

shf301


In response to David Amaral, to center the window, use this :

MoveWindow(handle, (Screen.PrimaryScreen.WorkingArea.Width - 1280) / 2, 
    (Screen.PrimaryScreen.WorkingArea.Height - 720) / 2, 1280, 720, true);

By the way, here's the code in vb.net for persons who want :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim processes As Process() = Process.GetProcessesByName("notepad")
    For Each p As Process In processes
        Dim handle As IntPtr = p.MainWindowHandle
        Dim Rect As New RECT()
        If GetWindowRect(handle, Rect) Then
            MoveWindow(handle, (My.Computer.Screen.WorkingArea.Width - 1280) \ 2, (My.Computer.Screen.WorkingArea.Height - 720) \ 2, 1280, 720, True)
        End If
    Next
End Sub

<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
    Public left As Integer
    Public top As Integer
    Public right As Integer
    Public bottom As Integer
End Structure

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef Rect As RECT) As Boolean
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function MoveWindow(hWnd As IntPtr, X As Integer, Y As Integer, Width As Integer, Height As Integer, Repaint As Boolean) As Boolean
End Function
like image 43
Drarig29 Avatar answered Oct 26 '22 02:10

Drarig29