Author:水如烟
现在实现“编辑”菜单组的功能。
要点:菜单可用性的检测及功能实现。
在这里,项目需要引用COM: Microsoft Internet Controls
文件:WebBrowser.Com.vb
Namespace LzmTW.uSystem.uWindows.uForm.Web
Partial Class WebBrowser
Public Event CanCutChanged As EventHandler
Public Event CanCopyChanged As EventHandler
Public Event CanPasteChanged As EventHandler
Public Sub ShowViewSourceDialog()
ComDocumentExec(DocumentCmdAction.ViewSource)
End Sub
Public Sub ShowFindDialog()
ComDocumentExec(DocumentCmdAction.Find)
End Sub
Public Sub ShowInternetOptionsDialog()
ComDocumentExec(DocumentCmdAction.InternetOptions)
End Sub
Public Sub Cut()
ComInternetExplorerExecWB(SHDocVw.OLECMDID.OLECMDID_CUT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
End Sub
Public Sub Copy()
ComInternetExplorerExecWB(SHDocVw.OLECMDID.OLECMDID_COPY, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
End Sub
Public Sub Paste()
ComInternetExplorerExecWB(SHDocVw.OLECMDID.OLECMDID_PASTE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
End Sub
Public Sub SelectAll()
ComInternetExplorerExecWB(SHDocVw.OLECMDID.OLECMDID_SELECTALL, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
End Sub
Public ReadOnly Property CanCut() As Boolean
Get
Return gStatusList(SHDocVw.OLECMDID.OLECMDID_CUT)
End Get
End Property
Public ReadOnly Property CanCopy() As Boolean
Get
Return gStatusList(SHDocVw.OLECMDID.OLECMDID_COPY)
End Get
End Property
Public ReadOnly Property CanPaste() As Boolean
Get
Return gStatusList(SHDocVw.OLECMDID.OLECMDID_PASTE)
End Get
End Property
Private Function StatusIsChanged(ByVal cmdID As SHDocVw.OLECMDID) As Boolean
Dim mCurrentStatus As Boolean = _
(Me.gComInternetExplorer.QueryStatusWB(cmdID) And SHDocVw.OLECMDF.OLECMDF_ENABLED) = SHDocVw.OLECMDF.OLECMDF_ENABLED
If mCurrentStatus <> gStatusList(cmdID) Then
gStatusList(cmdID) = mCurrentStatus
Return True
End If
Return False
End Function
Private Sub gComInternetExplorer_CommandStateChange(ByVal Command As Integer, ByVal Enable As Boolean) _
Handles gComInternetExplorer.CommandStateChange
If Me.StatusIsChanged(SHDocVw.OLECMDID.OLECMDID_CUT) Then
RaiseEvent CanCutChanged(Me.Document, New EventArgs)
End If
If Me.StatusIsChanged(SHDocVw.OLECMDID.OLECMDID_PASTE) Then
RaiseEvent CanPasteChanged(Me.Document, New EventArgs)
End If
If Me.StatusIsChanged(SHDocVw.OLECMDID.OLECMDID_COPY) Then
RaiseEvent CanCopyChanged(Me.Document, New EventArgs)
End If
End Sub
''以下为辅助定义
Private gStatusList As New Dictionary(Of SHDocVw.OLECMDID, Boolean)
Private ReadOnly Property ComDocument() As IOleCommandTarget
Get
Return CType(Me.gComInternetExplorer.Document, IOleCommandTarget)
End Get
End Property
Private cmdGUID As New Guid("ED016940-BD5B-11CF-BA4E -00C 04FD70816")
<StructLayout(LayoutKind.Sequential)> _
Private Structure OLECMDTEXT
Public cmdtextf As Integer
Public cwActual As Integer
Public cwBuf As Integer
Public rgwz As Char
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure OLECMD
Public cmdID As Integer
Public cmdf As Integer
End Structure
<ComImport(), Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Private Interface IOleCommandTarget
Sub QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As Integer, <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=1)> ByVal prgCmds As OLECMD, ByRef pCmdText As OLECMDTEXT)
Sub Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdId As Integer, ByVal nCmdExecOpt As Integer, ByRef pvaIn As Object, ByRef pvaOut As Object)
End Interface
Private Sub ComInternetExplorerExecWB(ByVal cmdID As SHDocVw.OLECMDID, ByVal opt As SHDocVw.OLECMDEXECOPT)
Try
Me.gComInternetExplorer.ExecWB(cmdID, opt)
Catch ex As Exception
End Try
End Sub
Private Sub ComDocumentExec(ByVal cmdID As Integer)
Try
ComDocument.Exec(cmdGUID, cmdID, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, Nothing, Nothing)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
Private Enum DocumentCmdAction
Find = 1
ViewSource
InternetOptions
End Enum
End Class
End Namespace
主文件WebBrowser.vb添加初始化代码:
Private WithEvents gComInternetExplorer As SHDocVw.InternetExplorer
Sub New()
Me.Url = New System.Uri("about:blank", System.UriKind.Absolute)
Me.gComInternetExplorer = CType(Me.ActiveXInstance, SHDocVw.InternetExplorer)
With gStatusList
.Add(SHDocVw.OLECMDID.OLECMDID_CUT, False)
.Add(SHDocVw.OLECMDID.OLECMDID_COPY, False)
.Add(SHDocVw.OLECMDID.OLECMDID_PASTE, False)
End With
End Sub
在WinMenuStrip类中再添加有关代码。这里略。
现在的代码下载