How to get the title of currently active window in VB.Net

In a previous post, i’ve covered how to get the title of currently active window in VB6. Here is code to do the same task in VB.Net

All about you do is the following steps:

  1. Get the handle to currently active window using GetForegroundWindow()
  2. Get the text length of the window’s title using GetWindowTitleLength().
  3. Get the Window’s title using GetWindowTitle().

Code:

Place this code at the top of the form. This is used to import System.Runtime.InteropServices to enable the DllImport function

Place this code inside the form. These are the Window API functions we use. We should declare it before using.

Place this code inside the form. This is our main function. You’ll call this function to get the title of currently active window

How to use:

It is as simple as calling our function

 

2 comments

  1. That will always give you the title of the form1 (vb application) because once you click at the button, the form is focused, so the best solution is doing that in timer_tick,

Leave a Reply

Your email address will not be published. Required fields are marked *