首页 > 编程语言 >C# 延时关闭MessageBox

C# 延时关闭MessageBox

时间:2023-02-14 00:44:25浏览次数:67  
标签:IntPtr MessageBox 窗口 title C# int static 延时 public


▲ 界面

Helper 类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

// 参考: https://www.cnblogs.com/hsiang/p/8878093.html

namespace CommonGE
{
    /// <summary>
    /// 描述:通过WinAPI进行查找窗口,并对窗口进行操作
    /// </summary>
    public class WndHelper
    {
        /// <summary>
        /// 查找窗口
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="title">窗口标题</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr FindWindow(IntPtr hwnd, string title);

        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="x">起始位置X</param>
        /// <param name="y">起始位置Y</param>
        /// <param name="nWidth">窗口宽度</param>
        /// <param name="nHeight">窗口高度</param>
        /// <param name="rePaint">是否重绘</param>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern void MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool rePaint);

        /// <summary>
        /// 获取窗口矩形
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="rect"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect);

        /// <summary>
        /// 向窗口发送信息
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="msg">信息</param>
        /// <param name="wParam">高字节</param>
        /// <param name="lParam">低字节</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int PostMessage(IntPtr hwnd, int msg, uint wParam, uint lParam);

        public const int WM_CLOSE = 0x10; //关闭命令

        public const int WM_KEYDOWN = 0x0100;//按下键

        public const int WM_KEYUP = 0x0101;//按键起来

        public const int VK_RETURN = 0x0D;//回车键

        /*public*/
        static bool IsWorking = false;

        /// <summary>
        /// 对话框标题
        /// </summary>
        /*public*/
        static string[] titles = new string[4] { "请选择", "提示", "错误", "警告" };

        /// <summary>
        /// 查找和移动窗口
        /// </summary>
        /// <param name="title">窗口标题</param>
        /// <param name="x">起始位置X</param>
        /// <param name="y">起始位置Y</param>
        public static void FindAndMoveWindow(string title, int x, int y)
        {
            Thread t = new Thread(() =>
            {
                IntPtr msgBox = IntPtr.Zero;
                while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;
                Rectangle r = new Rectangle();
                GetWindowRect(msgBox, out r);
                MoveWindow(msgBox, x, y, r.Width - r.X, r.Height - r.Y, true);
            });
            t.Start();
        }

        /// <summary>
        /// 查找和关闭窗口
        /// </summary>
        /// <param name="title">标题</param>
        public static void FindAndKillWindow(string title)
        {
            IntPtr ptr = FindWindow(IntPtr.Zero, title);
            if (ptr != IntPtr.Zero)
            {
                int ret = PostMessage(ptr, WM_CLOSE, 0, 0);
                Thread.Sleep(1000);
                ptr = FindWindow(IntPtr.Zero, title);
                if (ptr != IntPtr.Zero)
                {
                    PostMessage(ptr, WM_KEYDOWN, VK_RETURN, 0);
                    PostMessage(ptr, WM_KEYUP, VK_RETURN, 0);
                }
            }
        }

        public async static void AutoCloseMsgBox(string titleTxt, int delay_ms)
        {
            await Task.Delay(delay_ms);
            await Task.Run(() => FindAndKillWindow(titleTxt));
        }

        /// <summary>
        /// 查找和关闭窗口
        /// </summary>
        /*public*/
        static void FindAndKillWindow()
        {
            Thread t = new Thread(() =>
            {
                while (IsWorking)
                {
                    //按标题查找
                    foreach (string title in titles)
                    {
                        FindAndKillWindow(title);
                    }
                    Thread.Sleep(3000);
                }
            });

            t.Start();
        }
    }
}

这样延时关闭(调用):

private void btn_AutoClose_Click(object sender, EventArgs e)
{
    AutoCloseMsgBox("傻屌", 1000);
    Task.Run(() => { MessageBox.Show("沙雕,是你吗?", "傻屌"); });
}

private async void AutoCloseMsgBox(string titleTxt, int delay_ms)
{
    await Task.Delay(delay_ms);
    await Task.Run(() => MessageBoxHelper.FindAndKillWindow(titleTxt));
}




参考:

标签:IntPtr,MessageBox,窗口,title,C#,int,static,延时,public
From: https://www.cnblogs.com/huvjie/p/17118392.html

相关文章

  • SciTE settings for AutoHotkey files [官网example]
    #https://www.scintilla.org/ahk.properties原网站经常打不开,,备份#SciTEsettingsforAutoHotkeyfiles#whereisAutoHotkeyprograminstalledautohotkeydir=......
  • clion 2022 win10上编译的程序不可运行---问题解决
    会报错,0xc000007b网上说,可能是dll库丢失等问题,我试了无效我的修复过程如下编译的时候,默认是使用ninja,将buildtool改成make即可 ......
  • vue3watch
    vue3watch监视不到对象里面的oldValueref定义的基本类型响应式数据可以监视ref定义的多个基本类型响应式数据watch([name,age],(newV,oldV)=>{console.lo......
  • React.cloneElement
    React.cloneElement()接收三个参数第一个参数接收一个ReactElement,可以是真实的dom结构也可以是自定义的。第二个参数返回旧元素的props、key、ref。可以添加新的props,第......
  • typecho——typecho如何开启伪静态
    前言更新EasyBe的时候,突然发现博客一直没有配置伪静态,那就配置下吧,随便写个文档~内容!>伪静态配置主要分为两部分,nginx配置和开启地址重写功能;nginx将以下配置......
  • C语言学习笔记(八): 自定义数据类型
    结构体变量什么是结构体C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体结构体的成员可以是任何类型的变量,如整数,字符串,浮点数,其他结构体,指针......
  • 微服务学习计划——SpringCloud
    微服务学习计划——SpringCloud在学习并掌握了众多基础框架之后,我们的项目繁杂且难以掌握,那么我们就需要开启一门新的课程,也就是我们常说的微服务架构随着互联网行业的发......
  • Making a Plugin System with c++
      cplusplus.comTUTORIALS REFERENCE ARTICLES FORUM signup login[Legacyversion]C++TutorialsReferenceArticlesForum......
  • pandas中数据选取2(loc与iloc的用法)
    pandas中loc与iloc的用法a={"name":["lemon","jack","peter","Emma","james"],"city":["长沙","上海","深圳","北京","北京"],"a":[80,......
  • 代码随想录算法训练营第二十四天|LeetCode 77. 组合
    77.组合文章:代码随想录(programmercarl.com)视频:带你学透回溯算法-组合问题(对应力扣题目:77.组合)|回溯法精讲!_哔哩哔哩_bilibili思路:那么我把组合问题抽象为如下树形......