【发布时间】:2023-03-24 15:04:02
【问题描述】:
我在 C# 环境中使用 ManagedWindows API: http://mwinapi.sourceforge.net/
过去,我使用下面的代码成功地抓取了其他正在运行的程序的类似列表框的部分的内容,我在其中迭代键/值对以查找列表项。但是,对于这个特定的项目列表,我可以获得准确的项目数量,但该值始终为空!
使用这个:
TargetMidWindow.Content.ComponentType
我发现我遇到问题的列表是“listview”,而我成功使用的其他窗口是“detailslistview”,以防万一。下面是我用于查找所需数据的代码,除了更改我使用的搜索词之外,它几乎与我的其他成功代码相同。此外,如果相关,我试图从中提取数据的程序是 MetaTrader4,并且我已经能够成功地从程序的其他部分刮取数据。
// Find the main window
SystemWindow[] TopLevel = SystemWindow.AllToplevelWindows;
SystemWindow TargetTopWindow = SystemWindow.ForegroundWindow;
foreach (SystemWindow SearchWindow in TopLevel)
{
string Title = SearchWindow.Title;
if (Title.Contains("MetaTrader"))
{
TargetTopWindow = SearchWindow;
break;
}
}
// Find the section where positions are contained
SystemWindow[] MidLevel = TargetTopWindow.AllDescendantWindows;
SystemWindow TargetMidWindow = SystemWindow.ForegroundWindow;
foreach (SystemWindow SearchWindow in MidLevel)
{
string ClassName = SearchWindow.ClassName;
if (ClassName.Contains("SysListView32"))
{
SystemWindow ParentWindow = SearchWindow.Parent;
if ((ParentWindow.Title.Contains("Terminal")))
{
TargetMidWindow = SearchWindow;
}
}
}
// Get the positions
Dictionary<string, string> RawValues = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> KVP in TargetMidWindow.Content.PropertyList)
{
string key = KVP.Key;
string value = KVP.Value;
}
我需要做一些特别的事情来避免每个列表项都得到“空”值吗?
谢谢! 比尔
【问题讨论】:
标签: c# listview screen-scraping winapi