1. 程式人生 > >WPF用戶控件庫 嵌入外部(VLC)exe

WPF用戶控件庫 嵌入外部(VLC)exe

mds 初始 part 可執行 space 設置 ace 過程 inf

原文:WPF用戶控件庫 嵌入外部(VLC)exe

綜合網上資源完成的自己的第一篇博客

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

網上類似的貼子挺多的,由於情況不太一樣。網上相關帖子都是在 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 }

WPF用戶控件庫 嵌入外部(VLC)exe