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.
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.
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.
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.
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.
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?
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With