问:是否有办法给CheckListBox控件添加一个横的滚动条?
答:在版本2.5 Build
7以上,你可以添加一个滚动条。使用下面的代码就可以了:
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Const LB_SETHORIZONTALEXTENT = &H194
Private Sub Form_Load()
ChkList1.AddItem "Line 1"
ChkList1.AddItem "a big Line 2 some text some
text"
ChkList1.AddItem "Line 3"
addHorScrlBarListBox ChkList1
End Sub
Public Sub addHorScrlBarListBox(ByVal refControlListBox
As Object)
Dim nRet As Long
Dim nNewWidth As Integer
nNewWidth = 400 ' new width
in pixels
nRet = SendMessage(refControlListBox.hwnd, _
LB_SETHORIZONTALEXTENT, nNewWidth, ByVal
0&)
End Sub
回到前面
当用户在列表框上击右键时如何弹出菜单?
问:我想在用户在每个条目上右击时显示一个弹出式菜单。我所遇到的一个问题是右击时条目不会被选中,而且因为它没有被选中,我不知道弹出式是在哪个条目上。当鼠标右击,我可以知道点击的x和y坐标,是否有办法知道点击在那个条目上?
答: 右击不会自动选择条目。但是可以向列表框发送LB_ITEMFROMPOINT消息以获得列表框中最接近于制定点的条目的索引值。
下面是一个例子:
Global Const
LB_ITEMFROMPOINT = &H1A9
Declare Function
SendMessage Lib "user32" Alias "SendMessageA"
_
(ByVal hWnd
As Long, _
ByVal wMsg
As Long, _
ByVal wParam
As Long, _
lParam As
Any) As Long
Dim lRet As
Long
Dim lXPos
As Long, lYPos As Long
' Convert the cursor position into pixels , because that is what
is needed
lXPos = CLng(x
/ Screen.TwipsPerPixelX)
lYPos = CLng(y
/ Screen.TwipsPerPixelY)
'
If the right mouse button is clicked...
If Button
= 2 Then
'
Get the listitem closest to the cursor
'
NOTE: Since the X and Y values have to be in the form of high
and low
'
order words, send the values as ((lYPos * 65536) + lXPos)
lRet = SendMessage(ChkFileList.hWnd,
LB_ITEMFROMPOINT, 0, _
ByVal ((lYPos
* 65536) + lXPos))
'
If the returned value is a valid index, then set that item as
the selected
' item
If lRet
< ChkFileList.ListCount Then
ChkFileList.ListIndex
= lRet
'
now Popup the menu over the selected item with Selection bar
' Highlighting it
PopupMenu
MnuChkListInfo
Else:
'
If not possible to highlight the item then beep and exit
Beep
End If
End If
回到前面