關於封裝、打包特定谷歌瀏覽器(兩個版本)

1、對應打包代碼:

using System;
using System.Collections.Generic;
using System.Linq;
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;
using MahApps.Metro.Controls;
using System.Windows.Interop;
using System.Threading;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using System.Configuration;
using Microsoft.Win32;

namespace Citms.Chrome.FingerSensor
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow :MetroWindow
    {
        const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
        string DemoUrl = ConfigurationManager.AppSettings["Url"].ToString();
        bool Drive; 


        public MainWindow()
        {
            //此版本存在必須安裝驅動,否則異常(底層封裝基於驅動)
            if (IsRegeditItemExist("ZKSoftware"))
            {
                Drive = true;
            }
            else
            {
                Drive = false;
                MessageBox.Show("請安裝指紋驅動");
            }
            InitializeComponent();
            BrowserManager.Instance.Initialize(this,this.BrowserTabs);
            Browser home = BrowserManager.Instance.CreateBrowser(DemoUrl);
            home.AllowClose = false;
            BrowserManager.Instance.OpenBrowser(home);
            Sensor.Sensor.Instance.SensorCaptureEvent += Instance_SensorCaptureEvent;
            this.Home = home;
            this.Loaded += MainWindow_Loaded;
            this.Closing += MainWindow_Closing;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.SensorForm.Close();
        }

        public Sensor.SensorForm SensorForm = new Sensor.SensorForm();

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.SensorForm.ShowInTaskbar = false;
            this.SensorForm.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.SensorForm.Show();
            this.SensorForm.Visible = false;
            if (Drive)
            {
                //驅動成功安裝
                Sensor.Sensor.Instance.Start();
            }
            else
            {
                //驅動未成功安裝
                MessageBox.Show("驅動未安裝");
            }

        }

        /// <summary>
        /// 註冊表查詢
        /// </summary>
        /// <param name="Key">註冊表名稱</param>
        /// <returns></returns>
        private bool IsRegeditItemExist(string Key)
        {
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE");
            subkeyNames = software.GetSubKeyNames();
            //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中  
            foreach (string keyName in subkeyNames)
            {
                if (keyName == Key) 
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }



        public Browser Home { get; set; }

        //private void Content_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
        //{
        //    // 頁面加載完成
        //}

        private void Instance_SensorCaptureEvent(object sender, Sensor.Sensor sensor)
        {
            // 接收事件。
            string image = sensor.Image;
            string regTemplate = sensor.RegTemplate;
            string script = "receiveFingerData('" + image + "','" + regTemplate + "')";
            this.Home.ExecuteJavaScript(script);
            this.Home.ExecuteFrameJavaScript(script);
        }

        /// <summary>
        /// 刷新按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //首先獲得當前頁面的標籤
            int NowTableId = BrowserManager.Instance.Tabs.SelectedIndex;
            if (BrowserManager.Instance.Browsers[NowTableId].Content.Address != null)
            {
                //獲取當前標籤裝的URL
                string NOWUrl = BrowserManager.Instance.Browsers[NowTableId].Content.Address.ToString();
                BrowserManager.Instance.Browsers[NowTableId].Content.Load(NOWUrl);
            }


        }
    }
}

其中有涵蓋有一個註冊表信息掃描:

        需要引用: using Microsoft.Win32;
        /// <summary>
        /// 註冊表查詢
        /// </summary>
        /// <param name="Key">註冊表名稱</param>
        /// <returns></returns>
        private bool IsRegeditItemExist(string Key)
        {
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE");
            subkeyNames = software.GetSubKeyNames();
            //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中  
            foreach (string keyName in subkeyNames)
            {
                if (keyName == Key) 
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
       }

關於WPF重構對應打開新頁面的方法:

using CefSharp;
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Citms.Chrome.FingerSensor.Handlers
{
    public class LifespanHandler : ILifeSpanHandler
    {
        bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            newBrowser = null;
            ChromiumWebBrowser chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            chromiumWebBrowser.Dispatcher.Invoke(() => {
                Browser mybrowser=BrowserManager.Instance.CreateBrowser(targetUrl);
                BrowserManager.Instance.OpenBrowser(mybrowser);
            });

            return true;
        }

        void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
        {
        }

        bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
        {
            return false;
        }

        void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
        {

        }
    }
}

然後開始我們的VS2017打包程序:

1、創建打包項目:

2、構建打包程序文件

這個位子需要注意添加輸出:

3、創建對應的自定義用戶界面和關聯起來自定義參數

選中install創建自定義文本框

設置對應的自定義文本框(A)的屬性和其中重要的EditProperty屬性的設置:

開始自定義操作:

對應屬性

對應在這之前創造了

在類庫中添加對應的安裝程序類

程序類重構添加對應的方法體和函數對應吧生成的DLL或者exe放入打包中

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using System.Threading;

namespace IPClientSet
{
    [RunInstaller(true)]
    public partial class InstallClassLibrary : System.Configuration.Install.Installer
    {
        public InstallClassLibrary()
        {
            InitializeComponent();

            this.AfterInstall += Installer1_AfterInstall;
            this.BeforeUninstall += Installer1_BeforeUninstall;
        }


        private void Installer1_AfterInstall(object sender, InstallEventArgs e)
        {
            //暫時性封裝沒有特殊需求暫時不用
            ////System.Diagnostics.Debugger.Launch();
            ////獲取用戶設定的安裝目標路徑, 注意,需要在Setup項目裏面自定義操作的屬性欄裏面的CustomActionData添加上/targetdir="[TARGETDIR]\"
            //string installPath = this.Context.Parameters["targetdir"];
            //installPath = installPath.TrimEnd('\\') + "\\";          //後續業務邏輯
            //installPath += "Citms.Chrome.FingerSensor.exe.config";
            //string OpenUrl;
            //SetUrl FormSet = new SetUrl();
            ////this.Owner = FormSet;
            //FormSet.TopMost = true;
            //FormSet.ShowDialog();
            //bool Insta = FormSet.isActive;
            //if (Insta != true)
            //{
            //    FormSet.Owner = FormSet;
            //    FormSet.TopMost = true;
            //    FormSet.Focus();
            //    FormSet.ShowDialog();
            //}
            //else
            //{
            //    FormSet.Close();
            //    OpenUrl = FormSet.BtnOK_Click();
            //    EditConfigVal(installPath, "Url", OpenUrl);
            //}
            string OpenUrl = this.Context.Parameters["openurl"];//服務器名稱
            string installPath = this.Context.Parameters["targetdir"];
            installPath = installPath.TrimEnd('\\') + "\\";          //後續業務邏輯
            installPath += "Citms.Chrome.FingerSensor.exe.config";
            EditConfigVal(installPath, "Url", OpenUrl);

        }
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            string OpenUrl = this.Context.Parameters["openurl"];//服務器名稱
            string installPath = this.Context.Parameters["targetdir"];
            installPath = installPath.TrimEnd('\\') + "\\";          //後續業務邏輯
            installPath += "Citms.Chrome.FingerSensor.exe.config";
            EditConfigVal(installPath, "Url", OpenUrl);
        }



        private void Installer1_BeforeUninstall(object sender, InstallEventArgs e)
        {
            //獲取用戶設定的安裝目標路徑, 注意,需要在Setup項目裏面自定義操作的屬性欄裏面的CustomActionData添加上/targetdir="[TARGETDIR]\"
            string installPath = this.Context.Parameters["targetdir"];
            installPath = installPath.TrimEnd('\\') + "\\";
        }


        public static string GetConfigVal(string filePath, string keyName)
        {
            string val = "";
            XmlDocument doc = new XmlDocument();
            try
            {
                FileInfo fi = new FileInfo(filePath);
                XmlNode node;
                XmlElement element;
                if (fi.Exists)
                {
                    doc.Load(fi.FullName);
                    node = doc.SelectSingleNode("//appSettings");
                    element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                    if (element != null)
                    {
                        val = element.Attributes.Item(1).InnerText;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return val;
        }

        /// <summary>
        /// 修改配置文件信息
        /// </summary>
        /// <param name="filePath">文件路徑</param>
        /// <param name="keyName">配置名稱</param>
        /// <param name="value">配置值</param>
        public static void EditConfigVal(string filePath, string keyName, string value)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(filePath);
                XmlNode node;
                XmlElement element;
                node = doc.SelectSingleNode("//appSettings");
                element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                element.SetAttribute("value", value);
                doc.Save(filePath);
            }
            catch
            {
                throw;
            }
        }

        private void SetUrl_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章