1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| using System; using System.Runtime.InteropServices; using System.Windows.Forms;
namespace ConsoleApp1 { class Program { [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[Flags()] private enum SetWindowPosFlags : uint { SynchronousWindowPosition = 0x4000, DeferErase = 0x2000, DrawFrame = 0x0020, FrameChanged = 0x0020, HideWindow = 0x0080, DoNotActivate = 0x0010, DoNotCopyBits = 0x0100, IgnoreMove = 0x0002, DoNotChangeOwnerZOrder = 0x0200, DoNotRedraw = 0x0008, DoNotReposition = 0x0200, DoNotSendChangingEvent = 0x0400, IgnoreResize = 0x0001, IgnoreZOrder = 0x0004, ShowWindow = 0x0040, }
static void Main(string[] args) { IntPtr target_hwnd = FindWindowByCaption(IntPtr.Zero, "Visual Studio");
if (target_hwnd != IntPtr.Zero) { int width = "<your window's width>"; int height = "<your window's height>"; int x = (Screen.PrimaryScreen.WorkingArea.Width - width) / 2; int y = (Screen.PrimaryScreen.WorkingArea.Height - height) / 2; SetWindowPos(target_hwnd, IntPtr.Zero, x, y, width, height, 0); } } } }
|