Refactored GetWindowText native API call.

This commit is contained in:
Lorenz Cuno Klopfenstein 2010-07-01 01:24:14 +02:00
parent 41d4c479a0
commit b1696f4dd8
2 changed files with 24 additions and 22 deletions

View file

@ -10,10 +10,29 @@ namespace OnTopReplica.Native {
static class WindowMethods {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
static extern int GetWindowTextLength(IntPtr hWnd);
/// <summary>
/// Gets a window's text via API call.
/// </summary>
/// <param name="hwnd">Window handle.</param>
/// <returns>Title of the window.</returns>
public static string GetWindowText(IntPtr hwnd) {
int length = GetWindowTextLength(hwnd);
if (length > 0) {
StringBuilder sb = new StringBuilder(length + 1);
if (WindowMethods.GetWindowText(hwnd, sb, sb.Capacity) > 0)
return sb.ToString();
else
return String.Empty;
}
else
return String.Empty;
}
public enum WindowLong {
WndProc = (-4),

View file

@ -53,7 +53,7 @@ namespace OnTopReplica {
private bool EnumWindowProcAll(IntPtr hwnd, IntPtr lParam) {
if (WindowManagerMethods.IsWindowVisible(hwnd)) {
string title = GetWindowTitle(hwnd);
string title = WindowMethods.GetWindowText(hwnd);
_windows.Add( new WindowHandle(hwnd, title));
}
return true;
@ -63,7 +63,7 @@ namespace OnTopReplica {
if (WindowManagerMethods.IsWindowVisible(hwnd)) {
//Check if window has no parent
if ((long)WindowManagerMethods.GetParent(hwnd) == 0 && WindowManagerMethods.GetDesktopWindow() != hwnd) {
string title = GetWindowTitle(hwnd);
string title = WindowMethods.GetWindowText(hwnd);
_windows.Add( new WindowHandle(hwnd, title));
}
}
@ -81,7 +81,7 @@ namespace OnTopReplica {
//Reject empty titles
string title = GetWindowTitle(hwnd);
string title = WindowMethods.GetWindowText(hwnd);
if (string.IsNullOrEmpty(title))
return true;
@ -100,22 +100,5 @@ namespace OnTopReplica {
return true;
}
#region Auxiliary methods
private string GetWindowTitle(IntPtr hwnd) {
int length = WindowMethods.GetWindowTextLength(hwnd);
if (length > 0) {
StringBuilder sb = new StringBuilder(length + 1);
if (WindowMethods.GetWindowText(hwnd, sb, sb.Capacity) > 0)
return sb.ToString();
else
return String.Empty;
}
else
return String.Empty;
}
#endregion
}
}