473,396 Members | 2,021 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,396 software developers and data experts.

Process SetFocus

Hi All,

I need to start a new process calc for example and when ever the user click
on the button the
application should setfocus to the calc application.

I use this code but it is NOT working, what I do wrong ?

10x,

Shachar.

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
hWnd = myProcess.Handle;

if (hWnd == (IntPtr)0)
{
myProcess.Start();
hWnd = myProcess.Handle;
}
else
{
SetFocus(hWnd);
}
Dec 19 '05 #1
8 34222

"Shachar" <me@use.com> wrote in message
news:us*************@TK2MSFTNGP15.phx.gbl...
Hi All,

I need to start a new process calc for example and when ever the user
click on the button the
application should setfocus to the calc application.

I use this code but it is NOT working, what I do wrong ?

10x,

Shachar.

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
hWnd = myProcess.Handle;

if (hWnd == (IntPtr)0)
{
myProcess.Start();
hWnd = myProcess.Handle;
}
else
{
SetFocus(hWnd);
}


The process Handle is not a Windows handle! You should use the
MainWindowHandle property instead.

Willy.
PS. Please don't cross-post to non relevant NG's?
Dec 19 '05 #2
Can you show us the code for SetFocus()? Is this a p/invoke call to SetFocus
in user32.dll ?

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be
Dec 19 '05 #3
This is what I have tried and the focus is set correctly to the calculator

1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace ConsoleApplication3
5 {
6 class Class1
7 {
8 [STAThread]
9 static void Main(string[] args)
10 {
11 System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
12 myProcess.StartInfo.FileName = "calc";
13 myProcess.Start();
14 IntPtr hWnd = myProcess.Handle;
15 SetFocus(new HandleRef(null, hWnd));
16 }
17
18 [DllImport("user32.dll", CharSet=CharSet.Auto,
ExactSpelling=true)]
19 public static extern IntPtr SetFocus(HandleRef hWnd);
20 }
21 }

Gabriel
Dec 19 '05 #4
Hi,

It works because windows automatically set the focus on the new process
if you lost focus and try to set the focus using :
SetFocus(new HandleRef(null, hWnd));
It is not working .

SetFocus is an api from user32.dll

Shachar

"Gabriel Lozano-Morán" <gl*****@no-spam.org> wrote in message
news:eC****************@TK2MSFTNGP09.phx.gbl...
This is what I have tried and the focus is set correctly to the calculator

1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace ConsoleApplication3
5 {
6 class Class1
7 {
8 [STAThread]
9 static void Main(string[] args)
10 {
11 System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
12 myProcess.StartInfo.FileName = "calc";
13 myProcess.Start();
14 IntPtr hWnd = myProcess.Handle;
15 SetFocus(new HandleRef(null, hWnd));
16 }
17
18 [DllImport("user32.dll", CharSet=CharSet.Auto,
ExactSpelling=true)]
19 public static extern IntPtr SetFocus(HandleRef hWnd);
20 }
21 }

Gabriel

Dec 19 '05 #5
Hi,

I tried changing it to : hWnd = myProcess.MainWindowHandle;
But it don't set.

Shachar.

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eD**************@tk2msftngp13.phx.gbl...

"Shachar" <me@use.com> wrote in message
news:us*************@TK2MSFTNGP15.phx.gbl...
Hi All,

I need to start a new process calc for example and when ever the user
click on the button the
application should setfocus to the calc application.

I use this code but it is NOT working, what I do wrong ?

10x,

Shachar.

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
hWnd = myProcess.Handle;

if (hWnd == (IntPtr)0)
{
myProcess.Start();
hWnd = myProcess.Handle;
}
else
{
SetFocus(hWnd);
}


The process Handle is not a Windows handle! You should use the
MainWindowHandle property instead.

Willy.
PS. Please don't cross-post to non relevant NG's?

Dec 19 '05 #6

"Gabriel Lozano-Morán" <gl*****@no-spam.org> wrote in message
news:eC****************@TK2MSFTNGP09.phx.gbl...
This is what I have tried and the focus is set correctly to the calculator

1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace ConsoleApplication3
5 {
6 class Class1
7 {
8 [STAThread]
9 static void Main(string[] args)
10 {
11 System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
12 myProcess.StartInfo.FileName = "calc";
13 myProcess.Start();
14 IntPtr hWnd = myProcess.Handle;
15 SetFocus(new HandleRef(null, hWnd));
16 }
17
18 [DllImport("user32.dll", CharSet=CharSet.Auto,
ExactSpelling=true)]
19 public static extern IntPtr SetFocus(HandleRef hWnd);
20 }
21 }

Gabriel


Sorry, but this won't work for tree reasons:
1. The process Handle is not a Windows Handle (HWND), what you need is the
MainWindowHandle .
2. You have to wait for the process to start before you can get at it's main
window handle.
3. You can't set the focus using SetFocus() if the HWND is not attached to
calling thread's message queue.
Check the return value of your Win32 API call (using MainWindowHandle as
Handle), you'll see that it fails (PS you should always check API return
values!).
Anyway, you need to call SetForegroundWindow API to set the focus to the
receiving HWND and activate it's thread.

Here is your modified sample...

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
myProcess.WaitForInputIdle(2000);
IntPtr hWnd = myProcess.MainWindowHandle;
Console.WriteLine(hWnd);
bool p = SetForegroundWindow(hWnd);
if(!p)
Console.WriteLine("Could not set focus");
}
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)]
public static extern IntPtr SetFocus(IntPtr hWnd);
}
}
Willy.
Dec 19 '05 #7
10x,

It works.

Shachar.

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...

"Gabriel Lozano-Morán" <gl*****@no-spam.org> wrote in message
news:eC****************@TK2MSFTNGP09.phx.gbl...
This is what I have tried and the focus is set correctly to the
calculator

1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace ConsoleApplication3
5 {
6 class Class1
7 {
8 [STAThread]
9 static void Main(string[] args)
10 {
11 System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
12 myProcess.StartInfo.FileName = "calc";
13 myProcess.Start();
14 IntPtr hWnd = myProcess.Handle;
15 SetFocus(new HandleRef(null, hWnd));
16 }
17
18 [DllImport("user32.dll", CharSet=CharSet.Auto,
ExactSpelling=true)]
19 public static extern IntPtr SetFocus(HandleRef hWnd);
20 }
21 }

Gabriel


Sorry, but this won't work for tree reasons:
1. The process Handle is not a Windows Handle (HWND), what you need is the
MainWindowHandle .
2. You have to wait for the process to start before you can get at it's
main window handle.
3. You can't set the focus using SetFocus() if the HWND is not attached to
calling thread's message queue.
Check the return value of your Win32 API call (using MainWindowHandle as
Handle), you'll see that it fails (PS you should always check API return
values!).
Anyway, you need to call SetForegroundWindow API to set the focus to the
receiving HWND and activate it's thread.

Here is your modified sample...

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
myProcess.WaitForInputIdle(2000);
IntPtr hWnd = myProcess.MainWindowHandle;
Console.WriteLine(hWnd);
bool p = SetForegroundWindow(hWnd);
if(!p)
Console.WriteLine("Could not set focus");
}
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)]
public static extern IntPtr SetFocus(IntPtr hWnd);
}
}
Willy.

Dec 19 '05 #8
I guess I was a little to hasty in my post. Sorry bout that...

Bedankt Willy :)

Gabriel

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...

"Gabriel Lozano-Morán" <gl*****@no-spam.org> wrote in message
news:eC****************@TK2MSFTNGP09.phx.gbl...
This is what I have tried and the focus is set correctly to the calculator
1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace ConsoleApplication3
5 {
6 class Class1
7 {
8 [STAThread]
9 static void Main(string[] args)
10 {
11 System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
12 myProcess.StartInfo.FileName = "calc";
13 myProcess.Start();
14 IntPtr hWnd = myProcess.Handle;
15 SetFocus(new HandleRef(null, hWnd));
16 }
17
18 [DllImport("user32.dll", CharSet=CharSet.Auto,
ExactSpelling=true)]
19 public static extern IntPtr SetFocus(HandleRef hWnd);
20 }
21 }

Gabriel
Sorry, but this won't work for tree reasons:
1. The process Handle is not a Windows Handle (HWND), what you need is the
MainWindowHandle .
2. You have to wait for the process to start before you can get at it's

main window handle.
3. You can't set the focus using SetFocus() if the HWND is not attached to
calling thread's message queue.
Check the return value of your Win32 API call (using MainWindowHandle as
Handle), you'll see that it fails (PS you should always check API return
values!).
Anyway, you need to call SetForegroundWindow API to set the focus to the
receiving HWND and activate it's thread.

Here is your modified sample...

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process myProcess = new
System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
myProcess.WaitForInputIdle(2000);
IntPtr hWnd = myProcess.MainWindowHandle;
Console.WriteLine(hWnd);
bool p = SetForegroundWindow(hWnd);
if(!p)
Console.WriteLine("Could not set focus");
}
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)] public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)] public static extern IntPtr SetFocus(IntPtr hWnd);
}
}
Willy.

Dec 20 '05 #9

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

Similar topics

4
by: Mad Scientist Jr | last post by:
i am trying to set focus to a specific control depending on the outcome of a validator control and it is not working. none of these methods are working to setfocus: 1....
0
by: MrsLeigh | last post by:
I have a datagrid on a web page. In the footer there is a field that should be recieve the focus, either at the start or after a button is clicked to insert the row that is being added in the...
3
by: Jim Devenish | last post by:
In my application vehicles arrive and depart from a workshop. The ArrivalDate and the HandoverDate are each entered. Sometimes the person who should enter the arrival date forgets to do so. ...
12
by: Michael R | last post by:
TabCtrl --- ..............Tab1--Subform1 ..............Tab2--Subform2---TabCtrl2---Tab21 ..........................................................---Tab22...
0
by: srinivasarao yarru | last post by:
hi sir, in access 2003 how we can use the setfocuse property(we have to lostfocuse from one field with in that we have to setfocuse in same field) i am using this code but not setfocuse...
3
by: srinivasarao yarru | last post by:
hi in access 2003 how we can use the setfocuse property(we have to lostfocuse from one field with in that we have to setfocuse in same field) i am using this code but not setfocuse to same field...
5
agroover
by: agroover | last post by:
I can't seem to figure out how to get rid of the errors. I recieve the following error when I leave the Grade.SetFocus in my code... Microsoft Access can't move the focus to the control Grade ...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
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...
8
by: hawaiijeff | last post by:
I am writing an order receiving application in Access 2000. In this app you type in the tanker number the app looks that up in a table and should return just one row. The code below is showing how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.