mirror of
https://github.com/LorenzCK/OnTopReplica.git
synced 2024-05-20 20:33:06 +12:00
Refactored GetWindowText native API call.
This commit is contained in:
parent
41d4c479a0
commit
b1696f4dd8
|
@ -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),
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue