假设 C++/CLI,因为您提到了 SendKeys。SendKeys 不能可靠地工作,因为它会释放键,使 Alt-Tab 窗口消失。您想改用 SendInput() 并为 Alt 键发送一个 keydown,为 Tab 键发送一个 keydown + up。这段代码运行良好:
#include <windows.h>
#pragma comment(lib, "user32.lib")
...
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
INPUT input = {INPUT_KEYBOARD};
input.ki.wVk = (WORD)Keys::Menu;
UINT cnt = SendInput(1, &input, sizeof(input));
input.ki.wVk = (WORD)Keys::Tab;
if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
if (cnt != 1) throw gcnew System::ComponentModel::Win32Exception;
}