使用Win API查找并关闭窗口

介绍 本文解释了如何使用Win API查找和关闭窗口。 找到并关闭窗口 发现窗户 FindWindow函数检索顶级窗口的句柄,该顶级窗口的类名和窗口名与指定的字符串匹配。此函数不搜索子窗口。此函数不执行区分大小写的搜索。 隐藏,复制Code

FindWindow(string lpClassName,string lpWindowName) 

使用spy++查找类名和窗口名 是一个基于win32的实用程序,它提供了系统进程、线程、窗口和窗口消息的图形化视图。使用窗口查找工具,您可以找到所选窗口的属性。 步骤1:安排你的窗口,使Spy++和主题窗口可见。 第二步:从“间谍”菜单中,选择“查找窗口”,打开“查找窗口”对话框。 第三步:拖动Finder工具到所需的窗口。当您拖动该工具时,对话框中将显示窗口详细信息。(句柄、标题(窗口名)、类名) 隐藏,复制Code

using Microsoft.Win32;

[DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName,string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
            
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

private void closeWindow()
        {
            // retrieve the handler of the window  
            int iHandle = FindWindow("Notepad", "Untitled - Notepad");
            if (iHandle > 0)
            {
                // close the window using API        
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }  
        }	

历史 2007年12月19日:初任 本文转载于:http://www.diyabc.com/frontweb/news8465.html

原文地址:https://www.cnblogs.com/Dincat/p/13468001.html