C# 弹出对话框 自动按下确定按钮

来源:百度知道 编辑:UC知道 时间:2024/06/17 07:52:59
Windows桌面程序,弹出一个对话框,然后让它自动按下确定按钮,怎么发这个消息呢?
有没有搞错,我说的是桌面程序

那样是不行的,因为程序会停在你调用SHOWDIALOG()的地方,根本不会继续执行

你试试这种方法吧,虽然不是最好的但能实现
using System.Runtime.InteropServices;
using System.Threading;

[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
private static extern int GetForegroundWindow();
const int WM_CLOSE = 0x10;
Thread thread;
private void button1_Click(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(CloseMessageBox));
thread.Start();
MessageBox.Show("Hello World");
}
void CloseMessageBox()
{
Thread.Sleep(500);
int handle = GetForegroundWindow();
Thread.Sleep(2500);
SendMessage(handle, WM_CLOSE, 0, 0);
thread.Abort();
}