C#如何控制对话框在指定时间关闭

2024-12-28 19:02:17
推荐回答(2个)
回答1:

刚才没有写清楚,,,,
我你做了一个完整的,下边是所有的代码。。。。。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //用于找到MessageBox的句柄
[DllImport("user32.dll")]
static extern bool DestroyWindow(IntPtr hWnd); //根据句柄关于MessageBox

private void button1_Click(object sender, EventArgs e)
{
DateTime date = DateTime.Now;
MessageBox.Show("这周是" + date.ToShortDateString().ToString() + "到" + date.AddDays(7).ToShortDateString().ToString());

MessageBox.Show("这个月是" + date.ToShortDateString().ToString() + "到" + date.AddMonths(1).ToShortDateString().ToString(),"a");

}

private void timer1_Tick(object sender, EventArgs e)
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, "a");
if (ptr != IntPtr.Zero)
{
DestroyWindow(ptr); //查找到窗口则关闭
}

}
}
}

回答2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //用于找到MessageBox的句柄
[DllImport("user32.dll")]
static extern bool DestroyWindow(IntPtr hWnd); //根据句柄关于MessageBox

private void button1_Click(object sender, EventArgs e)
{
DateTime date = DateTime.Now;
MessageBox.Show("这周是" + date.ToShortDateString().ToString() + "到" + date.AddDays(7).ToShortDateString().ToString());

MessageBox.Show("这个月是" + date.ToShortDateString().ToString() + "到" + date.AddMonths(1).ToShortDateString().ToString(),"a");

}

private void timer1_Tick(object sender, EventArgs e)
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, "a");
if (ptr != IntPtr.Zero)
{
DestroyWindow(ptr); //查找到窗口则关闭
}

}
}
}