473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

set focus on the program with win32

Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process. Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);
while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated

Apr 11 '07 #1
3 11545
On Apr 11, 7:36 pm, Vinki <V...@discussions.microsoft.comwrote:
Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process. Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);

while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated
perhaps you need to call process.WaitForInputIdle() after calling
process.Start()?

Apr 12 '07 #2
"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process. Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);
while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated
You'll have to wait after Start until the process has it's main window created.
When the process has a UI you can use WaitForInputIdle, else you'll have to Sleep until the
MainWindowHandle returns a non null handle value..
Something like this will do...

....
process.Start();
System.Threading.Thread.Sleep(200);
IntPtr hWnd = process.MainWindowHandle;
while(hWnd == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
// or:
// process.WaitForInputIdle(100);
hWnd = process.MainWindowHandle;
Application.DoEvents(); // or run this code on a non UI thread!
}
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
..
Take care that you don't wait endlessly in the while loop!!

Willy.
Apr 12 '07 #3
Thanks a lot Willy. That was the problem. I really appreciate your help.

Thanks again.

"Willy Denoyette [MVP]" wrote:
"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process. Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);
while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated

You'll have to wait after Start until the process has it's main window created.
When the process has a UI you can use WaitForInputIdle, else you'll have to Sleep until the
MainWindowHandle returns a non null handle value..
Something like this will do...

....
process.Start();
System.Threading.Thread.Sleep(200);
IntPtr hWnd = process.MainWindowHandle;
while(hWnd == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
// or:
// process.WaitForInputIdle(100);
hWnd = process.MainWindowHandle;
Application.DoEvents(); // or run this code on a non UI thread!
}
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
..
Take care that you don't wait endlessly in the while loop!!

Willy.
Apr 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Todd Miller | last post by:
I'm working on a Tkinter backend for the matplotlib plotting software. matplotlib can be run interactively from some form of Python shell while plotting to a Tk window. One annoyance we've noticed...
2
by: Dov P | last post by:
hello everyone, i need to use api function named RegSetKeySecurity in my C# program. This function require ADVAPI32.DLL, but i cannot add this reference. I tried to use Microsoft.Win32.Registry...
3
by: Dean L. Howen | last post by:
Could anyone tell me how to program win32 in C#?
10
by: James Pyrich | last post by:
Greetings: I am using .NET to pop up a graphical window for a legacy application (remember the IBM System/23?). In order to achieve good performance, I created a GUI Broker Server and a Client...
6
by: TM | last post by:
For some reason I have a problem with the application focus deal in windows explorer getting reset. I used TweakUI to set the "Prevent applications from stealing focus" to on so that it flashes...
4
by: sam | last post by:
hi, How to set constant focus on window form in c#. cheers Sam
3
by: JDeats | last post by:
I have a Windows Forms application in which I need to be able to trap on an event when the application itself has lost focus (e.g. if the user clicks on another application they have opened on the...
6
by: Gilles Ganault | last post by:
Hello It looks like the development of the PyWin32 wrapper to the Win32 API stopped years ago, which is too bad because it means that writing GUI apps in Python even just for Windows means...
5
by: digory | last post by:
Hi Our product consists of a complex application that is written in C/C++ and uses the old event-based Win32 API (some code has already been in there in times of Windows 3.11!) We cannot afford...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.