473,472 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 11552
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.