chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
using System;
using System.Runtime.InteropServices;
namespace SolidWorksListener
{
    public class WindowIntPtrUtil
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string className, string windowName);
 
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowByProcessId(int processId, int threadId);
 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr ptr, out uint processId);
 
        /// <summary>
        /// 操作窗口 1是激活 6是最小化
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="command">1是激活 6是最小化</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool ShowWindow(IntPtr ptr, int command);
 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool SendMessage(IntPtr ptr, UInt32 Msg, IntPtr wParam, IntPtr lParam);
 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(IntPtr ptr, UInt32 Msg, IntPtr wParam, IntPtr lParam);
 
        public static void KillProcessByIntPtr(IntPtr ptr)
        {
            //GetWindowThreadProcessId(ptr, out uint processId);
            //if (processId == 0) return;
            //Process process = Process.GetProcessById((int)processId);
            //process.Kill();
            //process.WaitForExit();
            CloseWindow(ptr);
        }
 
        public static void CloseWindow(IntPtr ptr)
        {
            SendMessage(ptr, (UInt32)0x0010, IntPtr.Zero, IntPtr.Zero);
        }
    }
}