You are on page 1of 3

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.Text;

using HWND = System.IntPtr;

namespace SSMS_Window_Fixer

public static class Win32Api

[StructLayout(LayoutKind.Sequential)]

public struct Rect

public int Left { get; set; }

public int Top { get; set; }

public int Right { get; set; }

public int Bottom { get; set; }

public override string ToString()

//return $"Top: {Top}, Left: {Left}, Bottom: {Bottom}, Right: {Right}";

return $"Left: {Left}, Right: {Right}, Top: {Top}, Bottom: {Bottom}";

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string strClassName, string strWindowName);

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);

[DllImport("user32.dll", SetLastError = true)]

public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool
bRepaint);

[DllImport("USER32.DLL")]

private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

[DllImport("USER32.DLL")]

private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("USER32.DLL")]

private static extern int GetWindowTextLength(HWND hWnd);

[DllImport("USER32.DLL")]

private static extern bool IsWindowVisible(HWND hWnd);

[DllImport("USER32.DLL")]

private static extern IntPtr GetShellWindow();

private delegate bool EnumWindowsProc(HWND hWnd, int lParam);

/// <summary>Returns a dictionary that contains the handle and title of all the open
windows.</summary>

/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>

public static IDictionary<HWND, string> GetOpenWindows()

HWND shellWindow = GetShellWindow();


Dictionary<HWND, string> windows = new Dictionary<HWND, string>();

EnumWindows(delegate (HWND hWnd, int lParam)

if (hWnd == shellWindow) return true;

if (!IsWindowVisible(hWnd)) return true;

int length = GetWindowTextLength(hWnd);

if (length == 0) return true;

StringBuilder builder = new StringBuilder(length);

GetWindowText(hWnd, builder, length + 1);

windows[hWnd] = builder.ToString();

return true;

}, 0);

return windows;

You might also like