下面函数每隔1秒遍历一次系统窗口,发现包含指定字符串的窗口就关闭(关闭的只是此窗口,而不是进程,在多窗口进程如IE、资源管理器等进程中很管用)
编写/调试环境:VC++ 6.0
#include <windows.h>
#include <Tlhelp32.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
char strTitle[80];
HWND hwnd=NULL,afterhwnd=NULL;
char msg[2][30]={"记事本","百度"}; //可以在此添加需要关闭窗口中包含的字符串
while(1)
{
for(afterhwnd=NULL;(hwnd=FindWindowEx(NULL,afterhwnd,NULL,NULL));afterhwnd=hwnd)
{
GetWindowText(hwnd,strTitle,80); //遍历窗口
for(int i=0;i<2;i++)
if(strstr(strTitle,msg ))
{
PostMessage(hwnd,WM_CLOSE,0,0);
break;
}
}
//自阻塞1秒
Sleep(1000);
}
return 0;
}
编写/调试环境:VC++ 6.0
#include <windows.h>
#include <Tlhelp32.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
char strTitle[80];
HWND hwnd=NULL,afterhwnd=NULL;
char msg[2][30]={"记事本","百度"}; //可以在此添加需要关闭窗口中包含的字符串
while(1)
{
for(afterhwnd=NULL;(hwnd=FindWindowEx(NULL,afterhwnd,NULL,NULL));afterhwnd=hwnd)
{
GetWindowText(hwnd,strTitle,80); //遍历窗口
for(int i=0;i<2;i++)
if(strstr(strTitle,msg ))
{
PostMessage(hwnd,WM_CLOSE,0,0);
break;
}
}
//自阻塞1秒
Sleep(1000);
}
return 0;
}