Window Handle

Hello, I come from a Visual Basic background (Visual Studio 2010), and I would like to know how to get the window handle in Xojo. I’m assuming the programming language is RBasic. Here’s what I did in Visual Studio to get the window handle.

Dim hDC As IntPtr
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
'This is for the button click
 Dim hWnd As IntPtr = FindWindow(vbNullString, "Window Title")
        hDC = GetDC(hWnd)
MsgBox("Window Handle Success")


Check out the Window.Handle property. :slight_smile:

It may be stating the obvious, but doesn’t window.handle work for you?

Oh, will that work with a 3rd party application? For instance, If I want to get the notepad handle, won’t I need to use some sort of GetHandleCode and then use Window.Handle?

For other apps, FindWindow is right.

Ah you want to get the handle of a window in a different process
Then no Window.Handle wont do that

This may already be implemented in the Windows Functionality Suite -

[quote=17971:@Norman Palardy]
This may already be implemented in the Windows Functionality Suite -[/quote]

It is, in the UIExtrasWFS.FindWindowHandleFromPartialTitle method. I also created a class to help me manipulate such windows, it’s here.

Ahh yes, that looks about right. Ok, I downloaded the zip archive and opened it in xojo, and I see:

UI Extras

Classes - Modules - Interfaces - System Information

I went through each folder, but I don’t see a “FindWindowHandleFromPartialTitle” method.

It’s there:

Thanks, didn’t know I could access it from the webpage lol. Ok, I added the code, and it automatically added it as a “Method”. That’s much easier than visual studio lol, when I first started in VS, I had no clue where to put declarations. Ok, so this is what I added to a button click:

Dim FindWindowHandleFromPartialTitle As String = ("Untitled - Notepad")

I didn’t get any errors upon launching, so I assume it’s correct. It is correct right lol? Umm, in VS, I can use Try/Catch blocks to catch errors. For instance, if the notepad window wasn’t open, I would do:

FindWindow("Untitled - Notepad")
Catch EX As Exception
MsgBox("Window handle doesn't exist/notepad not open")
End Try

Something like that, typed it from memory. I see xojo has try/catch blocks also, but just wondering if I use it the same way. Thanks for the support guys, quickest response forum I’ve ever been on lol.

Have a look at the documentation . Xojo has Try/Catch blocks as well.

This is incorrect: it creates a string variable named “FindWindowHandleFromPartialTitle” containing the string “Untitled - Notepad”.

This creates an integer variable and stores the window handle returned from the WinAPI function. A value less than or equal to zero means there are no windows matching that title:

Dim HWND As Integer = UIExtrasWFS.FindWindowHandleFromPartialTitle("Untitled - Notepad")

Ahh ok, so I got that all working; thanks! Ok, this should be my last question regarding this topic. I want to get the pixel color of coordinate 266,182 of the 3rd party window. In VB.NET, I use “GetPixel” like this:

If GetPixel(hWnd/HDC,266,182) = &HFFFFFF Then
MessageBox.Show("The pixel is there!")
MessageBox.Show("Pixel not found!:)

Now, in Xojo, I’ve read that the fastest way to do this (yes I need the best/fastest way), is to use RGBSurface. However, in RGBSurface, I only see examples for setting the pixel of a picturebox on the form. I’m not quite used to this new software yet, so I haven’t quite figured out how to interchange between codes lol…

Ok, I experimented with RGBSurface with the picturebox, and it’s exactly what I want. Although I still don’t know how to work it with another window. May I ask what API was used to build RGBSurface, if any?