0

I know this question was asked several times, but still I couldn't make it work.

I have a top most - full screen window which on a button click needs to run a batch file with numerous commands, some of them open user interaction window.

As a starter, just to check that I'm on the right way, I'm trying to run a batch file with the command call notepad.
My goal is that the notepad will be on top of my caller window.

This is mainly trial and error now, so excuse me for the weird code.

Declarations and consts:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;

Execution:

static void ExecuteCommand(string command)
{
    Process process = new Process();
    process = Process.Start("c:\\TestFolder\\runNotepad.bat");

    SetForegroundWindow(process.MainWindowHandle);
    SetWindowPos(process.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    SetParent(process.MainWindowHandle, Process.GetCurrentProcess().MainWindowHandle);
}   

runNotepad.bat:

call notepad

Links that I've looked at:

Thanks!

Community
  • 1
  • 1
nafarkash
  • 359
  • 6
  • 24
  • 1
    Why do you run notepad from a btach file and not run it directly? Your code will only make the CMD window (DOS prompt) topmost, not Notepad – Thomas Weller Nov 28 '16 at 08:33
  • 1
    Have you tried `System.Diagnostics.Process`? Take a look at [this](http://stackoverflow.com/questions/16823518/calling-batch-file-from-c-sharp) question, might help! – devRicher Nov 28 '16 at 08:33
  • @ThomasWeller -This is just a dummy program to check if it's possible. My real batch file contains numerous commands, most of them are background (such as installing drivers), but one needs a user interaction in order to continue. My question is if it's even possible – nafarkash Nov 28 '16 at 08:50
  • @devRicher - My process is `System.Diagnostic.Process`.. The example you gave is just another implementation of the same thing I did. BTW I've tried the result given there before, didn't work. – nafarkash Nov 28 '16 at 08:54
  • It should be possible. Just run Notepad on its own. If needed, first run the batch file as a process and then run notepad as another process. – Thomas Weller Nov 28 '16 at 11:36

0 Answers0