关于封装、打包特定谷歌浏览器(两个版本)

来源:csdn博客 分类: 文章浏览史 发布时间:2020-05-30 13:58:42 最后更新:2020-05-30 浏览:278
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-05-30 13:58:42

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)
        {

        }
    }
}

 

php技术微信