using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Automation;
namespace QQEmail.Dal
{
public class QQ
{
public DevExpress.XtraGrid.GridControl grid = new DevExpress.XtraGrid.GridControl();
public bool isQQLoaded(ref int ProcessID)
{
string machinename = System.Environment.MachineName;//获得计算机名
Process[] processlist = Process.GetProcesses(machinename);//得到所有进程
foreach (Process p in processlist)//列举每个进程
{
if (p.ProcessName == "QQ")
{
ProcessID =p.Id;
return true;
}
}
return false;
}
public AutomationElement FindWindowByProcessId(int processId)
{
AutomationElement targetWindow = null;
Process p = Process.GetProcessById(processId);
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
return targetWindow;
}
public List<Model.QQ> getList( )
{
List<Model.QQ> list = new List<QQEmail.Model.QQ>();
int pid=0;
if (isQQLoaded(ref pid) )
{
AutomationElement aeDeskTop = FindWindowByProcessId(pid);
AutomationElement seekPanel = aeDeskTop.FindFirst(TreeScope.Element, new PropertyCondition(AutomationElement.ClassNameProperty, "#32770"));
if (seekPanel == null)
{
DevExpress.XtraEditors.XtraMessageBox.Show("请打开搜索面板");
}
else
{
AutomationElement tabControls = seekPanel.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "SysTabControl32"));
AutomationElement city = tabControls.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox"));
AutomationElement nextButton = tabControls.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "下页"));
while (nextButton.Current.IsEnabled)
{
foreach (AutomationElement ae in tabControls.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem)))
{
Queue<string> lst = new Queue<string>();
foreach (AutomationElement subAe in ae.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text)))
{
lst.Enqueue(subAe.Current.Name);
}
Model.QQ model = new QQEmail.Model.QQ();
model.Code = lst.Dequeue();
model.Name = lst.Dequeue();
model.Sex = lst.Dequeue();
model.Age = lst.Dequeue();
model.City = lst.Dequeue();
model.Email = model.Code + "@qq.com";
list.Add(model);
}
grid.Invoke(new EventHandler(delegate
{
grid.DataSource = list;
}));
InvokePattern ipClickButton = (InvokePattern)nextButton.GetCurrentPattern(InvokePattern.Pattern);
ipClickButton.Invoke();
System.Threading.Thread.Sleep(200);
} //AutomationElement[] selectItems = (city.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern).Current.GetSelection();
//if (selectItems.Length > 0)
//{
// SelectionItemPattern cityValue = (SelectionItemPattern)city.GtCurrentPattern(SelectionItemPattern.Pattern);
// cityValue.AddToSelection
//}
//foreach (AutomationElement ae in city.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem)))
//{
// SelectionItemPattern cityValue = (SelectionItemPattern)ae.GetCurrentPattern(SelectionItemPattern.Pattern);
// cityValue.Select();
//}
}
}
else
{
DevExpress.XtraEditors.XtraMessageBox.Show("请打开QQ(2008版)");
}
return list;
}
}
}