winform内部打开微信或者其他EXE程序

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Runtime.InteropServices;
 6 using System.Threading.Tasks;
 7 using System.Windows.Forms;
 8 
 9 namespace WindowsFormsApp1
10 {
11     internal static class Program
12     {
13         /// <summary>
14         /// 应用程序的主入口点。
15         /// </summary>
16         [STAThread]
17         static void Main()
18         {
19             Application.EnableVisualStyles();
20             Application.SetCompatibleTextRenderingDefault(false);
21 
22             // 获取微信的进程
23             var ps = Process.GetProcesses().Where(m=>m.ProcessName=="WeChat").FirstOrDefault();
24             //Process[] ps2 = Process.GetProcessesByName("WeChat");
25             // 获取微信的句柄,intClientWndHandle是父窗口的句柄,需要先得到这个句柄
26             var intClientWndHandle = ProcessEx.GetMainWindowHandle(ps.Id);
27             // 父窗体
28             Form1 frm = new Form1();
29             // 设置当前窗体是微信句柄的父级
30             SetParent(intClientWndHandle, (IntPtr)frm.Handle);
31             // 是否显示控件框
32             frm.ControlBox = false;
33             Application.Run(frm);
34         }
35 
36 
37         [DllImport("user32.dll", EntryPoint = "SetParent")]
38         public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
39 
40         [StructLayout(LayoutKind.Sequential)]
41         public struct ProcessEntry32
42         {
43             public uint dwSize;
44             public uint cntUsage;
45             public uint th32ProcessID;
46             public IntPtr th32DefaultHeapID;
47             public uint th32ModuleID;
48             public uint cntThreads;
49             public uint th32ParentProcessID;
50             public int pcPriClassBase;
51             public uint dwFlags;
52 
53 
54             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
55             public string szExeFile;
56         }
57          
58     }
59 }
复制代码

 

posted @   彪悍的代码不需要注释  阅读(615)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
39
0
点击右上角即可分享
微信分享提示