
MsgBoxEx函数能满足你的要求,把DebugPrint 改成你想执行的语句即可
wType参数改成可以提示输入VBA的vbMsgboxStyle常数。
这个API函数的参数如下:
hwnd:窗口句柄,可以设为0
lpText:消息框显示内容,类似于MsgBox函数的第一个参数Prompt
lpCaption:消息框标题,类似于MsgBox函数的第三个参数Caption
wType:消息框类型,类似于MsgBox函数的第二个参数Buttons
wlange:不是太明白这个参数,0或者1都看不出什么差别
dwTimeout:延时时间,单位是毫秒
返回的值和vbMsgBoxResult常数一样,多了一个返回值32000表示超过延时时间未选择任何按钮。
Private Declare Function MsgBoxEx Lib "user32" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wlange As Long, _
ByVal dwTimeout As Long) As Long‘函数声明
Private Sub TestMsgboxEx()’使用
Dim ret As Long
ret = MsgBoxEx(0, "要终止此程序么", "60秒后自动关闭", vbYesNo + vbInformation, 1, 60000)
If ret = 32000 Or ret = vbYes Then End
End Sub
写好了,以下是截图和部分源码,完整的源码在附件中:
1指定要d出的消息以及定时的时间(单位秒)
2d出后,对话框上的确定按钮上会动态倒计时,当时间为0时自动关闭,也可以通过点击确定按钮关闭
核心代码:
public partial class TimingMessageBox : Form{
// 自动关闭的时间限制,如3为3秒后自动关闭
private int second;
// 计数器,用以判断当前窗口d出后持续的时间
private int counter;
// 构造函数
public TimingMessageBox(string message, int second)
{
InitializeComponent();
// 显示消息
thislabelMessageText = message;
// 获得时间限制
thissecond = second;
// 初始化计数器
thiscounter = 0;
// 初始化按钮的文本
thisbuttonOKText = stringFormat("确定({0})", thissecond - thiscounter);
// 激活并启动timer,设置timer的触发间隔为1000毫秒(1秒)
thistimer1Enabled = true;
thistimer1Interval = 1000;
thistimer1Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// 如果没有到达指定的时间限制
if (thiscounter <= thissecond)
{
// 刷新按钮的文本
thisbuttonOKText = stringFormat("确定({0})", thissecond - thiscounter);
thisRefresh();
// 计数器自增
thiscounter++;
}
// 如果到达时间限制
else
{
// 关闭timer
thistimer1Enabled = false;
thistimer1Stop();
// 关闭对话框
thisClose();
}
}
private void buttonOK_Click(object sender, EventArgs e)
{
// 单击确定按钮,关闭对话框
thisClose();
}
}
然后在主窗体中调用:
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
}
private void buttonShowMessageBox_Click(object sender, EventArgs e)
{
string message = thistextBoxMessageTextTrim();
int second = ConvertToInt32(thistextBoxSecondTextTrim());
TimingMessageBox messageBox=new TimingMessageBox(message,second);
messageBoxShowDialog();
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)