|
-
Nov 23rd, 2003, 01:55 AM
#1
Thread Starter
New Member
VB.NET code to find active window title.
Hi All,
I am looking for VB.NET code which can help me get the active window (the one user has got focus to) title. I know you can do this using Win 32 API like this:
VB Code:
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
Timer1.Interval = 5000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim lWnd As Long
Dim strName As String
lWnd = GetActiveWindow
strName = String(GetWindowTextLength(lWnd) + 1, Chr$(0))
GetWindowText lWnd, strName, Len(strName)
Debug.Print strName
End Sub
but I want to use, Managed code and not API calls.
Do you know of way of achieving the same in Managed code?
Also, is there any event which tells me that Active window has been changed by user??
JD
______________________________
d.k.jariwala
~ simple thought simple act ~
-
Nov 23rd, 2003, 03:15 AM
#2
you will need to use the api's , but you must replace the " Longs " with " Integers " and the String , with StringBuilder. here's a quick example...
VB Code:
[Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetActiveWindow [Color=Blue]Lib[/color] "user32.dll" () [Color=Blue]As[/color] [Color=Blue]Integer
[/color] [Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetWindowText [Color=Blue]Lib[/color] "user32.dll" [Color=Blue]Alias[/color] "GetWindowTextA" ([Color=Blue]ByVal[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color], [Color=Blue]ByVal[/color] lpString [Color=Blue]As[/color] System.Text.StringBuilder, [Color=Blue]ByVal[/color] cch [Color=Blue]As[/color] [Color=Blue]Integer[/color]) [Color=Blue]As[/color] [Color=Blue]Integer
[/color] [Color=Blue]Declare[/color] [Color=Blue]Function[/color] GetWindowTextLength [Color=Blue]Lib[/color] "user32.dll" [Color=Blue]Alias[/color] "GetWindowTextLengthA" ([Color=Blue]ByVal[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color]) [Color=Blue]As[/color] [Color=Blue]Integer
[/color] [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Button1_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] Button1.Click
[Color=Blue]Dim[/color] hwnd [Color=Blue]As[/color] [Color=Blue]Integer[/color] = GetActiveWindow
[Color=Blue]Dim[/color] sBuild [Color=Blue]As[/color] [Color=Blue]New[/color] System.Text.StringBuilder(GetWindowTextLength(hwnd) + 1)
GetWindowText(hwnd, sBuild, sBuild.Capacity)
MessageBox.Show(sBuild.ToString)
[Color=Blue]End[/color] [Color=Blue]Sub[/color]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Nov 23rd, 2003, 08:59 AM
#3
I wonder how many charact
I would think since you can get a handle to all open windows, .net or not with the below code, there should be a way to figure which one has active focus... perhaps the Management namespace contains class or classes than can extract that info.
You may have to buy a $40 book on WMI, because no one seems to post much information on the Net about it.
VB Code:
'form level variable
Private c As Array
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
c = Process.GetProcesses
Dim i As Integer
For i = 0 To c.Length - 1
ListBox1.Items.Add(c(i))
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim a As Process = c(ListBox1.SelectedIndex)
MessageBox.Show(a.MainWindowTitle & " [Handle = " _
& a.MainWindowHandle.ToString & "]")
End Sub
Last edited by nemaroller; Nov 23rd, 2003 at 09:21 AM.
-
Nov 23rd, 2003, 09:53 AM
#4
Thread Starter
New Member
Hi
Dynamic sysop,
Thanks for suggesting the required code changes so as to make it work in .NET. But what I am really looking for is 'managed code' to achieve the same!
And nemaroller,
yes, I looked in to process class. I went through all the properties but couldnt find a property/member/method, which could tell me whether the given process has got user's focus.
and by WMI, you mean 'Windows Management I?????'
Thanks for your help,
JD
______________________________
d.k.jariwala
~ simple thought simple act ~
-
Nov 23rd, 2003, 10:07 AM
#5
I wonder how many charact
yes I meant Windows Management. There may be other ways beside using WMI, but the only thing you lose by using the API is platform independence. And at this time, .Net really only works on Windows systems.
-
Nov 23rd, 2003, 12:44 PM
#6
Thread Starter
New Member
Platform depedence?
It's not my concern anyway. We are 100% Windows shop and I am happy with it! 
Btw, I couldnt find any namespace named 'Windows Management' , could you point me to online MSDN document which talks about it??
Thanks once again for your help!
JD
______________________________
d.k.jariwala
~ simple thought simple act ~
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|