当前位置: 移动技术网 > IT编程>开发语言>.net > WPF用户控件库 嵌入外部(VLC)exe

WPF用户控件库 嵌入外部(VLC)exe

2019年02月28日  | 移动技术网IT编程  | 我要评论

金石良缘主题曲,崖柏手串,理财交流

综合网上资源完成的自己的第一篇博客

------------------------------------------------------------------------

网上类似的贴子挺多的,由于情况不太一样。网上相关帖子都是在 mainwindow 嵌入。我需要在原有客户端上开发新的插件即用户控件库实现嵌入外部exe。

主要问题:获取不到窗口句柄。

 

1、利用系统api实现嵌入。

1         [dllimport("user32.dll", setlasterror = true)]
2         public static extern long setparent(intptr hwndchild, intptr hwndnewparent);
3 
4         [dllimport("user32.dll")]
5         public static extern bool showwindowasync(intptr hwnd, int ncmdshow);

2、当时在获取页面(用户控件库)的句柄问题上碰壁,主要思路是在页面上加border、grid等类似的容器控件。然后可通过程序集“presentationcore”里的方法获取。

但需要注意的是,不能在页面加载过程中获取句柄。可在button的click事件触发、容器控件的load事件触发、、、

 1 using system;
 2 using system.diagnostics;
 3 using system.windows;
 4 using system.windows.interop;
 5 using system.reflection;
 6 using system.io;
 7 
 8 namespace alarmcenter.addin.runvlc
 9 {
10     /// <summary>
11     /// homepage.xaml 的交互逻辑
12     /// </summary>
13     public partial class homepage
14     {
15         string rootpath;
16         //定义变量
17         private intptr prsmwh;//外部exe文件运行句柄
18         private process process;//外部exe文件对象
19         public homepage()
20         {
21             initializecomponent();
22             rootpath = alarmcenter.core.general.getapplicationrootpath();//获取相对可执行文件路径
23         }
24 
25         public void runvlc()
26         {
27             //获取当前窗口句柄
28             intptr handle = ((hwndsource)presentationsource.fromvisual(bd_test)).handle;
29 
30             string path = null;
31             var currentassembly = assembly.getentryassembly();
32             var currentdirectory = new fileinfo(currentassembly.location).directoryname;
33             if (currentdirectory == null) return;
34             //初始化配置,指定可执行文件路径
35             if (assemblyname.getassemblyname(currentassembly.location).processorarchitecture == processorarchitecture.x86)
36                 path = rootpath + @"\bin\vlcplayersdk\libvlc_x86\vlc.exe";
37             else
38                 path = rootpath + @"\bin\vlcplayersdk\libvlc_x64\vlc.exe";
39 
40             process = process.start(path, rootpath + "\\bin\\宣传片\\test.mkv -f --no-video-title-show --repeat --no-interact --video-on-top --mouse-hide-timeout=1");
41             prsmwh = process.mainwindowhandle;
42             while (prsmwh == intptr.zero)
43             {
44                 prsmwh = process.mainwindowhandle;
45             }
46             //设置父窗口
47             sdk.setparent(prsmwh, handle);
48             sdk.showwindowasync(prsmwh, 3);//子窗口最大化 
49         }
50 
51         public override void dispose()
52         {
53             process?.kill();
54             base.dispose();
55         }
56 
57         private void bd_test_loaded(object sender, routedeventargs e)
58         {
59             runvlc();
60         }
61     }
62 }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网