vb,如何使处于上面的控件不接受事件,而交由处于下面的控件处理?
现在的问题是:当我点击这个控件时,如果鼠标点到的位置是标签控件所在位置,那么我的 按钮 不会做出任何反应,而点击其他的位置一切正常。我想可能是标签控件把鼠标事件给截获了,而我在设计这个按钮时没有对标签控件的鼠标事件做相应的处理造成的。
我想知道:如何让这个标签控件不截获事件?而是向下传递给usercontrol来处理?就像标签不存在一样。
备注:1. 我的按钮是重量级控件,usercontrol的HitTest 事件不起作用。
2. 我不想一个一个的处理标签的事件,最好整体解决,把这个标签给屏蔽掉。
感谢各位,问题解决后我会追加分数的。 展开
我这里有一个用户控件,可以解决你的问题
不用那个标签控件,不是就是在你的在控件上打字吗,这样解决
以下内容复制到文本文件,然后另存为userCommand.ctl,然后再VB里添加文件就可以看到效果了
VERSION 5.00
Begin VB.UserControl userCommand
ClientHeight = 1095
ClientLeft = 0
ClientTop = 0
ClientWidth = 2700
ScaleHeight = 1095
ScaleWidth = 2700
Begin VB.Label Label1
Alignment = 2 'Center
BackStyle = 0 'Transparent
Caption = "Label1"
ForeColor = &H00FFFFFF&
Height = 225
Left = 2490
TabIndex = 0
Top = 1020
Visible = 0 'False
Width = 915
End
End
Attribute VB_Name = "userCommand"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'缺省属性值:
Const m_def_Caption = "Command"
Const m_def_DownColor = &H8000000F
Const m_def_ysColor = &H8000000F
Const m_def_hgColor = &H8000000F
'属性变量:
'Dim m_Caption As String
Dim m_DownColor As OLE_COLOR
Dim m_ysColor As OLE_COLOR
Dim m_hgColor As OLE_COLOR
Dim m_d As Boolean
'事件声明:
Event Click()
Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Public Property Get Enabled() As Boolean
Attribute Enabled.VB_Description = "返回/设置一个值,决定一个对象是否响应用户生成事件。"
Enabled = UserControl.Enabled
End Property
Public Sub DrawGrid()
UserControl.Cls '重画
UserControl.CurrentX = (UserControl.Width - TextWidth(Caption)) / 2
'设置文字显示的X坐标
UserControl.CurrentY = (UserControl.Height - TextHeight(Caption)) / 2
'设置文字显示的Y坐标
UserControl.Print Caption '显示文字“abc”
End Sub
Public Property Let Enabled(ByVal New_Enabled As Boolean)
UserControl.Enabled() = New_Enabled
PropertyChanged "Enabled"
End Property
Private Sub UserControl_Click()
UserControl.BackColor = ysColor
DrawGrid
RaiseEvent Click
End Sub
'
'
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
UserControl.BackColor = DownColor
DrawGrid
m_d = True
RaiseEvent MouseDown(Button, Shift, X, Y)
End Sub
'
'
'
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_d = False
DrawGrid
RaiseEvent MouseUp(Button, Shift, X, Y)
End Sub
Public Property Get ysColor() As OLE_COLOR
Attribute ysColor.VB_Description = "原始颜色"
ysColor = m_ysColor
End Property
Public Property Let ysColor(ByVal New_ysColor As OLE_COLOR)
m_ysColor = New_ysColor
PropertyChanged "ysColor"
End Property
Public Property Get hgColor() As OLE_COLOR
Attribute hgColor.VB_Description = "鼠标滑过的颜色"
hgColor = m_hgColor
End Property
Public Property Let hgColor(ByVal New_hgColor As OLE_COLOR)
m_hgColor = New_hgColor
PropertyChanged "hgColor"
End Property
Private Sub UserControl_InitProperties()
Set UserControl.Font = Ambient.Font
m_ysColor = m_def_ysColor
m_hgColor = m_def_hgColor
m_DownColor = m_def_DownColor
' m_Caption = m_def_Caption
End Sub
'从存贮器中加载属性值
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
UserControl.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012)
UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
Set UserControl.Font = PropBag.ReadProperty("Font", Ambient.Font)
m_ysColor = PropBag.ReadProperty("ysColor", m_def_ysColor)
m_hgColor = PropBag.ReadProperty("hgColor", m_def_hgColor)
m_DownColor = PropBag.ReadProperty("DownColor", m_def_DownColor)
Label1.Caption = PropBag.ReadProperty("Caption", m_def_Caption)
UserControl.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
Caption = PropBag.ReadProperty("Caption", m_def_Caption)
UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012)
UserControl.FontSize = PropBag.ReadProperty("FontSize", 11)
End Sub
Private Sub UserControl_Resize()
DrawGrid
End Sub
'将属性值写到存储器
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("BackColor", UserControl.BackColor, &H8000000F)
Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012)
Call PropBag.WriteProperty("Enabled", UserControl.Enabled, True)
Call PropBag.WriteProperty("Font", UserControl.Font, Ambient.Font)
Call PropBag.WriteProperty("ysColor", m_ysColor, m_def_ysColor)
Call PropBag.WriteProperty("hgColor", m_hgColor, m_def_hgColor)
Call PropBag.WriteProperty("DownColor", m_DownColor, m_def_DownColor)
Call PropBag.WriteProperty("BackColor", UserControl.BackColor, &H8000000F)
Call PropBag.WriteProperty("Caption", Caption, m_def_Caption)
Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012)
Call PropBag.WriteProperty("FontSize", UserControl.FontSize, 11)
End Sub
Public Property Get DownColor() As OLE_COLOR
Attribute DownColor.VB_Description = "鼠标按下时的颜色"
DownColor = m_DownColor
End Property
Public Property Let DownColor(ByVal New_DownColor As OLE_COLOR)
m_DownColor = New_DownColor
PropertyChanged "DownColor"
End Property
'
'
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
SetCapture UserControl.hWnd
If m_d = False Then UserControl.BackColor = hgColor
DrawGrid
If X < 0 Or X > UserControl.Width Or Y < 0 Or Y > UserControl.Height Then
UserControl.BackColor = ysColor
DrawGrid
ReleaseCapture
End If
RaiseEvent MouseMove(Button, Shift, X, Y)
End Sub
Private Sub UserControl_Initialize()
UserControl.AutoRedraw = True
End Sub
Public Property Get BackColor() As OLE_COLOR
Attribute BackColor.VB_Description = "返回/设置对象中文本和图形的背景色。"
BackColor = UserControl.BackColor
End Property
Public Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
UserControl.BackColor() = New_BackColor
PropertyChanged "BackColor"
End Property
Public Property Get Caption() As String
Caption = Label1.Caption
End Property
Public Property Let Caption(ByVal New_Caption As String)
Label1.Caption() = New_Caption
PropertyChanged "Caption"
UserControl_Resize
End Property
Public Property Get ForeColor() As OLE_COLOR
Attribute ForeColor.VB_Description = "返回/设置对象中文本和图形的前景色。"
ForeColor = UserControl.ForeColor
End Property
Public Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
UserControl.ForeColor() = New_ForeColor
PropertyChanged "ForeColor"
End Property
Public Property Get FontSize() As Single
Attribute FontSize.VB_Description = "指定给定层的每一行出现的字体大小(以磅为单位)。"
FontSize = UserControl.FontSize
End Property
Public Property Let FontSize(ByVal New_FontSize As Single)
UserControl.FontSize() = New_FontSize
PropertyChanged "FontSize"
End Property
用这两个api函数:SetCapture ,ReleaseCapture 的确可以解决问题,还把鼠标离开事件给解决了。但是在产生鼠标离开事件时有点小问题:
当某个控件覆盖住这个按钮的一部分时,把鼠标移到这个控件上(确切的说事移到覆盖住按钮的那一部分),按钮按钮并不会产生鼠标离开事件,请问下这个怎么解决啊?谢谢!
那个确实没办法,因为真的还没离开,上诉的解决办法就是为了不在自定义控件上加标签,而是用UserControl.Print 打印的方法替换了标签,一样的效果,并且效果更好,原因在于解决个上下居中的问题,标签不能实现上下居中,只能左右居中,字体不变的情况还好,如果改变字体的大小标签也不会自动扩充,这样不好控制标签,直接打印就没有这样的问题了
在标签的X事件中调用你的usercontrol控件的X事件过程不就得了。比如
Private Sub Label1_Click()
usercontrol_Click ' 调用usercontrol控件的Click事件过程
End Sub
如果还想要个动态效果,比如usercontrol控件改变一下颜色、图形之类,那就先做这些工作然后再调用usercontrol控件的事件过程。比如:
Private Sub Label1_Click()
' 此处为设置usercontrol控件外观的代码
' 适当延时
' 此处为恢复usercontrol控件外观的代码
usercontrol_Click ' 调用usercontrol控件的Click事件过程
End Sub
这里,“设置”--“延时” --“恢复” 模拟usercontrol控件被单击时的情形。
2、获取标签的单击事件,在标签的单击事件中调用按钮的事件。
你没看明白,我的标签就在按钮当中,是按钮的caption
呵呵,我做过这样的按钮,其实将标签事件作为按钮事件就可以,我做的在很多单位运行没有问题。
能说明白些吗?判断什么?
就是说,上边的语句肯定会有一个输出地结果,你利用这个结果,编写一个条件语句,并且让你想跳过的语句作为满足条件的执行语句,这样就可以跳过了