在vb中实现真正锁定的带自定义菜单的文本控件
///
///这个东西的出台,是由于一个网友的帖子,太气人,我才写的,很匆忙,又什么问题,请指出!谢谢
//QQ:9181729/mail:shawfile@163.net/http://shawls.yeah.net
///
vb中的textbox控件,虽然可以设置locked属性来实现对文本的锁定,但是,如果用户使用右键菜单,那么就不起作用了,仍然可以对文本进行编辑,有没有办法使用户不能对文本框进行编辑而且替换掉控件的那个右键菜单呢?
完整实现代码如下:
’核心代码的代码
'uTextBox 是你要屏蔽的文本控件 m_Menu 是你要弹出的菜单 m_Read标记是否锁定
Private Sub uTextBox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And m_Read = True Then
OldWindowProc = GetWindowLong(uTextBox.hWnd, GWL_WNDPROC)
' 取得窗口函数的地址
Call SetWindowLong(uTextBox.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage)
' 用SubClass_WndMessage代替窗口函数处理消息
End If
End Sub
Private Sub uTextBox_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And m_Read = True Then
Call SetWindowLong(uTextBox.hWnd, GWL_WNDPROC, OldWindowProc)
' 恢复窗口的默认函数
' 弹出自定义菜单
If Not m_Menu Is Nothing Then
If TypeOf m_Menu Is Menu Then
PopupMenu m_Menu
End If
End If
End If
End Sub
'以下放置在模块中
Public 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
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Function SubClass_WndMessage(ByVal hWnd As OLE_HANDLE, ByVal Msg As OLE_HANDLE, ByVal wp As OLE_HANDLE, ByVal lp As Long) As Long
If