Nano
Nano

Reputation: 49

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

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.

Upvotes: 4

Views: 7638

Answers (6)

Dennis
Dennis

Reputation: 61

public static void PlaceNotepad( int x, int y, int w, int h) 
{
    Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "iWindowPosX", x); // X is horizontal / left to right
    Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "iWindowPosY", y); // Y is vertical / top to bottom
    Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "iWindowPosDX", w); // DX is width
    Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "iWindowPosDY", h); // DY is height
}

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941705

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);
}

Upvotes: 11

Tom B
Tom B

Reputation: 2180

take a look at this question I think that'll do what you need.

Upvotes: 0

simion314
simion314

Reputation: 1394

I would start notepad and then move it to the desired location. You find the hwd of the notepad with FindWindow(unmanaged code) then send some move/resize events to the window.You will need to use some unmanaged code, possible windows hooks Maybe you can find here the code http://pinvoke.net/

Upvotes: 1

m.edmondson
m.edmondson

Reputation: 30892

Take a look here to access the command line from your app. The code would look like:

System.Diagnostics.Process.Start("Notepad");

I don't believe you have the ability to position exactly where you want on the screen

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44605

to open it you could use Process.Start or ShellExecute API call, to set its application window to a certain size and position I would call the API SetWindowsPos.

Upvotes: 1

Related Questions