using System.Runtime.InteropServices;
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll ")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwnd = GetForegroundWindow();//取前台窗体句柄
SendMessage(hwnd, 256, 0xD, 0);//按下回车键
//SendMessage(hwnd, 257, 0xD, 0);//弹起回车键
}