This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NativeMethods.cs
62 lines (51 loc) · 1.83 KB
/
NativeMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Runtime.InteropServices;
namespace System_Info
{
internal class NativeMethods
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
public static void SetOnDesktop(IntPtr hWnd)
{
// IntPtr hWnd = new WindowInteropHelper(window).Handle;
SetParent(hWnd, Getparrenthandle());
}
private static IntPtr Getparrenthandle()
{
IntPtr hwndWorkerW = IntPtr.Zero;
IntPtr shellDefView = IntPtr.Zero;
IntPtr hwndDesktop = IntPtr.Zero;
IntPtr progMan = FindWindow("ProgMan", "Program Manager");
if (progMan != IntPtr.Zero)
{
shellDefView = FindWindowEx(progMan, IntPtr.Zero, "SHELLDLL_DefView", null);
if (shellDefView != IntPtr.Zero)
{
hwndDesktop = FindWindowEx(shellDefView, IntPtr.Zero, "SysListView32", null);
}
}
while (hwndDesktop == IntPtr.Zero)
{
hwndWorkerW = FindWindowEx(IntPtr.Zero, hwndWorkerW, "WorkerW", null);
if (hwndWorkerW == IntPtr.Zero)
{
break;
}
shellDefView = FindWindowEx(hwndWorkerW, IntPtr.Zero, "SHELLDLL_DefView", null);
if (shellDefView == IntPtr.Zero)
{
continue;
}
hwndDesktop = FindWindowEx(shellDefView, IntPtr.Zero, "SysListView32", null);
}
return hwndDesktop;
}
}
}