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!