【发布时间】:2015-06-01 05:04:55
【问题描述】:
我想从其他程序复制文本, 在这个程序中 Ctrl+a 被考虑用于其他命令,我不能使用“ SendKeys.SendWait("^a");"选择文本。
有没有办法复制那个文本?
【问题讨论】:
标签: c# api winapi sendmessage sendkeys
我想从其他程序复制文本, 在这个程序中 Ctrl+a 被考虑用于其他命令,我不能使用“ SendKeys.SendWait("^a");"选择文本。
有没有办法复制那个文本?
【问题讨论】:
标签: c# api winapi sendmessage sendkeys
您可以使用UIAComWrapper 执行此操作,您将需要该窗口的句柄(从您尝试复制的位置)以及您可以从UIAutomationVerify 获得的有关该元素的信息。
var elementCollection = AutomationElement.FromHandle(windowHandle).FindAll(TreeScope.Subtree, Condition.TrueCondition);
foreach (var item in elementCollection)
{
//check item properties if element is the one you looking for
}
另外,除了Condition.TrueCondition,您还可以提供更复杂的过滤器来仅获取那个元素。
编辑,添加实例:
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const string InternetExplorerClass = "IEFrame";
static void Main()
{
var windowHandle = new IntPtr(0);
//Find internet explorer instance
windowHandle = FindWindow(InternetExplorerClass, null);
if (!windowHandle.Equals(IntPtr.Zero))
{
//create filter to improve search speed
var localizedControlType = new PropertyCondition(
AutomationElement.LocalizedControlTypeProperty,
"tab item");
//get all elements in internet explorer that match our filter
var elementCollection =
AutomationElement.FromHandle(windowHandle)
.FindAll(TreeScope.Subtree, localizedControlType);
//iterate through search results
foreach (AutomationElement item in elementCollection)
{
Console.WriteLine(item.Current.Name);
}
}
else
{
Console.WriteLine("Internet explorer not found");
}
Console.ReadLine();
}
上面的代码将找到 Internet Explorer 并将所有选项卡标题打印到控制台。我把源代码放到GitHub。
【讨论】:
那是什么类型的编辑器,为什么不能只获取源文件?也许这会起作用: 1.将光标放在第一行的开头 2. 按 Ctrl+Shift+End 3. Ctrl+C
或者,您可以尝试通过Windows Input Simulator library模拟键盘输入
【讨论】: