0

I have a launching application that uses Process.Start("MyExe") to open another one of my applications, which is stored in an executable, and presents a WPF window.

However I also want to be able to set the position of the WPF on the screen from within the launcher program. I have tried using MoveWindow but it's not working.

Process flash = new Process();

flash.StartInfo.FileName = appDirectoryPath + "\\" + OnScreenKeyboardExe;
flash.Start();
Thread.Sleep(100);

IntPtr id = flash.MainWindowHandle;
File.WriteAllText("D://errorlog.txt", id.ToString());
OperatingSystemBase.MoveWindow(flash.MainWindowHandle, 1000, 1000, 500, 500, true);

in errorlog file I got id 0. So may be I am not able to get process or something.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dhimen2312
  • 13
  • 8
  • 'exe window' - is this a console window, or an actual WPF window (as your question tags suggest)? – LordWilmore Mar 15 '18 at 11:35
  • @LordWilmore, it's an actual wpf window. – Dhimen2312 Mar 15 '18 at 11:39
  • Can you please post the code where you attempted to use `MoveWindow`, along with any error that might have been thrown? – LordWilmore Mar 15 '18 at 11:41
  • @LordWilmore, sure. – Dhimen2312 Mar 15 '18 at 11:49
  • Slightly different approach, using `SetWindowPos` is explained here ... https://stackoverflow.com/questions/1190423/using-setwindowpos-in-c-sharp-to-move-windows-around. Might be worth a quick try. – LordWilmore Mar 15 '18 at 11:59
  • 1
    If the call to `MoveWindow` fails then please check `GetLastWin32Error()`, to see whether there is anything reported of use in there – LordWilmore Mar 15 '18 at 12:04
  • Ah, so if you're getting back zero for MainWindowHandle then that is your problem, and fortunately the web is full of people asking similar questions, so you might want to start looking there – LordWilmore Mar 15 '18 at 12:07
  • @LordWilmore, when i put thread to sleep for 300 it works but it shows exe window and then goes to specified location. – Dhimen2312 Mar 15 '18 at 13:45
  • Well yes, that will be the case. That's why it's called MoveWindow and not StartWithWindowAt – LordWilmore Mar 15 '18 at 13:52
  • What's the _actual_ problem you're trying to solve here? Why are you doing this in the first place? You may wish to have your own visual container open, and then open the new exe within that container, as explained here: https://social.msdn.microsoft.com/Forums/windows/en-US/94a67da1-8143-4e37-86c0-4deab0d1ec93/c-run-exe-from-a-form-and-keep-the-exe-within-the-boundaries-of-that-original-form?forum=winforms – LordWilmore Mar 15 '18 at 13:52

1 Answers1

0

Your problem is that you are not waiting long enough for the program to start, before you try to move it.

I changed Thread.Sleep(100) to Thread.Sleep(1000) and it worked fine!

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40