在vb中实现真正锁定的带自定义菜单的文本控件

    在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 Msg <> WM_CONTEXTMENU Then
        SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
        ' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理
        Exit Function
    End If
    SubClass_WndMessage = True
End Function

'一下是一个完整的自定义的控件的例子“
'下面放置到一个自定义控件中
VERSION 5#
Begin VB.UserControl UTextBox
ClientHeight = 525
ClientLeft = 0
ClientTop = 0
ClientWidth = 1425
ScaleHeight = 525
ScaleWidth = 1425
Begin VB.TextBox uTextBox
Appearance = 0         'Flat
BackColor = &H80000018
Height = 345
Left = 30
TabIndex = 0
Top = 90
Width = 1275
End
End
Attribute VB_Name = "UTextBox"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private m_BackColor As OLE_COLOR   '颜色
Private m_MaxLength As Integer     '长度
Private m_TextAlig As VBRUN.AlignmentConstants  '文本位置
Private m_Menu As Menu     '自定义菜单
Private m_Read As Boolean    '是否可读
Private m_Enabled  As Boolean   'Enabled
'Private m_MulitLine As Boolean '换行
Private Const m_DefBackColor = &H80000018   '默认文本背景颜色
Public Event Change()          '文本修改事件
Public Event Click()           'click事件

Private Sub UserControl_Initialize()
    Call UserControl_Resize
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Let BackColor = PropBag.ReadProperty("BackColor", m_DefBackColor)
    Let MaxLength = PropBag.ReadProperty("MaxLength", 0)
    Let Text = PropBag.ReadProperty("Text", "")
    Let Alignment = PropBag.ReadProperty("Alignment", 0)
    Set Menu = PropBag.ReadProperty("Menu", Nothing)
    Let ReadOnly = PropBag.ReadProperty("Read", False)
    Let Enabled = PropBag.ReadProperty("Enabled", True)
    '    Let uTextBox.MultiLine = PropBag.ReadProperty("MultiLine", False)
    'Let uTextBox.ScrollBars = PropBag.ReadProperty("ScrollBars", 0)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("BackColor", BackColor, m_DefBackColor)
    Call PropBag.WriteProperty("MaxLength", MaxLength, 0)
    Call PropBag.WriteProperty("Text", Text, "")
    Call PropBag.WriteProperty("Alignment", Alignment, 0)
    Call PropBag.WriteProperty("Menu", Menu, Nothing)
    Call PropBag.WriteProperty("Read", ReadOnly, False)
    Call PropBag.WriteProperty("Enabled", Enabled, True)
    'Call PropBag.WriteProperty("MultiLine", MultiLine, False)
    'Call PropBag.WriteProperty("ScrollBars", ScrollBars, 0)
End Sub

Private Sub uTextBox_Click()
    RaiseEvent Click
End Sub

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

Private Sub UserControl_Resize()
    With UserControl
        uTextBox.Move .ScaleLeft, .ScaleTop, .ScaleWidth, .ScaleHeight
    End With
End Sub

Public Property Let BackColor(ByVal vNewValue As OLE_COLOR)
    Let m_BackColor = vNewValue
    Let uTextBox.BackColor = vNewValue
    Let UserControl.BackColor = vNewValue
End Property

Public Property Get BackColor() As OLE_COLOR
    Let BackColor = m_BackColor
End Property

Public Property Let Text(ByVal vNewValue As String)
    Let uTextBox.Text = vNewValue
End Property

Public Property Get Text() As String
    Let Text = uTextBox.Text
End Property

Public Property Let Alignment(ByVal vNewValue As VBRUN.AlignmentConstants)
    Let m_TextAlig = vNewValue
    Let uTextBox.Alignment = vNewValue
End Property

Public Property Get Alignment() As VBRUN.AlignmentConstants
    Let Alignment = m_TextAlig
End Property

Public Property Let ReadOnly(ByVal vNewValue As Boolean)
    Let m_Read = vNewValue
    Let uTextBox.Locked = vNewValue
End Property

Public Property Get ReadOnly() As Boolean
    Let ReadOnly = m_Read
End Property

Private Sub uTextBox_Change()
    RaiseEvent Change
End Sub

Public Property Set Menu(ByRef vNewValue As Menu)
    Set m_Menu = vNewValue
End Property

Public Property Get Menu() As Menu
    Set Menu = m_Menu
End Property

Public Property Let MaxLength(ByVal vNewValue As Integer)
    Let m_MaxLength = vNewValue
    Let uTextBox.MaxLength = vNewValue
End Property

Public Property Get MaxLength() As Integer
    Let MaxLength = m_MaxLength
End Property

Public Property Let SelLength(ByVal vNewValue As Integer)
    'Let m_SelLength = vNewValue
    Let uTextBox.SelLength = vNewValue
End Property

Public Property Get SelLength() As Integer
    Let SelLength = uTextBox.SelLength
End Property

Public Property Let SelStart(ByVal vNewValue As Integer)
    Let uTextBox.SelStart = vNewValue
End Property

Public Property Get SelStart() As Integer
    Let SelStart = uTextBox.SelStart
End Property

Public Property Let SelText(ByVal vNewValue As String)
    Let uTextBox.SelText = vNewValue
End Property

Public Property Get SelText() As String
    Let SelText = uTextBox.SelText
End Property

'Public Property Let MulitLine(ByVal vNewValue As Boolean)
'    Let uTextBox.MultiLine = vNewValue
'End Property

'Public Property Get MultiLine() As Boolean
'    Let MulitLine = uTextBox.MulitLine
'End Property

'Public Property Let ScrollBars(ByVal vNewValue As VBRUN.ScrollBarConstants)
'    Let uTextBox.ScrollBars = vNewValue
'End Property

'Public Property Get ScrollBars() As VBRUN.ScrollBarConstants
'    Let ScrollBars = uTextBox.ScrollBars
'End Property

Public Property Let Enabled(ByVal vNewValue As Boolean)
    Let m_Enabled = vNewValue
    Let uTextBox.Enabled = vNewValue
    Let UserControl.Enabled = vNewValue
End Property

Public Property Get Enabled() As Boolean
    Let Enabled = m_Enabled
End Property

'将前面模块中的代码保留

 '新建一个窗体,看现在这个自定义的真正锁定的文本控件如何?



相关参考


vb屏蔽文本框点右键时的弹出菜单

VB 小技巧自定义TextBox文本框右键菜单

VB 自启动建立右键菜单

VB在菜单上增加图标

vbListBox 之中点击右键弹出菜单

【引用】在VB6.0中实现弹出式菜单的几种方法

vb中实现真正锁定的带自定义菜单的文本控件

记录一下:在菜单上添加自绘图形的例子(VB6代码)


VB相关

vbline的用法[]

画图工具的VB实现

VB 一个获得自己外网 IP 地址的程序代码

VB程序中实现IP地址子网掩码网关DNS的更改  []

VB 中应用 FSO 对象模型介绍(摘自网络)

[] VbFSO 对象的介绍

VB 画坐标轴

VB 二进制文件的操作

[VB]BMPJPGVBKeyCode常数用法

vb实时曲线的绘制和保存

VB操作EXCEL

vb初学回顾:最大公约数 最小公倍数 素数求取

vb 关于窗口样式的API以及处理文本的API参考

【引用】在VB6.0中实现弹出式菜单的几种方法

【引用】URLDownloadToFile_VB下载文件!

利用WinRar压缩和解压缩文件

VB 剪切板

VB实现指示窗口中拖动方框的程序

VB绘制走动的表针

如何用VB制作DLL文件

【引用】VB修改IP地址

VB多窗体退出代码

[]VB:如何检测到U盘的插拔(源代码)

巧用SendMessage函数扩展Treeview功能

vb中如何在任务管理器里面隐藏应用程序进程

如何实现VBEXCEL的无缝连接

一个API方式存取日志文件的模块[VB]

VB用记录集填充表格函数

VB打开文本文件各种方法

vb ClipBoard 剪切板应用(复制剪切粘贴)

【引用】窗口处理技巧大全 vb(窗体控件)

【转】 Md rd命令之VB

vb:读写文本文件

vb中实现真正锁定的带自定义菜单的文本控件

【引用】使用CommonDialogShowSave后如何判断是保存还是取消?

vb 关于commondialog的多选VB获取Windows操作系统所有版本

vb UTF文本文件访问

VB编程中的Unicode vs Ansi

VBPiView4注册机

VB获取超过2G文件的大小

CopyMemory还要快的函数SuperCopyMemory

VB:编程效率快步提高之:十七种可用一行代码完成的技巧

VB画出来的五星红旗

Qt第一印象——QteQt  


更多精彩>>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值