ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Messagebox自定义计时关闭

2019-07-10 18:00:23  阅读:237  来源: 互联网

标签:IntPtr Messagebox string 自定义 int hwnd System timer1 计时


Messagebox自定义计时关闭

 

新建Winform项目WindowsFormsAppTESTMessageBoxAutoClose

主窗体代码

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

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

private void Button1_Click(object sender, EventArgs e)
{
MessageBoxTimeOut mb = new MessageBoxTimeOut();
mb.Show("123", "提示:");
}

private void Button2_Click(object sender, EventArgs e)
{
FunCord = 1;
StartTimer(1000);
t = 10;
timer1.Enabled = true;
if (MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)== DialogResult.OK)
{
MessageBox.Show("哈哈哈,你选择了ok");
}
//MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
//MessageBox.Show("直接关闭");
}

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowText(IntPtr hWnd, string text);

[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
const int WM_CLOSE = 0x10;
const int BM_CLICK = 0xF5;
int FunCord;
IntPtr hwnd;
int t;

System.Windows.Forms.Timer timer1;

private void StartTimer(int interval)
{
timer1 = new Timer();
timer1.Interval = interval;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
if (FunCord == 1)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
timer1.Enabled = false;
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
else if (FunCord == 2)
{
hwnd = FindWindow(null, "关闭提示");
IntPtr a = FindWindowEx(hwnd, (IntPtr)null, null,t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(a, t.ToString() + "秒后关闭");
if (t == 0)
{
timer1.Enabled = false;
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
else if (FunCord == 3)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "确定");
SendMessage(OKHwnd, BM_CLICK, 0, 0);
timer1.Enabled = false;
}
}
else if (FunCord == 4)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "取消");
SendMessage(OKHwnd, BM_CLICK, 0, 0);
timer1.Enabled = false;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
FunCord = 2;
t = 5;
timer1.Enabled = true;
MessageBox.Show(t + "秒后关闭", "关闭提示");
timer1.Enabled = false;
}
}
}

 

扩展类MessageBoxTimeOut:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsAppTESTMessageBoxAutoClose
{
public class MessageBoxTimeOut
{
private string _caption;

public void Show(string text, string caption)
{
Show(3000, text, caption);
}
public void Show(int timeout, string text, string caption)
{
Show(timeout, text, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
public void Show(int timeout, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
this._caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption, buttons, icon);
}
private void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止计时器
((Timer)sender).Enabled = false;
}

[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
private void KillMessageBox()
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, this._caption);
if (ptr != IntPtr.Zero)
{
//查找到窗口则关闭
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

}
}

 

标签:IntPtr,Messagebox,string,自定义,int,hwnd,System,timer1,计时
来源: https://blog.csdn.net/LongtengGensSupreme/article/details/95358566

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有