4

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Nano
  • 49
  • 1
  • 3
  • Have you considered integrating a notepad clone into your application. That way you can customize it to do exactly what you need it to. I create such a clone and you can find the source code here: http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html – Luis Perez Aug 01 '12 at 19:18

6 Answers6

11

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);
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I have a question, This class is perfect for start aplication in desired position. But what i need now make changes when aplication is already runnini and no need start the appz. Finally, i not use this for handle notepad i inform notepad like example. The apication i want controll, when is updated, change the name of exe to, Aplication1.4, when is updated again Aplication1.5 etc etc. Process listed use that names, how can search process no matter the version of the software? I apreacite a lot if can modificate the class for do this 2 things i comented now Best regards, – Nano Jan 18 '11 at 21:50
  • 5
    My crystal ball is unable to reverse-engineer the comment, it suffered a syntax error exception six times, bit of a record. Somehow it doesn't seem to have much to do with the original question or notepad. It recommends a well formulated new question. – Hans Passant Jan 18 '11 at 23:35
1

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.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • I reformulate the question due i know how open notepad, : ) What i need exactly is move the position of notepad and resize the windows to desired values. Best regards – Nano Jan 18 '11 at 14:37
1

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/

simion314
  • 1,394
  • 16
  • 29
0

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

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0

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

Community
  • 1
  • 1
Tom B
  • 2,160
  • 2
  • 13
  • 15
0
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
}
Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
Dennis
  • 41
  • 5