momsoft2
asked on
Launch a process in a designated monitor
I want to be able to launch a process in the Secondary monitor. I am using the code below, but it doesn't seem to work. In fact the result is the same, no matter what value do I use for the hIcon field.
Since notepad does remember its last position, I have also tested with a test application that does not set a default monitor and uses the default position when showing up.
Since notepad does remember its last position, I have also tested with a test application that does not set a default monitor and uses the default position when showing up.
const
SEE_MASK_HMONITOR = $00200000; // Not defined in ShellAPI unit
procedure TForm1.Button2Click(Sender: TObject);
var
sei : TShellExecuteInfo;
SecondaryMonitor : Cardinal;
begin
// Get the Handle to the secondary monitor
if Screen.MonitorCount = 1 then
SecondaryMonitor := 0
else if Screen.PrimaryMonitor = Screen.Monitors[0] then
SecondaryMonitor := Screen.Monitors[1].Handle
else
SecondaryMonitor := Screen.Monitors[0].Handle;
// Fill in ShellExecuteEx info
ZeroMemory(@sei,SizeOf(sei));
sei.cbSize := SizeOf(sei);
sei.fMask := SEE_MASK_HMONITOR;
sei.hIcon := SecondaryMonitor;
sei.lpVerb := 'open';
sei.lpFile := 'notepad.exe';
sei.nShow := SW_SHOWMAXIMIZED;
ShellExecuteEx(@sei);
end;
ASKER
ShellExecuteEx does not return any error whatsoever. It simply ignores the supplied monitor.
I am already getting the monitor handle using the Screen.Monitors[x].Handle. Getting the handle using the procedure you describe seems like going in circles.
Anyway, I have used GetMonitorInfo to check that the handle provided by TScreen is valid (see below). It gives the correct size of my secondary monitor.
Therefore, I suspect that the problem lies in ShellExecuteEx.
I am already getting the monitor handle using the Screen.Monitors[x].Handle.
Anyway, I have used GetMonitorInfo to check that the handle provided by TScreen is valid (see below). It gives the correct size of my secondary monitor.
Therefore, I suspect that the problem lies in ShellExecuteEx.
procedure TForm1.Button3Click(Sender: TObject);
var
SecondaryMonitor : Cardinal;
mi : TMonitorInfo;
begin
// Determine the monitor handle using Delphi Screen
if Screen.MonitorCount = 1 then
SecondaryMonitor := 0
else if Screen.PrimaryMonitor = Screen.Monitors[0] then
SecondaryMonitor := Screen.Monitors[1].Handle
else
SecondaryMonitor := Screen.Monitors[0].Handle;
// Check that the handle is valid
mi.cbSize := SizeOf(mi);
GetMonitorInfo(SecondaryMonitor,@mi);
ShowMessage(Format('The monitor rect is: %d,%d %d,%d',
[mi.rcMonitor.Left,mi.rcMonitor.Top,mi.rcMonitor.Right,mi.rcMonitor.Bottom]));
end;
Is the program you are trying to launch one of yours? Did you write it or do you access to the source code? The reason I ask is that the application your are trying to launch may have specific settings requiring it to launch in the same screen location everytime (i.e. ScreenCenter).
ASKER
I have tried many different programs, including a test one that does not set any special positioning, i.e. Position := poDefaultPosOnly.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
forgot to mention, after yuo found the handle of the window, use the movewindow function to place it in correct monitor as in my very first link.
ASKER
I was precisely thinking along the same line of placing myself the window in the desired monitor.
I prefer to use ShellExecute because I have had many problems in the past with CreateProcess. Getting the process ID is easy, enumerating processes. But I have a question? How do I ensure that ShellExecute does not return until the main window has been created? I have noticed that the function returns immediately.
Another issue is getting the main window once I have the process ID. What do you suggest?
I prefer to use ShellExecute because I have had many problems in the past with CreateProcess. Getting the process ID is easy, enumerating processes. But I have a question? How do I ensure that ShellExecute does not return until the main window has been created? I have noticed that the function returns immediately.
Another issue is getting the main window once I have the process ID. What do you suggest?
How does the main code of your test application look like? Delphi applications have a hidden main window that maybe is positioned on the other monitor instead of the real window. In newer Delphi this problem can be solved by adding "Application.MainFormOnTas kbar := True;" Maybe you/we should test this with a window application created by C++/Delphi without any framework (VCL, MFC, ATL ...).
You can wait for an process until it can process input events by calling WaitForInputIdle (http://msdn.microsoft.com/en-us/library/ms687022(VS.85).aspx)
You can wait for an process until it can process input events by calling WaitForInputIdle (http://msdn.microsoft.com/en-us/library/ms687022(VS.85).aspx)
"Maybe you/we should test this with a window application created by C++/Delphi without any framework (VCL, MFC, ATL ...). "
Sorry, I mean the whole problem! I'll check it in C++ when I have time.
Sorry, I mean the whole problem! I'll check it in C++ when I have time.
when you want your app to start on a second monitor,
i suppose you want all your forms to open on that same monitor
or where the app is residing at that moment
when you open a secondary form you need to set it to poOwnerFormCenter
and create your forms with passing the form calling it
procedure TForm1.Button1Click(Sender : TObject);
begin
Form2 := TForm2.Create(Self);
try
// do stuff with Form2
finally
FreeAndNil(Form2);
end;
end;
also when autocreating the forms in the dpr
change the owner of the second, third, etc forms from
Application.CreateForm(TFo rm1, Form1);
Application.CreateForm(TFo rm2, Form2);
to
Application.CreateForm(TFo rm1, Form1);
Form2 := TForm2.Create(Form1);
For the initial form i need to look into code at work
I think i solved this issue once
i suppose you want all your forms to open on that same monitor
or where the app is residing at that moment
when you open a secondary form you need to set it to poOwnerFormCenter
and create your forms with passing the form calling it
procedure TForm1.Button1Click(Sender
begin
Form2 := TForm2.Create(Self);
try
// do stuff with Form2
finally
FreeAndNil(Form2);
end;
end;
also when autocreating the forms in the dpr
change the owner of the second, third, etc forms from
Application.CreateForm(TFo
Application.CreateForm(TFo
to
Application.CreateForm(TFo
Form2 := TForm2.Create(Form1);
For the initial form i need to look into code at work
I think i solved this issue once
ASKER
I see from the comments received that I have not expressed myself correctly. My needs are to find a general way to launch ANY application and place it in a secondary monitor.
When I found the new functionality in SellExecuteEx, I thought that this was the correct approach, but I have discovered that it is not.
Therefore, the way to go seems to be:
1) Use ShellExecuteEx to launch the application.
2) Obtain the process ID by enumerating all running processes.
3) Use WaitForInputIdle to wait for the application window to be created.
4) Determine the main window of the application using EnumWindows.
5) Manually move the main window to the secondary monitor using MoveWindow.
I will create a test application and post it here for your comments. But so far, your comments are really helping me to solve this problem. Thank you all very much!
When I found the new functionality in SellExecuteEx, I thought that this was the correct approach, but I have discovered that it is not.
Therefore, the way to go seems to be:
1) Use ShellExecuteEx to launch the application.
2) Obtain the process ID by enumerating all running processes.
3) Use WaitForInputIdle to wait for the application window to be created.
4) Determine the main window of the application using EnumWindows.
5) Manually move the main window to the secondary monitor using MoveWindow.
I will create a test application and post it here for your comments. But so far, your comments are really helping me to solve this problem. Thank you all very much!
with CreateProcess you have direct access to the process id
var
ProcessInfo: TProcessInformation;
StartupInfo: TStartupInfo;
BatchFile: string; // example: "notepad.exe"
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
cb:= SizeOf(TStartupInfo);
dwFlags:= STARTF_USESHOWWINDOW;
wShowWindow:= SW_SHOWNORMAL;
end;
{ Execute batch file as separated process }
CreateProcess(PChar(BatchF ile), nil, nil, nil, False, NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo);
see ProcessInfo.hProcess for the processid
var
ProcessInfo: TProcessInformation;
StartupInfo: TStartupInfo;
BatchFile: string; // example: "notepad.exe"
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
cb:= SizeOf(TStartupInfo);
dwFlags:= STARTF_USESHOWWINDOW;
wShowWindow:= SW_SHOWNORMAL;
end;
{ Execute batch file as separated process }
CreateProcess(PChar(BatchF
nil, nil, StartupInfo, ProcessInfo);
see ProcessInfo.hProcess for the processid
ASKER
Your suggestion helped me a lot. I have not yet been able to make a test that works, but I am in the right track. If I get stuck, I'll post a new question. Thank you very much.
ASKER
I am working on a complete example, using the suggestions given in your comments. I have not finished it yet but it seems to be the way to go. Thank you all very much.
now, I never played with a multimnonitor system, so you will need to do this yourself and report back if it succeeded or not, dn if not, what errors you get (btw,. did you check what error shellexecuteex returnes?it would help if you would give us the code).
look at this function: http://msdn.microsoft.com/en-us/library/ms534603(VS.85).aspx
you will use this function to get the correct handle of the monitor using the Screen.Monitors[ x ].Left and top as desccribed in the first link. I figure since that works for your own application, getting the handle via monitorfrompoint should return a valid monitor handle which should work fine with shellexecuteex.
but, do not forget to tell us what error code shellexecuteex returns. we kind of need that to debug the issue ;)