using Container.Common; using Container.Model; using Container.Services; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Container.ChildWindows { /// /// UCAppButton.xaml 的交互逻辑 /// public partial class UCAppButton : UserControl { [DllImport("user32.dll", SetLastError = true)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); const int GWL_HWNDPARENT = -8; [DllImport("user32.dll")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); public static int SW_SHOW = 5; public static int SW_NORMAL = 1; public static int SW_MAX = 3; public static int SW_HIDE = 0; public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶 public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶 public static readonly IntPtr HWND_BOTTOM = new IntPtr(1); //取消窗体置顶 public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置 public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小 Class_Log log = new Class_Log(); App app = ((App)System.Windows.Application.Current); string ProcessName = ""; AppListModel AppInfo = new AppListModel(); public UCAppButton(AppListModel ExeInfo) { InitializeComponent(); AppInfo = ExeInfo; //this.DataContext = ExeInfo; //this.mask.DataContext = ExeInfo; Uri uri = new Uri(ExeInfo.Logo, UriKind.Relative); //ImageBrush ib = new ImageBrush(); //image.Source = new BitmapImage(uri); //Uri uri = new Uri(logo, UriKind.Absolute); this.image.Source = BitmapFrame.Create(uri); txtName.Text = ExeInfo.Name; this.MouseLeftButtonUp += new MouseButtonEventHandler(this_MouseLeftButtonDown); } private void this_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Window w = ((Canvas)(((Canvas)this.Parent).Parent)).Parent as Window; w.Close(); //打开应用 try { app.appClick = true; if (!AppInfo.Code.Equals(app.AppPlayinfo.Code)) { if (app.NowAppType != "html" && app.NowAppType != "bat" && app.NowAppType != "") { killExE(); } ////else if (app.NowAppType == "html") ////{ //// app.winh5.WinClose(); //// //app.winh5 = null; ////} string fileName = AppInfo.Startup; //if (string.IsNullOrEmpty(AppInfo.Startup)) //{ // fileName = AppInfo.Startup; //} //else //{ // fileName = AppInfo.exestartname + "/" + AppInfo.exestartname; //} if (StartExE(fileName)) { app.AppPlayinfo = AppInfo; //app.AppPlayName = ProcessName; //app.AppPlayNameCH = ""; //app.Appversion = ""; IntPtr Win = FindWindow(null, "WindowButton"); //CustomBar是我的程序中需要置顶的窗体的名字 if (Win != null) { SetWindowPos(Win, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } else { if (app.AppList != null && app.AppList.Count > 1) { WindowButton win = new WindowButton(); win.Show(); } } } else { app.AppPlayName = ""; app.CurAppPlayName = app.AppPlayName; app.AppPlayNameCH = ""; app.Appversion = ""; //打开失败 } } } catch(Exception ex) { // MessageBox.Show(ex.ToString()); } } /// /// 打开外部exe应用程序 /// 应用程序地址 /// private bool StartExE(string path,string param = "") { try { if (!File.Exists(path)) { //System.Windows.MessageBox.Show("应用程序打开失败:。。。" + path + " 文件不存在"); log.WriteLogFile("应用程序打开失败:" + path + " 文件不存在","UCAppButtonlog"); return false; } if (path.Substring(path.Length - 4, 4).ToLower().Equals(".bat")) { param = ""; Process myprocess = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(path, param); path = path.Replace("\\", "/"); startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录 startInfo.Verb = "runas"; startInfo.CreateNoWindow = true; myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; myprocess.Start(); //MyProcessName.Add("chrome"); app.AppPlayName = "chrome"; app.CurAppPlayName = app.AppPlayName; app.NowAppType = "bat"; //startexe = true; //timer.Start(); log.WriteLogFile("打开成功111", "startlog"); } else { Process myprocess = new Process(); path = path.Replace("\\", "/").Replace("//", "/"); ProcessStartInfo startInfo = new ProcessStartInfo(path, param); startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录 startInfo.Verb = "runas"; startInfo.CreateNoWindow = true; myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; log.WriteLogFile(startInfo.WorkingDirectory); log.WriteLogFile(startInfo.FileName); myprocess.Start(); //MyProcessName.Add(myprocess.ProcessName); app.NowAppType = "exe"; app.AppPlayName = myprocess.ProcessName; app.CurAppPlayName = app.AppPlayName; //startexe = true; } //Process myprocess = new Process(); //ProcessStartInfo startInfo = new ProcessStartInfo(path, param); //path = path.Replace("\\", "/"); //startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录工作 //myprocess.StartInfo = startInfo; //myprocess.StartInfo.UseShellExecute = false; //myprocess.Start(); //ProcessName = myprocess.ProcessName; return true; } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "UCAppButtonlog"); return false; } } /// /// 关闭已打开外部应用程序 /// private bool killExE() { try { if (app.NowAppType == "exe") { Process[] pro = Process.GetProcesses(); for (int i = 0; i < pro.Length; i++) { if (pro[i].ProcessName.ToLower() == app.AppPlayName.ToLower()) { pro[i].Kill();//结束进程 return true; } } } else if (app.NowAppType == "html") { //this.Dispatcher.Invoke(new Action(() => //{ // try // { // if (app.winh5 != null) // app.winh5.Close(); // } // catch { } //})); } } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "UCAppButtonlog"); } return false; } } }