公用的cefsharp窗口

書接上回,.net實現一個nw,一個字,簡單。html

結構,無廢話,上圖。vue

 

要說這部分上回展現過的,大體結構如此,其實要說清楚結構,仍是得從工做流程開始提及json

流程後端

  1.經過桌面的快捷方式啓動WebOnDesk, 攜帶參數,目標客戶文件夾的名字,這個文件夾由於是在本機xampp服務器上,因此也能夠拼接出相似localhost/xxx的形式,用做界面瀏覽器

  2.啓動搜索參數指向的文件目錄服務器

    1)xxxx.dll,與文件名同名的客戶端邏輯dll,動態掛載,全部的業務邏輯,後端支持均可以由它來完成,nw裏的package.json完成的那點事,都是分分鐘完成的。強大的一批,由於他的擴展是無窮的。固然也能夠沒有這個dll,由於js自己也能夠寫不少邏輯在裏面。因此,這個很重要的東西,也是可選的,由於,可能不操做本地,直接操做網絡上的東西。網絡

    2)圖標,這都是小菜一碟,順手的事兒。ide

  3.cefsharp加載拼接好的localhost/xxx或者dll內部指向的其餘外網網頁(不過,這就至關於瀏覽器了,無趣)。lua

  4.鬼喲,完事兒了,想幹啥幹啥吧。url

  短小精煉,無廢話,連錯誤處理均可以省略

    

稍微展現一下代碼,別學我,我這隨便寫寫的。

主窗口代碼

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那點兒事兒吧。

相關文章
相關標籤/搜索