Hello!
I see 4 windows on the monitor (for example), but EnumWindows gives me more than a dozen - some incomprehensible windows that are not on the screen at all. They names are something like "Microsoft Text Input Application, Microsoft Store, etc"
IsVisible returns true, IsIconic returns false, GetWindowsRect return some rect with non-zero width and heights. So, how I can filter them ?? For now all what I can do - to get image with PrintWindow and to see it is totally black, but this way looks like a gag not solution :)
My task is simple - to get screenshot for all windows on the screen one by one, but it is not very good for me to get additional 10 black rectangles.
To be more informative: OS - Windows10
What I have tried:
class
OpenWindowsGetter
[DllImport(
"
USER32.DLL"
)]
static
extern
bool
EnumWindows(EnumWindowsProc enumFunc,
int
lParam);
[DllImport(
"
USER32.DLL"
)]
static
extern
int
GetWindowText(
IntPtr
hWnd, StringBuilder lpString,
int
nMaxCount);
[DllImport(
"
USER32.DLL"
)]
static
extern
int
GetWindowTextLength(
IntPtr
hWnd);
[DllImport(
"
USER32.DLL"
)]
static
extern
bool
IsWindowVisible(
IntPtr
hWnd);
[DllImport(
"
USER32.DLL"
)]
static
extern
IntPtr
GetShellWindow();
[DllImport(
"
User32.dll"
)]
static
extern
bool
IsIconic(
IntPtr
hwnd);
delegate
bool
EnumWindowsProc(
IntPtr
hWnd,
int
lParam);
public
static
IDictionary<IntPtr, string> GetOpenWindows()
IntPtr
lShellWindow = GetShellWindow();
Dictionary<IntPtr, string> lWindows =
new
Dictionary<IntPtr, string>();
EnumWindows(
delegate
(
IntPtr
hWnd,
int
lParam)
if
(hWnd == lShellWindow)
return
true
;
if
(!IsWindowVisible(hWnd))
return
true
;
if
(IsIconic(hWnd))
return
true
;
int
lLength = GetWindowTextLength(hWnd);
if
(lLength ==
0
)
return
true
;
StringBuilder lBuilder =
new
StringBuilder(lLength);
GetWindowText(hWnd, lBuilder, lLength +
1
);
lWindows[hWnd] = lBuilder.ToString();
return
true
;
},
0
);
return
lWindows;
And to see a result with combobox WindsCB:
private
void
button1_Click(
object
sender, EventArgs e)
WindsCB.Items.Clear();
IDictionary<IntPtr, string> winds = OpenWindowsGetter.GetOpenWindows();
foreach
(
var
item
in
winds) WindsCB.Items.Add(item);
And the remarks section for the IsWindowVisible function (winuser.h) - Win32 apps | Microsoft Docs [ ^ ] function states: