forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowHandle.cs
More file actions
68 lines (60 loc) · 1.68 KB
/
WindowHandle.cs
File metadata and controls
68 lines (60 loc) · 1.68 KB
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
63
64
65
66
67
68
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace CADSnoop.Model
{
public static class WindowHandle
{
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
/// <summary>
/// Sets the given window's owner to Cad window.
/// </summary>
/// <param name="dialog">Target window.</param>
public static void SetCadAsWindowOwner(this Window dialog)
{
if (null == dialog) { return; }
WindowInteropHelper helper = new WindowInteropHelper(dialog);
helper.Owner = FindCadWindowHandle();
}
/// <summary>
/// Finds the Cad window handle.
/// </summary>
/// <returns>Revit window handle.</returns>
public static IntPtr FindCadWindowHandle()
{
try
{
IntPtr foundRevitHandle = IntPtr.Zero;
uint currentThreadID = GetCurrentThreadId();
// Search for the Revit process with current thread ID.
Process[] revitProcesses = Process.GetProcessesByName("acad");
Process foundRevitProcess = null;
foreach (Process aRevitProcess in revitProcesses)
{
foreach (ProcessThread aThread in aRevitProcess.Threads)
{
if (aThread.Id == currentThreadID)
{
foundRevitProcess = aRevitProcess;
break;
}
} // For each thread in the process.
// When we have found our Revit process, then stop searching.
if (null != foundRevitProcess) { break; }
} // For each revit process found
if (null != foundRevitProcess)
{
foundRevitHandle = foundRevitProcess.MainWindowHandle;
}
return foundRevitHandle;
}
catch (Exception)
{
return IntPtr.Zero;
}
}
}
}