C# C SendKeys.SendWait(“^C”)不会在其他应用程序中复制所选文本

C# C SendKeys.SendWait(“^C”)不会在其他应用程序中复制所选文本,c#,text,copy,sendkeys,C#,Text,Copy,Sendkeys,我有以下代码: using语句是 using System; using System.Runtime.InteropServices; using System.Drawing; using System.Diagnostics; using System.Windows.Forms; 在我的主要任务之前 // import the function in your [DllImport("User32.dll")] static extern int SetForeg

我有以下代码:

using语句是

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
在我的主要任务之前

// import the function in your 
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);

[STAThreadAttribute]
static void **Main**(string[] args)
// Get the Processes
Process[] p = Process.GetProcesses();
string processName = "";
// Loop through the processes list
for (int i = 0; i < p.Length; i++)
{
    // Find myProcessName
    if (p[i].ProcessName == "myProcessName")
    {
        Console.WriteLine("PID: " + p[i].Id);
        processName = p[i].ProcessName;
    }
}

// Set the process/window to the foreground
Process p = Process.GetProcessesByName(processName)[0];
if (p != null)
{
    // Get the process handle
    IntPtr h = p.MainWindowHandle;
    // Set the window to the foreground
    SetForegroundWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
    // Set the Active window
    SetActiveWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
}

// Select the Text
MouseOperations.SetCursorPosition(1269, 218);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);

MouseOperations.SetCursorPosition(123, 310);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.SetCursorPosition(133, 502);

// Send the Ctrl+C
SendKeys.SendWait("^c");
// Application.DoEvents();
// Print the clipboard
if (Clipboard.ContainsText())
{
    Console.WriteLine("##########################################");
    Console.WriteLine(Clipboard.GetText());
    Console.WriteLine("##########################################");
}

// Done, lift the left mouse key
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);
总的来说,我有

// import the function in your 
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);

[STAThreadAttribute]
static void **Main**(string[] args)
// Get the Processes
Process[] p = Process.GetProcesses();
string processName = "";
// Loop through the processes list
for (int i = 0; i < p.Length; i++)
{
    // Find myProcessName
    if (p[i].ProcessName == "myProcessName")
    {
        Console.WriteLine("PID: " + p[i].Id);
        processName = p[i].ProcessName;
    }
}

// Set the process/window to the foreground
Process p = Process.GetProcessesByName(processName)[0];
if (p != null)
{
    // Get the process handle
    IntPtr h = p.MainWindowHandle;
    // Set the window to the foreground
    SetForegroundWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
    // Set the Active window
    SetActiveWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
}

// Select the Text
MouseOperations.SetCursorPosition(1269, 218);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);

MouseOperations.SetCursorPosition(123, 310);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.SetCursorPosition(133, 502);

// Send the Ctrl+C
SendKeys.SendWait("^c");
// Application.DoEvents();
// Print the clipboard
if (Clipboard.ContainsText())
{
    Console.WriteLine("##########################################");
    Console.WriteLine(Clipboard.GetText());
    Console.WriteLine("##########################################");
}

// Done, lift the left mouse key
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);
鼠标操作是从这里开始的

我在VisualStudio2019中运行该程序,命令窗口显示,我想要的程序出现在命令窗口上方,文本被选中,但没有任何内容被复制到剪贴板


你知道我这里缺少什么吗?

另一个应用程序是你的吗?if not询问制作它的公司是否保护它不受sendkeys的影响。看起来没问题,只是Main中的第一个循环似乎只是等效于字符串processName=myProcessName,因为接下来要做的是再次按名称查找进程。。。您可以在当时获取Process.Id,并消除初始循环。在复制粘贴方面,你在复制时一直按住鼠标左键——我不确定这在Windows中是否有效;我想交换这两个命令的顺序。@CoolBots按照您的建议在复制之前举起鼠标左键,并没有改变结果。@Franck我的理解是SendKeys与键盘按键无法区分-您知道防止SendKeys的方法吗“?您是否介意发布指向此类资源的链接或快速解释?谢谢!您可以检查您的代码是否适用于其他应用程序,例如记事本?另一个应用程序是您的吗?如果不询问制作该应用程序的公司是否保护它不受sendkeys的影响。看起来还可以,但Main中的第一个循环似乎只是与string pro等效。”cessName=myProcessName,因为下一步要做的事情是再次按名称查找进程…此时可以获取进程.Id,并消除初始循环。就复制粘贴而言,复制时按住鼠标左键-我不确定这是否在Windows中起作用;我将交换这两个命令的顺序。@CoolBotse在复制之前,按您的建议单击鼠标左键并没有改变结果。@Franck我的理解是SendKeys与键盘按键无法区分-您知道防止SendKeys的方法吗“?您介意发布一个指向此类资源的链接或快速解释吗?谢谢!您能检查一下您的代码是否适用于其他应用程序,比如记事本吗?”?