<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:"Segoe UI";
        panose-1:2 11 5 2 4 2 4 2 2 3;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-NZ link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><i><span style='font-family:"Calibri","sans-serif";color:black'>I also tried Russells suggestion about setting the foreground window and it don’t work for me (Windows 8.1)<o:p></o:p></span></i></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I tested this earlier on W7 by starting another program, so my main program lost focus. My main program was triggering<br> </span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>SetForegroundWindow(forms.application.mainWindow.handle) <br>from a timer and it worked OK. That is, focus returned to my main program when the timer went off.<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Just now I ran <br>bcdShellExecute(<path toExcel>);<br>SetForegroundWindow(forms.application.mainWindow.handle);<br>and focus was not returned to my main program, it stayed with Excel.<br><br>As a guess I tried <br>bcdShellExecute(<path toExcel>);<br>Sleep(150);<br>SetForegroundWindow(forms.application.mainWindow.handle);<br>and focus was returned to my main program. We can each guess what this means about interrupting a starting program in W7.<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I tried a similar test on W8.1 and focus is not returned to my main program even using sleep(1250).<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>R<o:p></o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> delphi-bounces@listserver.123.net.nz [mailto:delphi-bounces@listserver.123.net.nz] <b>On Behalf Of </b>John Bird<br><b>Sent:</b> Friday, 25 July 2014 12:22 a.m.<br><b>To:</b> NZ Borland Developers Group - Delphi List<br><b>Subject:</b> Re: [DUG] Shellexecute question<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>Hey that should have been perfect, as I already was doing SW_SHOWMINIMIZED which works fine, and so does SW_SHOWNOACTIVE which also does what it should – start the other window but not change focus.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>However SW_SHOWMINNOACTIVE doesn’t work. Now the other program is a Delphi program of mine that does some initialisation in the Formshow event (turning on timers etc) and I am wondering if somehow the event doesn’t fire. The process starts, but nothing runs, looks asleep in Task Manager. Not worth messing around with, as its a minor issue.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>I toyed with using SW_SHOWINACTIVE and getting the program (Program B) to minimise itself on start, but that is just damned complicated and fiddly/fragile. It also seems prone to ending up with 2 icons on the task bar, as though multiple copies have started even if only one is running – (maybe due to the large amount of work it does on startup – it is unresponsive for a good while) .<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>I also tried Russells suggestion about setting the foreground window and it don’t work for me (Windows 8.1)<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>What I did in the end was go back to the SW_SHOWMINIMIZED and then after the ShellExecute (in Program A) I put a ShowMessage saying I had started the other program. Because this gives a modal clue that they have to click on to continue it will do the job of setting the focus back. And its a bit useful for them to be informed it has been started.<o:p></o:p></span></p></div><div><div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> <o:p></o:p></span></p></div><div><div><p class=MsoNormal style='background:whitesmoke'><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> <a href="mailto:jsmith@deltics.co.nz" title="jsmith@deltics.co.nz">Jolyon Smith</a> <o:p></o:p></span></p></div><div><p class=MsoNormal style='background:whitesmoke'><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>Sent:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> Thursday, July 24, 2014 8:21 PM<o:p></o:p></span></p></div><div><p class=MsoNormal style='background:whitesmoke'><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>To:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> <a href="mailto:russell@belding.co.nz" title="russell@belding.co.nz">Russell Belding</a> ; <a href="mailto:delphi@listserver.123.net.nz" title="delphi@listserver.123.net.nz">NZ Borland Developers Group - Delphi List</a> <o:p></o:p></span></p></div><div><p class=MsoNormal style='background:whitesmoke'><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>Subject:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'> Re: [DUG] Shellexecute question<o:p></o:p></span></p></div></div></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div></div><div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>Have you tried passing </span><strong><span style='font-size:10.0pt;font-family:"Segoe UI","sans-serif";color:#2A2A2A'>SW_SHOWMINNOACTIVE instead of SW_MINIMIZED ?</span></strong><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'><o:p> </o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Segoe UI","sans-serif";color:#2A2A2A'>Caveat: The show flag parameter is merely passed to the application being executed. What it chooses to do with that flag is it's own affair, but if you're lucky, it will respect your wishes. If not, then you will have to engage in a focus window arms race/lotto as already suggested. But Route #1 would be to try the officially mandated mechanisms.<br><br>Good luck. :)</span><span style='font-family:"Calibri","sans-serif";color:black'><o:p></o:p></span></p></div></div><div><p class=MsoNormal style='margin-bottom:12.0pt'><span style='font-family:"Calibri","sans-serif";color:black'><o:p> </o:p></span></p><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>On 24 July 2014 19:46, russell <<a href="mailto:russell@belding.co.nz" target="_blank">russell@belding.co.nz</a>> wrote:<o:p></o:p></span></p><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Tyr this after spawning the other program.</span><span style='color:black'><o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>SetForegroundWindow(forms.application.mainWindow.handle)</span><span style='color:black'><o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>To give the main window of your program focus.</span><span style='color:black'><o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Perhaps modifications of this will take you to the window of the calling program where you want the focus?</span><span style='color:black'><o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Russell</span><span style='color:black'><o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> <a href="mailto:delphi-bounces@listserver.123.net.nz" target="_blank">delphi-bounces@listserver.123.net.nz</a> [mailto:<a href="mailto:delphi-bounces@listserver.123.net.nz" target="_blank">delphi-bounces@listserver.123.net.nz</a>] <b>On Behalf Of </b>John Bird<br><b>Sent:</b> Thursday, 24 July 2014 4:43 p.m.<br><b>To:</b> NZ Borland Developers Group - Delphi List<br><b>Subject:</b> [DUG] Shellexecute question</span><span style='color:black'><o:p></o:p></span></p></div></div><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p><div><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>I have a program (Program A) that fires up another (program B) via ShellExecute, if its not already running. However even though Program B is started minimised, focus shifts away from Program A, which is a minor nuisance.</span><span style='color:black'><o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:black'>Is there any way to stop this within Delphi? Or will I have to do something like delve into the Windows API?</span><span style='color:black'><o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Calibri","sans-serif";color:black'>if ShellExecute(Application.Mainform.Handle, 'open', Pchar(aProgName), PChar(aparaml), PChar(aDir), SW_SHOWMINIMIZED) <= 32 then</span><span style='color:black'><o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Calibri","sans-serif";color:black'> ShowMessage('Start Minimised error:')</span><span style='color:black'><o:p></o:p></span></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> <o:p></o:p></span></p></div></div></div></div></div></div></div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'><br>_______________________________________________<br>NZ Borland Developers Group - Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'> <o:p></o:p></span></p></div></div><div class=MsoNormal align=center style='text-align:center'><span style='font-family:"Calibri","sans-serif";color:black'><hr size=2 width="100%" align=center></span></div><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:black'>_______________________________________________<br>NZ Borland Developers Group - Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a><br>Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></span></p></div></div></div></div></body></html>