2016-11-07 106 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