This will get the title of the active window (using vba)
Option Explicit Private Declare Function GetActiveWindow Lib "User32.dll" () As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Function ActiveWindowName() Dim hWnd As Long Dim lngRet As Long Dim strText As String hWnd = GetActiveWindow() strText = String(100, Chr(0)) lngRet = GetWindowText(hWnd, strText, 100) ActiveWindowName=strText End Function
It will return the title in the active window, but I assume that 100 characters will suffice.
This code should give a function that returns the current header, and correctly adjust the length. (I currently don't have C #, so I can't verify this):
[DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); public static string GetActiveWindowText() { IntPtr hWind = GetActiveWindow();
You can then check the string to see what it contains. In the VBA example, input =ActiveWindowName() in A1 returns Microsoft Excel - Book1
Seanc
source share