473,787 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic question...on switching windows

If I know the "title" of an open window of an application running on the
same PC that my application is installed, say for example "Window abc",
what's the best method of getting my vb.net to "switch" to that window as if
I'd clicked on the taskbar or pressed ALT+TAB?

It should be simple, but I've had no luck so far ..

Thanks,

JC
Nov 21 '05 #1
8 4090
"John Clark" <jo**@johnNOTHI SBITclark.me.uk > schrieb
If I know the "title" of an open window of an application running on the
same PC that my application is installed, say for example "Window abc",
what's the best method of getting my vb.net to "switch" to that window as
if I'd clicked on the taskbar or pressed ALT+TAB?

It should be simple, but I've had no luck so far ..


Hi John,

get a handle of the window using Win32-API FindWindow
http://www.pinvoke.net/default.aspx/user32.FindWindow

and bring it to front using
http://www.pinvoke.net/default.aspx/...regroundWindow

or BringWindowToTo p
http://www.pinvoke.net/default.aspx/...ingWindowToTop

Cheers

Arne Janning

Nov 21 '05 #2
On Thu, 23 Sep 2004 00:31:53 +0200, Arne Janning wrote:
"John Clark" <jo**@johnNOTHI SBITclark.me.uk > schrieb
If I know the "title" of an open window of an application running on the
same PC that my application is installed, say for example "Window abc",
what's the best method of getting my vb.net to "switch" to that window as
if I'd clicked on the taskbar or pressed ALT+TAB?

It should be simple, but I've had no luck so far ..


Hi John,

get a handle of the window using Win32-API FindWindow
http://www.pinvoke.net/default.aspx/user32.FindWindow

and bring it to front using
http://www.pinvoke.net/default.aspx/...regroundWindow

or BringWindowToTo p
http://www.pinvoke.net/default.aspx/...ingWindowToTop

Cheers

Arne Janning


Actually, to make SetForegroundWi ndow work reliably, you'll also need to
use GetWindowThread ProcessId and AttachThreadInp ut as well...

findwindow 'get the window handle
getwindowthread processid 'get the thread handle for the window
attachthreadinp ut ' attach to it's input queue
setforgroundwin dow ' set it to the foreground
attachthreadinp ut ' detach from thread's input queue

This is because of changes made to the way SetForegroundWi ndow works in
Win98/Win2k and greater. The idea was to keep applications from stealing
the focus....

According to msdn SetForegroundWi ndow only works if one of the following is
true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegrou ndWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROU NDLOCKTIMEOUT
in SystemParameter sInfo).
Windows 2000/XP: No menus are active.

Using the above sequence seems to circumvent these restrictions and forces
the window to the top...

--
Tom Shelton [MVP]
Nov 21 '05 #3
"Tom Shelton" schrieb
According to msdn SetForegroundWi ndow only works if one of the following
is
true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegrou ndWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROU NDLOCKTIMEOUT
in SystemParameter sInfo).
Windows 2000/XP: No menus are active.

Using the above sequence seems to circumvent these restrictions and forces
the window to the top...


Hi Tom,

thanks for the feedback. You are right, of course, but I don't know if it
helps John.
As I couldn't find a working implementation in VB.NET on groups.google.c om
or somewhere else on the net here is some code that worked for me and might
work for John as well:

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click

Dim hwndApp As IntPtr
Dim hwndForeground As IntPtr
Dim ThreadForegroun d As Integer

hwndApp = FindWindow("Not epad", "Untitled - Notepad")
If Not hwndApp.Equals( IntPtr.Zero) AndAlso _
Not hwndApp.Equals( GetForegroundWi ndow) Then
ThreadForegroun d = GetWindowThread ProcessId(hwndF oreground,
IntPtr.Zero)
AttachThreadInp ut(ThreadForegr ound, 0, True)
SetForegroundWi ndow(hwndApp)
AttachThreadInp ut(ThreadForegr ound, 0, False)
End If
End Sub

One may have to use Spy++ to get more details about the window that shall be
set to the foreground.

Cheers

Arne Janning
Nov 21 '05 #4
On Thu, 23 Sep 2004 02:57:07 +0200, Arne Janning wrote:
"Tom Shelton" schrieb
According to msdn SetForegroundWi ndow only works if one of the following
is
true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegrou ndWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROU NDLOCKTIMEOUT
in SystemParameter sInfo).
Windows 2000/XP: No menus are active.

Using the above sequence seems to circumvent these restrictions and forces
the window to the top...


Hi Tom,

thanks for the feedback. You are right, of course, but I don't know if it
helps John.
As I couldn't find a working implementation in VB.NET on groups.google.c om
or somewhere else on the net here is some code that worked for me and might
work for John as well:

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click

Dim hwndApp As IntPtr
Dim hwndForeground As IntPtr
Dim ThreadForegroun d As Integer

hwndApp = FindWindow("Not epad", "Untitled - Notepad")
If Not hwndApp.Equals( IntPtr.Zero) AndAlso _
Not hwndApp.Equals( GetForegroundWi ndow) Then
ThreadForegroun d = GetWindowThread ProcessId(hwndF oreground,
IntPtr.Zero)
AttachThreadInp ut(ThreadForegr ound, 0, True)
SetForegroundWi ndow(hwndApp)
AttachThreadInp ut(ThreadForegr ound, 0, False)
End If
End Sub

One may have to use Spy++ to get more details about the window that shall be
set to the foreground.

Cheers

Arne Janning


Good example Arne...

--
Tom Shelton [MVP]
Nov 21 '05 #5
"Tom Shelton" schrieb
According to msdn SetForegroundWi ndow only works if one of the following
is
true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegrou ndWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROU NDLOCKTIMEOUT
in SystemParameter sInfo).
Windows 2000/XP: No menus are active.

Using the above sequence seems to circumvent these restrictions and forces
the window to the top...


Hi Tom,

thanks for the feedback. You are right, of course, but I don't know if it
helps John.
As I couldn't find a working implementation in VB.NET on groups.google.c om
or somewhere else on the net here is some code that worked for me and might
work for John as well:

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click

Dim hwndApp As IntPtr
Dim hwndForeground As IntPtr
Dim ThreadForegroun d As Integer

hwndApp = FindWindow("Not epad", "Untitled - Notepad")
If Not hwndApp.Equals( IntPtr.Zero) AndAlso _
Not hwndApp.Equals( GetForegroundWi ndow) Then
ThreadForegroun d = GetWindowThread ProcessId(hwndF oreground,
IntPtr.Zero)
AttachThreadInp ut(ThreadForegr ound, 0, True)
SetForegroundWi ndow(hwndApp)
AttachThreadInp ut(ThreadForegr ound, 0, False)
End If
End Sub

One may have to use Spy++ to get more details about the window that shall be
set to the foreground.

Cheers

Arne Janning
Nov 21 '05 #6
On Thu, 23 Sep 2004 02:57:07 +0200, Arne Janning wrote:
"Tom Shelton" schrieb
According to msdn SetForegroundWi ndow only works if one of the following
is
true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegrou ndWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROU NDLOCKTIMEOUT
in SystemParameter sInfo).
Windows 2000/XP: No menus are active.

Using the above sequence seems to circumvent these restrictions and forces
the window to the top...


Hi Tom,

thanks for the feedback. You are right, of course, but I don't know if it
helps John.
As I couldn't find a working implementation in VB.NET on groups.google.c om
or somewhere else on the net here is some code that worked for me and might
work for John as well:

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click

Dim hwndApp As IntPtr
Dim hwndForeground As IntPtr
Dim ThreadForegroun d As Integer

hwndApp = FindWindow("Not epad", "Untitled - Notepad")
If Not hwndApp.Equals( IntPtr.Zero) AndAlso _
Not hwndApp.Equals( GetForegroundWi ndow) Then
ThreadForegroun d = GetWindowThread ProcessId(hwndF oreground,
IntPtr.Zero)
AttachThreadInp ut(ThreadForegr ound, 0, True)
SetForegroundWi ndow(hwndApp)
AttachThreadInp ut(ThreadForegr ound, 0, False)
End If
End Sub

One may have to use Spy++ to get more details about the window that shall be
set to the foreground.

Cheers

Arne Janning


Good example Arne...

--
Tom Shelton [MVP]
Nov 21 '05 #7
Many thanks for all the assistance here :-) Very much appreciated!

JC

Nov 21 '05 #8
Many thanks for all the assistance here :-) Very much appreciated!

JC

Nov 21 '05 #9

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

Similar topics

16
2261
by: Kenneth McDonald | last post by:
For unfortunate reasons, I'm considering switching back to Win XP (from OS X) as my "main" system. Windows has so many annoyances that I can only compare it to driving in the Bay Area at rush hour (OS X is like driving in Portland at rush hour--not as bad, but getting there), but there are really only a couple of things that are really, absolutely preventing me from making the switch. Number one is the lack of a decent command line and...
56
4136
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation error. (Not as infrequent as some bugs that have been discussed here in the past, but maybe once in an overnight run of the program when it was configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
4
2229
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time process? 2. What information contained in sn key. I gone through that it is having public key. How it is using this key to intract with client. 3. When we have to run gacutil.exe file. Whenever we
4
2072
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired = True, I would then have the Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New Object() {ar}). How would one achieve the same thing on an asp.net page (without using a webseervice)? In the code below, because the...
13
15569
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the HTTP level but as yet no fully useful answer. I too have been trying to call an apache/axis webservice which desires a username/password from my C# Client. (ie the equivalent of _call.setUsername("Myname") name from within a Java client proxy)...
21
3224
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
3
4386
by: Simon Verona | last post by:
I have a parent form which contains a toolbar. The toolbar controls the loading and switching to of MDI child forms. The code for the toolbar click event and one of the subroutines that loads and shows a child form is: Private Sub tbMenu_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbMenu.ButtonClick Dim btn As System.Windows.Forms.ToolBarButton For Each btn In...
6
4276
by: j2ee.singh | last post by:
Hi, I'm looking to buy a new laptop primarily to learn & practice .NET and C#. My Question is: Is there any requirement for .NET and C# in terms of the following Operating Systems: - Windows Vista Home Basic - Windows Vista Business
4
3100
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these applications into Visual Basic.Net. My managers main thought is that Visual Basic 6 is (or has!) stopped being supported by Microsoft.
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
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.