6

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.

2
  • 1
    You have Rect.right where you should have Rect.top, which is why the window moves off the screen sometimes. Commented Aug 27, 2015 at 22:39
  • Thanks for your time, really apreciated. Commented Aug 28, 2015 at 9:07

2 Answers 2

8

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); 
Sign up to request clarification or add additional context in comments.

8 Comments

Updated my answer, you want to pass the Left and Top to MoveWindow to keep the Window from moving.
Cool. But How can I center it in the screen after resizing?
Thanks for your time, really apreciated.
That code doesn't work. It says Unrecognized escape sequence "\".
Try with /. The \ operator is used in vb.net to return an integer result, and not a floating-point result but it doesn't exist in c#.
|
2

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.