Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Notepad to specific location on the screen, and to desired size?

Tags:

c#

resize

winapi

I need to open nNtepad to a specific size and in a specific position on the screen.

How can I do that from C#?

I'd appreciate a code sample.

like image 638
Nano Avatar asked Dec 13 '22 16:12

Nano


1 Answers

You can pinvoke MoveWindow. Like this:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        var prc = Process.Start("notepad.exe");
        prc.WaitForInputIdle();
        bool ok = MoveWindow(prc.MainWindowHandle, 0, 0, 300, 200, false);
        if (!ok) throw new System.ComponentModel.Win32Exception();
    }
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
}
like image 117
Hans Passant Avatar answered Dec 28 '22 09:12

Hans Passant