Wednesday, September 24, 2003 10:27 PM
richard
P/Invoke to resize a window...
Here's a simple Platform Invoke example of how to resize a window - a feature I needed at one point (don't ask why) but couldn't find, not even in Eric Gunnerson's Win32 Window sample on GotDotNet.com.
using System;
using System.Runtime.InteropServices;
namespace SetWindowPositionSample
{
public sealed class Window
{
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName);
[DllImport(@"user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public static void SetPositionSize(string name, int x, int y, int width, int height)
{
IntPtr hWind = FindWindow(null, name);
SetWindowPos(hWind, (IntPtr)null, x, y, width, height, 0u);
}
private Window()
{
}
}
}
Here's a sample of how to call it:
public static void Main()
{
Process proc = new Process();
proc.StartInfo.FileName = "Notepad.exe";
proc.StartInfo.CreateNoWindow=true;
proc.Start();
// give the window time to open:
Thread.Sleep(100);
Window.SetPositionSize("Untitled - Notepad", 0, 0, 350, 111);
}