Application Example: VC ++ automatically closes the advertisement window

Source: Internet
Author: User
Application Example: VC ++ realizes automatic closing of the advertising window -- Linux general technology-Linux programming and kernel information. The following is a detailed description. I. Introduction

When we browse the Internet, some Advertisement Windows will pop up automatically when we visit some websites, many personal homepages even bring up several or even a dozen Advertisement Windows to make money by using online advertisements. It is very troublesome to close these windows one by one, and it will occupy a large amount of system resources if not, so many people hate this, as a programmer, you can use your own technical advantages to compile some suitable gadgets based on your actual needs. Therefore, this article introduces how to use software programming to automatically close the advertisement window, in order to play an effective role.

Ii. program design ideas and implementation

First, analyze the general characteristics of the advertising window. The advertisement window is usually dynamically popped up through the Javascript script or VBScript script language on the home page. The essence is an IE window. However, most Windows do not have menus, tool bars, and other things after they are created. Therefore, we can search for IE Windows and then determine whether there are menus, tool bars, and other things in the windows, if there is no such window, it is regarded as an advertisement window. Although such judgment is not very strict, some normal Windows may be closed by mistake in actual use, however, the probability of occurrence is quite small. It can be ignored. In addition, this often happens in chat rooms, so you only need to close it before chatting and run the program only when browsing the Web page.

The implementation of the program is not complex. Because the ad forms popped up at any time after the program runs, you should set a timer to search for the following ad windows at intervals, if yes, disable it; otherwise, continue monitoring. Therefore, you can use the SetTimer () function to start the timer in the program initialization entry function. The following describes some key code based on the actual code:

Start enumeration search for each window from the first window. If the first window exists, you can search for the next window by GetWindow (GW_HWNDNEXT) in the while loop, determine whether it is an advertisement form in the form until the last form is searched:

CWnd * pMainWnd = AfxGetMainWnd ()-> GetWindow (GW_HWNDFIRST );
While (pMainWnd)
{
......
PMainWnd = pMainWnd-> GetWindow (GW_HWNDNEXT );
}

To determine whether the window is an advertisement window, you can use the GetClassName () function to obtain the window type to determine whether the window is an IE window:

CString strClassName;
GetClassName (pMainWnd-> m_hWnd, strClassName. GetBufferSetLength (100), 100 );

You do not need to consider any forms that are not of the IE type. You can compare the strClassName window type you just obtained with "IEFrame" for judgment, in the IE window, you can proceed with the following judgment:

// Obtain the sub-window pointer Based on the handle
CWnd * pChildWnd = CWnd: FromHandle (finddomainwex (pMainWnd-> m_hWnd, NULL, "Worker", NULL ));
If (! PChildWnd)
PChildWnd = CWnd: FromHandle (find+wex (pMainWnd-> m_hWnd, NULL, "WorkerA", NULL ));

If the child window exists, use a similar method to obtain the next child window pointer:

CWnd * pChildNextWnd = CWnd: FromHandle (find+wex (pMainWnd-> m_hWnd, pChildWnd-> m_hWnd, "Worker", NULL ));
If (! PChildNextWnd)
PChildNextWnd = CWnd: FromHandle (finddomainwex (pMainWnd-> m_hWnd, pChildWnd-> m_hWnd, "WorkerA", NULL ));

If the window exists, you can verify that the current window is visible in the next step to determine that it is indeed an advertisement window. You can get the current window style through GetWindowLong, then, the logic operation can be used to determine whether the current window has the visual effect of WS_VISIBLE:

If (pChildNextWnd)
{
If (! (GetWindowLong (pChildWnd-> m_hWnd, GWL_STYLE) & WS_VISIBLE ))
{
PTempWnd = NULL;
PTempWnd = pMainWnd;
// Close the advertisement window
......
Return;
}
}

Now you can accurately determine which advertisement window is used. You just need to close it. The previous Code has saved the window pointer we want to close to the variable pTempWnd, therefore, we can close the WM_CLOSE message by simply sending it:

PTempWnd-> PostMessage (WM_CLOSE, 0, 0 );

Iii. Summary

This article only introduces the core code for closing the advertising window. In actual programming, many practical factors need to be taken into account. For example, to save the screen space, it is best to make it into the system tray mode; because the memory needs to reside for a long time, we should try to control the program size and adopt the Release version. The content described in this article is universal and can be used to monitor a process. After the process is started, the program triggers an event to process it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.