c# winform 自动关闭messagebox 模拟回车
2024-10-19 11:38:32
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace WindowsApplication1
{
public partial class AutoDeleteMessageBox : Form
{
[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; public AutoDeleteMessageBox()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
StartKiller();
MessageBox.Show("3秒钟后自动关闭MessageBox窗口", "MessageBox");
} private void StartKiller()
{
Timer timer = new Timer();
timer.Interval = ; //3秒启动
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
} private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止Timer
((Timer)sender).Stop();
} private void KillMessageBox()
{
//按照MessageBox的标题,找到MessageBox的窗口
IntPtr ptr = FindWindow(null, "MessageBox");
if (ptr != IntPtr.Zero)
{
//找到则关闭MessageBox窗口
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
最新文章
- 《Linux内核分析》之第三章读书笔记
- Unity(64bit)5.1 打开无故出现框框,纹理不见了
- Flipping elements with WPF
- truncate/drop表非常慢,怎么办?用硬链接,极速体验
- drupal 2016-11-3
- activiti自定义流程之自定义表单(三):表单列表及预览和删除
- 金融系列14《QPBOC交易流程》
- 引擎设计跟踪(九.14.2h) 开发计划
- BLOCK专题
- 微软Hololens学院教程-Hologram 230-空间场景建模(Spatial mapping )【微软教程已经更新,本文是老版本】
- placeholder
- OpenGL判断一个点是否可见
- python装饰器大详解
- vue elementUI之Form表单 验证
- NSUserDefaults standardUserDefaults使用注意事项
- redis学习笔记01 — 基本介绍、安装配置及常用命令
- logistics回归简单应用(二)
- vim编辑
- IDEA 实用功能Auto Import:自动优化导包(自动删除、导入包)
- 【Linux】shell学习之sed