如何在vb里彻底屏蔽右键,各个控件,尤其是webbrowser里不能有右键
3个回答
展开全部
想实现屏蔽Webbrowser里的右键不容易,因为它是IE的控件而且没有提供鼠标判断的事件参数。因此,必须调用系统级别的WinAPI函数,术语叫钩子函数Hook。这种方法屏蔽掉右键与是否用到js就没有关系了。
VB屏蔽右键的代码如下:
Option Explicit
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public OldWindowProc As Long
Public Const WM_CONTEXTMENU = &H7B
Const GWL_WNDPROC = (-4)
Public Function SubClass1_WndMessage(ByVal hWnd As OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wp As OLE_HANDLE, ByVal lp As Long) As Long
If Msg = WM_CONTEXTMENU Then
SubClass1_WndMessage = True
Else
SubClass1_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
End If
End Function
Public Sub Hook(hWnd As Long)
OldWindowProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)
End Sub
Public Sub UnHook(hWnd As Long)
Call SetWindowLong(hWnd, GWL_WNDPROC, OldWindowProc)
End Sub
’窗体------------------
Option Explicit
Private Sub Form_Load()
Hook Me.Webbrowser1.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Me.Webbrowser1.hWnd
End Sub
当然这种方法不仅仅屏蔽Webbrowser里的右键,所有地方都被屏蔽了。因此要有一个判断,仅在指定窗体活动时启用,其它地方不再屏蔽。
VB屏蔽右键的代码如下:
Option Explicit
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public OldWindowProc As Long
Public Const WM_CONTEXTMENU = &H7B
Const GWL_WNDPROC = (-4)
Public Function SubClass1_WndMessage(ByVal hWnd As OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wp As OLE_HANDLE, ByVal lp As Long) As Long
If Msg = WM_CONTEXTMENU Then
SubClass1_WndMessage = True
Else
SubClass1_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
End If
End Function
Public Sub Hook(hWnd As Long)
OldWindowProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)
End Sub
Public Sub UnHook(hWnd As Long)
Call SetWindowLong(hWnd, GWL_WNDPROC, OldWindowProc)
End Sub
’窗体------------------
Option Explicit
Private Sub Form_Load()
Hook Me.Webbrowser1.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Me.Webbrowser1.hWnd
End Sub
当然这种方法不仅仅屏蔽Webbrowser里的右键,所有地方都被屏蔽了。因此要有一个判断,仅在指定窗体活动时启用,其它地方不再屏蔽。
展开全部
Private Sub Text1_Change()
If Text1.Text = "VB" Or Text1.Text = "vb" Then
' SendKeys "{Esc}" '按下ESC
Dim WordObject As Object
Dim WordDoc
Set WordObject = CreateObject("Word.Application")
WordObject.Visible = 1
Set WordDoc = WordObject.Documents.Add
WordObject.Application.Activate
WordDoc.Paragraphs.Last.Range.Text = "你好 VB !"
End If
End Sub
If Text1.Text = "VB" Or Text1.Text = "vb" Then
' SendKeys "{Esc}" '按下ESC
Dim WordObject As Object
Dim WordDoc
Set WordObject = CreateObject("Word.Application")
WordObject.Visible = 1
Set WordDoc = WordObject.Documents.Add
WordObject.Application.Activate
WordDoc.Paragraphs.Last.Range.Text = "你好 VB !"
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要针对JS屏蔽右键 可加我百度HI
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询