关闭ShowDialog的模态窗口

人盡茶涼 提交于 2020-02-21 14:05:52
 [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string a, string b);

        [DllImport("user32.dll")]
        private static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
   Timer timer = new Timer();
            timer.Tick += (obj, arg) =>
                {
                    //1、尝试关闭ColorDialog
                    IntPtr ptr1 = FindWindow(null, "颜色");
                    if (ptr1 != IntPtr.Zero)
                    {
                        PostMessage(ptr1, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("ColorDialog已关闭");
                    }
                    //2、尝试关闭FolderBrowserDialog
                    IntPtr ptr2 = FindWindow(null, "浏览文件夹");
                    if (ptr2 != IntPtr.Zero)
                    {
                        PostMessage(ptr2, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("FolderBrowserDialog已关闭");
                    }
                    //3、尝试关闭FontDialog
                    IntPtr ptr3 = FindWindow(null, "字体");
                    if (ptr3 != IntPtr.Zero)
                    {
                        PostMessage(ptr3, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("FontDialog已关闭");
                    }
                    //4、尝试关闭OpenFileDialog
                    IntPtr ptr4 = FindWindow(null, "打开");
                    if (ptr4 != IntPtr.Zero)
                    {
                        PostMessage(ptr4, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("OpenFileDialog已关闭");
                    }
                    //5、尝试关闭SaveFileDialog
                    IntPtr ptr5 = FindWindow(null, "另存为");
                    if (ptr5 != IntPtr.Zero)
                    {
                        PostMessage(ptr5, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("SaveFileDialog已关闭");
                    }
                    //6、尝试关闭MessageBox
                    IntPtr ptr6 = FindWindow(null, "测试消息");
                    if (ptr6 != IntPtr.Zero)
                    {
                        PostMessage(ptr6, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("MessageBox已关闭");
                    }
                    //7、尝试关闭自定义窗口(Show)
                    IntPtr ptr7 = FindWindow(null, "自定义窗口(Show)");
                    if (ptr7 != IntPtr.Zero)
                    {
                        PostMessage(ptr7, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("自定义窗口(Show)已关闭");
                    }
                    //8、尝试关闭自定义窗口(ShowDialog)
                    IntPtr ptr8 = FindWindow(null, "自定义窗口(ShowDialog)");
                    if (ptr8 != IntPtr.Zero)
                    {
                        PostMessage(ptr8, 0x0010, IntPtr.Zero, IntPtr.Zero);
                        SetInfoBar("自定义窗口(ShowDialog)已关闭");
                    }
                };
            timer.Interval = 5000;
            timer.Enabled = true;
            timer.Start();

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!