码迷,mamicode.com
首页 > 其他好文 > 详细

公用的cefsharp窗口

时间:2018-09-17 16:11:47      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:enable   desktop   amp   业务   对象   localhost   doc   启动引导   cto   

书接上回,.net实现一个nw,一个字,简单。

结构,无废话,上图。

 技术分享图片

要说这部分上回展示过的,大致结构如此,其实要说清楚结构,还是得从工作流程开始说起

流程

  1.通过桌面的快捷方式启动WebOnDesk, 携带参数,目标客户文件夹的名字,这个文件夹因为是在本机xampp服务器上,所以也可以拼接出类似localhost/xxx的形式,用作界面

  2.启动搜索参数指向的文件目录

    1)xxxx.dll,与文件名同名的客户端逻辑dll,动态挂载,所有的业务逻辑,后端支持都可以由它来完成,nw里的package.json完成的那点事,都是分分钟完成的。强大的一批,因为他的扩展是无穷的。当然也可以没有这个dll,因为js本身也可以写很多逻辑在里面。所以,这个很重要的东西,也是可选的,因为,可能不操作本地,直接操作网络上的东西。

    2)图标,这都是小菜一碟,顺手的事儿。

  3.cefsharp加载拼接好的localhost/xxx或者dll内部指向的其他外网网页(不过,这就相当于浏览器了,无趣)。

  4.鬼哟,完事儿了,想干啥干啥吧。

  短小精炼,无废话,连错误处理都可以省略

  技术分享图片  

稍微展示一下代码,别学我,我这随便写写的。

主窗口代码

Imports CefSharp
Imports CefSharp.WinForms

Public Class Form1

    Public WorkPath As IO.DirectoryInfo
    Private WithEvents Cef As ChromiumWebBrowser
    Private WithEvents Methods As CustomerMethods.Class1

    Public Const WM_DRAWCLIPBOARD = &H308

    Private Declare Sub SwitchToThisWindow Lib "User32.dll" Alias "SwitchToThisWindow" (ByVal hWnd As Integer, ByVal bRestore As Integer)

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            Methods.ending()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        启动参数分析
        WorkPath = New IO.DirectoryInfo(Microsoft.VisualBasic.Command)
        If WorkPath.Exists = False Then
            MsgBox("启动目录不存在,请检查快捷方式参数")
            Close()
        End If

        加载用户方法Dll
        Dim fi As New IO.FileInfo(WorkPath.FullName + "\" + WorkPath.Name + ".dll")
        If fi.Exists Then
            Try
                Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(WorkPath.FullName + "\" + WorkPath.Name + ".dll")
                Dim classtemp As Type = asm.GetType(WorkPath.Name + ".Class1")
                Methods = asm.CreateInstance(classtemp.FullName)
                Methods.MainForm = Me
            Catch ex As Exception
                MsgBox("加载用户Dll错误")
            End Try
        Else
            Methods = New DefMethod
        End If
      
        创建桌面快捷方式
        CreateLnk(WorkPath)

        图标设置
        Try
            Me.Icon = New Icon(WorkPath.FullName + "\favicon.ico")
        Catch ex As Exception
        End Try

        标题
        Me.Text = WorkPath.Name

        启动引导前项
        Try
            Methods.init()
        Catch ex As Exception
            MsgBox("Method 初始化错误:" + ex.Message)
        End Try
        启动
        If Methods.StartPath.Length = 0 Then
            Methods.StartPath = "localhost/" + WorkPath.Name
        End If

        CefSharp.Cef.Initialize(New CefSharp.CefSettings)
        CefSharp.CefSharpSettings.LegacyJavascriptBindingEnabled = True

        Cef = New ChromiumWebBrowser(Methods.StartPath)

        注册js对象
        Try
            Dim sett As New BrowserSettings
            sett.WebSecurity = CefState.Disabled
            Cef.BrowserSettings = sett
            Cef.RegisterAsyncJsObject("wnd", Methods)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Controls.Add(Cef)
        Cef.Dock = DockStyle.Fill
    End Sub

    Public Sub CreateLnk(path As IO.DirectoryInfo)

        Dim wsh As Object = CreateObject("WScript.Shell")

        Dim Lnk As Object = wsh.CreateShortcut("C:\Users\Administrator\Desktop\" & path.Name & ".lnk")
        With Lnk
            .arguments = path.FullName
            .iconlocation = path.FullName + "\favicon.ico"
            .targetpath = Application.StartupPath + "\WebOnDisk.exe "
            .workingdirectory = Application.StartupPath
            .save()
        End With

    End Sub


    Private Sub Methods_CallJsEvent(methodname As String, args() As String) Handles Methods.CallJsEvent
        Try
            Cef.EvaluateScriptAsync(methodname, args)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Methods_LoadEvent(url As String) Handles Methods.LoadEvent
        Cef.Load(url)
    End Sub

    Private Sub Methods_SetDevToolsEvent(isShow As Boolean) Handles Methods.SetDevToolsEvent
        If isShow Then
            Cef.ShowDevTools()
        Else
            Cef.CloseDevTools()
        End If
    End Sub

    Private Sub Cef_FrameLoadEnd(sender As Object, e As FrameLoadEndEventArgs) Handles Cef.FrameLoadEnd
        Cef.ShowDevTools()
    End Sub

    Private Sub Methods_SingleInstanceCheck() Handles Methods.SingleInstanceCheck
        单进程检测
        If Methods.IsSingleInstance Then
            Dim ps() As Process = Process.GetProcessesByName(Me.ProductName)
            For Each p As Process In ps
                If p.MainWindowTitle = WorkPath.Name Then

                    SwitchToThisWindow(p.MainWindowHandle, 1)
                    Process.GetCurrentProcess.Kill()
                    Close()
                End If
            Next
        End If
    End Sub
End Class

如果不提供dll,给一个默认的类

Public Class DefMethod
    Inherits CustomerMethods.Class1

    Public Overrides Sub ending()

    End Sub

    Public Overrides Sub init()

    End Sub
End Class

如果要写dll,继承一下这个

Imports System.Web.Script.Serialization
Imports System.Windows.Forms

Public MustInherit Class Class1
    Public StartPath As String = ""
    Public MainForm As System.Windows.Forms.Form
    Public mIsSingleInstance As Boolean = False
    Public Event SingleInstanceCheck()
    Public Property IsSingleInstance
        Get
            Return mIsSingleInstance
        End Get
        Set(value)
            mIsSingleInstance = value
            RaiseEvent SingleInstanceCheck()
        End Set
    End Property

    Public MustOverride Sub init()
    Public MustOverride Sub ending()

    Public Event CallJsEvent(methodname As String, args() As String)
    Public Sub calljs(methodname As String, ParamArray args() As String)

        RaiseEvent CallJsEvent(methodname, args)
    End Sub

    Public Event SetDevToolsEvent(isShow As Boolean)
    
    Public Sub showDevTools()
        RaiseEvent SetDevToolsEvent(True)
    End Sub

    Public Sub closeDevTools()
        RaiseEvent SetDevToolsEvent(False)
    End Sub

    Public Event LoadEvent(url As String)
    Public Sub load(url As String)
        RaiseEvent LoadEvent(url)
    End Sub

    Public Sub SetWindowBounds(bound As Drawing.Rectangle)
        MainForm.Bounds = bound
    End Sub
End Class

html和vue那边的目录结构

技术分享图片

 

大概就这样吧,大体上一个简单的nw就搞出来了,只能说简陋

下回说说vue那点儿事儿吧。

公用的cefsharp窗口

标签:enable   desktop   amp   业务   对象   localhost   doc   启动引导   cto   

原文地址:https://www.cnblogs.com/allofalan/p/9662259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!