28-12-2015 19:13:32
C# / Interop
0 Bookmark(s)
215 View(s)
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern int RegisterWindowMessage(string lpString);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)] //
public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
public string GetWindowText(IntPtr hWnd)
{
var titleBuffer = new StringBuilder();
var length = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();
if (length > 0)
{
titleBuffer = new StringBuilder(length + 1);
SendMessage(hWnd, WM_GETTEXT, titleBuffer.Capacity, titleBuffer);
}
return titleBuffer.ToString();
}