⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 142.htm

📁 好书啊!很好哦
💻 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&nbsp弹出式菜单                            <br>
      <b> 类 别</b>:用户界面                                           
      <hr size="1" width="100%">
      <span class="content">&nbsp&nbsp&nbsp&nbsp有时,我们要在ListBox的弹出式菜单中通过ItemIndex显示项目的有关信息。但是,在项目上右击鼠标时,ItemIndex不会象左击那样被改变。这篇文章将通过例子来告诉你如何实现此功能。<br>假设你有一个ListBox,填充有称作Widgets的类:<br><br>&nbsp&nbsp&nbsp&nbsptype&nbspTWidget&nbsp=&nbspclass(Tobject)&nbsp<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWidgetName&nbsp&nbsp:&nbspstring;&nbsp<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWidgetStatus&nbsp:&nbspboolean;&nbsp<br>&nbsp&nbsp&nbsp&nbspEnd;&nbsp<br><br>&nbsp&nbsp&nbsp&nbspWidgets&nbsp:&nbsparray&nbsp[0..10]&nbspof&nbspTWidget&nbsp<br><br>&nbsp&nbsp&nbsp&nbsp每一项WidgetName在ListBox中显示出来。&nbsp<br><br>&nbsp&nbsp&nbsp&nbsp你想要通过一个弹出式菜单来改变每一个Widget的状态,其中弹出式菜单与ListBox的OnPopUp事件关联。如果状态是活动的,设置“Active”;如果不是活动的,不设置“Active”。单击“Active”来改变状态。<br><br>&nbsp&nbsp&nbsp&nbsp问题是:鼠标左键单击ListBox会选择一Widget,但用右键单击(并显示弹出菜单)时不会选择。如果鼠标不在已选项上,弹出菜单显示的将不是鼠标所在Widget的状态,而是已选Widget的状态。<br><br>&nbsp&nbsp&nbsp&nbsp幸运的是,ListBox的OnMouseDown比弹出式菜单的OnPopUp先被触发。这样,我们就能在弹出式菜单显示之前用OnMouseDown事件设置ItemIndex。<br><br>&nbsp&nbsp&nbsp&nbspTlistBox有一个方法:ItemAtPos(Pos:&nbspTPoint;&nbspExisting:&nbspBoolean):&nbspInteger;<br>如果能在Pos座标处找到ListBox的一项,这一方法将返回这一项的Index。如果没有找到任何项,且Existing值设为True,ItemAtPos将返回值-1,如果Existing值设为False,ItemAtPos将返回ListBox最后一项的Index值加1。<br><br>&nbsp&nbsp&nbsp&nbsp用这个方法结合OnMouseDown事件就解决了我们的问题:<br><br>&nbsp&nbsp&nbsp&nbspOnMouseDown代码:&nbsp<br><br>procedure&nbspTForm1.WidgetListMouseDown(Sender:&nbspTObject;&nbspButton:&nbspTMouseButton;&nbspShift:&nbspTShiftState;&nbspX,&nbspY:&nbspInteger);&nbsp<br>&nbsp&nbspvar&nbsp<br>&nbsp&nbspMousePos&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp:&nbspTPoint;&nbsp<br>&nbsp&nbspOverItemIndex&nbsp:&nbspinteger;&nbsp<br><br>begin&nbsp<br>&nbsp&nbspMousePos.x&nbsp:=&nbspX;&nbsp<br>&nbsp&nbspMousePos.y&nbsp:=&nbspY;&nbsp<br><br>&nbsp&nbspif&nbspButton&nbsp=&nbspmbRight&nbspthen&nbsp<br>&nbsp&nbsp&nbsp&nbspbegin&nbsp<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspOverItemIndex&nbsp:=&nbspWidgetList.ItemAtPos(MousePos,False);&nbsp<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWidgetList.ItemIndex:=OverItemIndex;&nbsp<br>&nbsp&nbsp&nbsp&nbspend;&nbsp<br>end;&nbsp<br><br><br>OnPopUp代码:&nbsp<br>procedure&nbspTForm1.PopupMenu1Popup(Sender:&nbspTObject);&nbsp<br>var&nbsp<br>&nbsp&nbspIndex&nbsp:&nbspinteger;&nbsp<br>begin&nbsp<br>&nbsp&nbspIndex&nbsp:=&nbspWidgetList.ItemIndex;&nbsp<br>&nbsp&nbspPopUpMenuItemActive.Checked&nbsp:=&nbspWidgets[Index].WidgetStatus;&nbsp<br>end;&nbsp<br><br><br>弹出式菜单项"Active"的OnClick代码:<br>procedure&nbspTForm1.PopUpMenuItemActiveClick(Sender:&nbspTObject);&nbsp<br>var&nbsp<br>&nbsp&nbspIndex&nbsp:&nbspinteger;&nbsp<br>begin&nbsp<br>&nbsp&nbspIndex&nbsp:=&nbspWidgetList.ItemIndex;&nbsp<br>&nbsp&nbspWidgets[Index].WidgetStatus&nbsp:=&nbspnot&nbspWidgets[Index].WidgetStatus;&nbsp<br>&nbsp&nbspPopUpMenuItemActive.Checked:=not&nbspPopUpMenuItemActive.Checked;&nbsp<br>end;&nbsp<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 + -