Reputation: 1
I am trying to launch a program ("notepad.exe" for example) and then resize the window. When I run the code below I get a non-zero window handle but setWindowPos does nothing (it does not throw an error). Can someone help me understand what i am doing wrong, I am new to the Windows API.
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
static void Main(string[] args)
{
try
{
// Start the process
Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.Start();
process.WaitForInputIdle(2000);
// Get the process handle
IntPtr processHandle = process.Handle;
if (processHandle == IntPtr.Zero)
{
Console.WriteLine("Null pointer");
}
Console.WriteLine($"Process started with handle: 0x{processHandle.ToInt64():X}");
try
{
// 0x0040 is SHOWWINDOW
SetWindowPos(processHandle, 0, 0, 0, 1000, 200, 0x0040);
Console.WriteLine("Set the window position");
} catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine(); // Keep handle valid until user input
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
Upvotes: 0
Views: 32