When browsing some websites with IE, the homepage of the website will pop up some ad pages. So whenever you open a page like this, you have to manually close the ad page, feel more trouble. So, can you write a program to determine if the open Web page is a pop-up ad, and then automatically close the ads to avoid the trouble of manually closing the pop-up window each time? Some of the solutions are introduced in some newspapers, and the following methods for intercepting messages via the hook function are discussed:
1, pop-up advertising box is also the IE Browse window, generally speaking, it is a no menu, no toolbar window. So you can open a window on the desktop, first determine whether the window type is "Ieframe", and then determine whether Ieframe's child window type "WORKERW" is not visible (this is the characteristics of the General pop-up Ad window), so you can send a closed message to the window. The above steps can generally automatically close most pop-up ads box.
2, through the Windows programming hooks (hook) function to solve the interception of open windows on the desktop message. The basic principle of the hook function is to register some actions of the Windows system, and when these events occur, the preset callback function is invoked first, and the callback function is processed by the original function. The function of the callback function here is to implement the functions described in 1. Because the hook function implemented here is to monitor messages generated by other process windows, it must be implemented with a dynamic link library.
The main code is as follows:
In the dynamic link library, implement the hook callback function.
lresult CALLBACK callwndproc (
int ncode,//Hook code
WPARAM WPARAM,//current-process flag
LPARAM lPa RAM/address of structure with message data
{
Cwpstruct *PCWP = NULL;
CString strClassName;
hwnd hwnd = NULL;
unsigned long ulstyle = 0;
BOOL bisclosed = FALSE;
if (ncode < 0)
{
return CallNextHookEx (Ghook, Ncode, WParam, LParam);
}
if (NULL!= lParam)
{
PCWP = (cwpstruct *) LParam;
if (Wm_showwindow = = pcwp->message)
{
:: GetClassName (Pcwp->hwnd, Strclassname.getbuffersetlength (128), 128);
//ie window is of type Ieframe
if (0 = = Strclassname.comparenocase ("Ieframe"))
{
HWnd =:: GetWindow (Pcwp->hwnd, Gw_child);
if (NULL = hWnd)
{
bisclosed = TRUE;
}
while (NULL!= hWnd)
{
:: GetClassName (hwnd, Strclassname.getbuffersetlength (127), 127);
Ieframe's child window contains the window type WORKERW
if (0 = strclassname.comparenocase ("Workerw"))
{
//If WORKERW is not visible, it is usually an ad pop-up window
if (0 = (:: GetWindowLong (HWnd),Gwl_style) & ws_visible))
{
bisclosed = TRUE;
Break
}
break;
}
Else
{
bisclosed = TRUE;
}
hwnd =:: GetWindow (hwnd, gw_hwndnext);
}
//If the IE ad pop-up window closes
if (TRUE = = bisclosed)
{
::P ostmessage (Pcwp->hwnd, wm_close, 0, 0);
}
}
}
}
return CallNextHookEx (Ghook, Ncode, WParam, LParam);
}
in the main program, call the Callwndproc in the DLL.
//Get dynamic link library handle
hinstance Hmodhook = GetModuleHandle ("CloseWndDll.dll");
if (NULL = = Hmodhook)
{
return TRUE;
}
//Set hook function
if (NULL = = Ghook)
{
Ghook = SetWindowsHookEx (Wh_callwndproc,
(HookProc) GetProcAddress ( Hmodhook, "Callwndproc"), Hmodhook, 0);
}
When the main program exits, notice that the hook unload function is invoked.
//Uninstall hook function
if (NULL!= ghook)
{
UnhookWindowsHookEx (ghook);
Ghook = NULL;
}