2016-11-07 108 views
1

我遇到了编写代码的麻烦。我有一个列出工作人员详细信息的表格。我想在选择一条记录并右键单击时将电子邮件地址复制到剪贴板。我有以下代码:如果声明不起作用AutoIT

#include <GUIConstantsEx.au3> 
#include <mssql.au3> 
#include <MsgBoxConstants.au3> 
#include <Array.au3> 
#include <WindowsConstants.au3> 
#include <AutoItConstants.au3> 
global $title = "E-Mail address lookup" 
global $name = InputBox($title,"Please type the name of the person you wish to find") 
global $sqlCon = _MSSQL_Con("server", "username", "password", "directory-plus") 
global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'") 
if StringLen(StringStripWS($name,3)) < 1 then 
MsgBox(0, $title, "Name cannot be empty") 
Else 
Global $rset = UBound($result) - 1 
Global $ControlID = GUICreate($title, 500, 150) 
Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 480, 150) 
for $count = 1 to $rset step 1 
     GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][1] & "|" & $result[$count][2], $idListview) 
     if MouseClick($MOUSE_CLICK_RIGHT)==1 Then 
      ClipPut($result[$count][2]) 
     EndIf 
Next 
GUISetState(@SW_SHOW) 
GUISetState() 

While 1 
Global $Msg = GUIGetMsg() 
Switch $Msg 
    Case -3, $ControlID 
     Exit 
EndSwitch 
WEnd 
EndIf 

我还以为在我的IF语句for循环会做的伎俩,但是,当我运行我的代码,它只是复制无论是在电子邮件地址栏最后一行时事实上,我希望它复制的电子邮件地址,当你右键点击特定的行,但我不知道如何做到这一点。

回答

1

您的代码有两个主要问题。首先是MouseClick()不检查被按下的鼠标按钮,而是发送鼠标按钮单击。由于发送鼠标按钮点击将成功,MouseClick($ MOUSE_CLICK_RIGHT)== 1将评估为true。对于您创建的每个列表视图项目,您将电子邮件地址放在剪贴板上。

第二个问题是if语句在错误的地方。它在您创建每个列表视图项目后立即执行。

相反,改变你的while语句如下

While 1 
Global $Msg = GUIGetMsg() 
Switch $Msg 
    Case -3, $ControlID 
    Exit 
    case $GUI_EVENT_SECONDARYDOWN 
    $selecteditem=StringSplit(GUICtrlRead(GUICtrlRead($idListview)),"|") 
    if @error=0 and $selecteditem[0]>1 Then 
    ClipPut($selecteditem[2]) 
    EndIf 
EndSwitch 
WEnd