转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
在vb中实现真正锁定的带自定义菜单的文本控件         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1496 更新时间:2009/4/23 16:38:07

     在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.00
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

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台