Z
Z
Zakharov Alexander2017-06-08 01:25:05
Programming
Zakharov Alexander, 2017-06-08 01:25:05

[Solved] C# There is a handle. Need to find out that the handle belongs to the window (form, wpf) but not to the context menu from the tray or taskbar?

Hello.
Actually a subject.
"Need to figure out that handle belongs to a window (form or wpf or dialog) but not to the context menu from the tray or taskbar?"
More user language - you need to check that the foreground window (whose handle I have) is exactly a window, and not some kind of system control such as a context menu in the tray, an icon, or some other control. The GetWindowRect functions don't care what to return when there is a handle, so it doesn't check "if these are coordinates from the window"?
Update Solution:

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern UInt32 GetWindowLong(IntPtr hWnd, int index);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);

private const int GWL_STYLE = -16;
private const int GWL_EXSTYLE = -20;
private const ulong WS_VISIBLE = 0x10000000L;
private const ulong WS_BORDER = 0x00800000L;
private const int WS_EX_TOPMOST = 0x0008;
// https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
private const int 
    WS_EX_APPWINDOW = 0x00040000, 
    WS_EX_TOOLWINDOW = 0x00000080, 
    WS_EX_TRANSPARENT = 0x00000020, 
    WS_EX_WINDOWEDGE = 0x00000100, 
    WS_EX_CLIENTEDGE = 0x00000200, 
    WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE)
    ;
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
private const UInt32 WS_MAXIMIZE = 0x01000000, WS_MINIMIZE = 0x20000000, WS_POPUP = 0x80000000, WS_CAPTION = 0x00C00000, WS_DISABLED = 0x08000000;

IntPtr fore_ptr = GetForegroundWindow();
IntPtr mainWindowHandle = process.MainWindowHandle;
//Console.WriteLine("process.Name:"+process.ProcessName+", fore_ptr:" + fore_ptr+ ", mainWindowHandle:"+ mainWindowHandle);
//fore_ptr = mainWindowHandle;

UInt32 _result_exstyle = 0;
UInt32 _result_style = 0;
bool visible = false;
_result_exstyle = GetWindowLong(startWindowHandle, GWL_EXSTYLE);
_result_style = GetWindowLong(startWindowHandle, GWL_STYLE);

// Определить видимость окна:
if (  (_result_exstyle & WS_EX_TOOLWINDOW)==0 
    && (_result_style & WS_VISIBLE)!=0
    ) {
    visible = true;
}

now visible indicates that the fore_ptr window is visible.
For the convenience of defining window styles, it is convenient to use the Spy ++ tool included in Visual Studio:
cb345b1ffd1947209e9dca6ebe60a9ce.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question