diff --git a/OnTopReplica/Native/WindowMethods.cs b/OnTopReplica/Native/WindowMethods.cs index 7f65543..3b12152 100644 --- a/OnTopReplica/Native/WindowMethods.cs +++ b/OnTopReplica/Native/WindowMethods.cs @@ -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); + + /// + /// Gets a window's text via API call. + /// + /// Window handle. + /// Title of the window. + 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), diff --git a/OnTopReplica/WindowManager.cs b/OnTopReplica/WindowManager.cs index d912c4e..0af3ba9 100644 --- a/OnTopReplica/WindowManager.cs +++ b/OnTopReplica/WindowManager.cs @@ -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 } }