我在C#.NET项目的类顶部有以下代码:
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKey);
以下是我尝试在别处使用的方法:
private void CopyHighlighted()
{
uint KEYEVENTF_KEYUP = 2;
byte VK_CONTROL = 0x11;
SetForegroundWindow(GetForegroundWindow());
keybd_event(VK_CONTROL, 0, 0, 0);
keybd_event(0x43, 0, 0, 0);
keybd_event(0x43, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
}
此代码以前有效。但现在每当我在窗口中突出显示某些文本并调用它时,剪贴板都会保持空白。有没有人见过这种行为?