📄 142.htm
字号:
<!-- saved from url=(0022)http://internet.e-mail -->
<html>
<head>
<title>通过鼠标右击选择TListBox中的选项 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="inc.css">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#000000" vlink="195434" alink="195434">
<table width="621" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="621"><a href="http://www.CoDelphi.com"><img src="images/logo.gif" border="0" width="153" height="60"><img src="images/bigbanner.gif" border="0" width="468" height="60"></a></td>
</tr>
</table><br>
<br>
<table width="621" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="left" valign="top" class="font9" height="155">
<div align="center" class="btitle">通过鼠标右击选择TListBox中的选项 <br>
<br>
</div>
<div align="center"><strong>CoDelphi.com </strong></div>
<b><br>
摘 要</b>:如何屏蔽屏幕保护 <br>
<b> 关键字</b>:TListBox 弹出式菜单 <br>
<b> 类 别</b>:用户界面
<hr size="1" width="100%">
<span class="content">    有时,我们要在ListBox的弹出式菜单中通过ItemIndex显示项目的有关信息。但是,在项目上右击鼠标时,ItemIndex不会象左击那样被改变。这篇文章将通过例子来告诉你如何实现此功能。<br>假设你有一个ListBox,填充有称作Widgets的类:<br><br>    type TWidget = class(Tobject) <br>      WidgetName  : string; <br>      WidgetStatus : boolean; <br>    End; <br><br>    Widgets : array [0..10] of TWidget <br><br>    每一项WidgetName在ListBox中显示出来。 <br><br>    你想要通过一个弹出式菜单来改变每一个Widget的状态,其中弹出式菜单与ListBox的OnPopUp事件关联。如果状态是活动的,设置“Active”;如果不是活动的,不设置“Active”。单击“Active”来改变状态。<br><br>    问题是:鼠标左键单击ListBox会选择一Widget,但用右键单击(并显示弹出菜单)时不会选择。如果鼠标不在已选项上,弹出菜单显示的将不是鼠标所在Widget的状态,而是已选Widget的状态。<br><br>    幸运的是,ListBox的OnMouseDown比弹出式菜单的OnPopUp先被触发。这样,我们就能在弹出式菜单显示之前用OnMouseDown事件设置ItemIndex。<br><br>    TlistBox有一个方法:ItemAtPos(Pos: TPoint; Existing: Boolean): Integer;<br>如果能在Pos座标处找到ListBox的一项,这一方法将返回这一项的Index。如果没有找到任何项,且Existing值设为True,ItemAtPos将返回值-1,如果Existing值设为False,ItemAtPos将返回ListBox最后一项的Index值加1。<br><br>    用这个方法结合OnMouseDown事件就解决了我们的问题:<br><br>    OnMouseDown代码: <br><br>procedure TForm1.WidgetListMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); <br>  var <br>  MousePos      : TPoint; <br>  OverItemIndex : integer; <br><br>begin <br>  MousePos.x := X; <br>  MousePos.y := Y; <br><br>  if Button = mbRight then <br>    begin <br>      OverItemIndex := WidgetList.ItemAtPos(MousePos,False); <br>      WidgetList.ItemIndex:=OverItemIndex; <br>    end; <br>end; <br><br><br>OnPopUp代码: <br>procedure TForm1.PopupMenu1Popup(Sender: TObject); <br>var <br>  Index : integer; <br>begin <br>  Index := WidgetList.ItemIndex; <br>  PopUpMenuItemActive.Checked := Widgets[Index].WidgetStatus; <br>end; <br><br><br>弹出式菜单项"Active"的OnClick代码:<br>procedure TForm1.PopUpMenuItemActiveClick(Sender: TObject); <br>var <br>  Index : integer; <br>begin <br>  Index := WidgetList.ItemIndex; <br>  Widgets[Index].WidgetStatus := not Widgets[Index].WidgetStatus; <br>  PopUpMenuItemActive.Checked:=not PopUpMenuItemActive.Checked; <br>end; <br><br><br></span>
<table border="0" cellspacing="0" cellpadding="1" class="font9" align="center" width="100%">
<tr align="left" valign="middle" bgcolor="195434">
<td height="1"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#C9C9C6" height="2"></td>
</tr>
</table>
<div align="center"><br>
中文开发在线<a href="http://www.codelphi.com" target="_blank">www.codelphi.com</a>授权使用。
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -