BLOG.CSHARPHELPER.COM: Close Mozilla Firefox popups in C#
Close Mozilla Firefox popups in C#
Some browser tools try to block advertising popups, but lately some have been sneaking through on my system so I wrote this program to stop them. Get your Firefox windows set up the way you want them and then start the program. Later when a web page tries to display a popup, the program will close it.
The example List the currently running desktop windows in C# explains how to list the running desktop windows. This example uses thue same technique to make a list of windows when it starts. Later if a new window appears that ends with the string "Mozilla Firefox," the program stops it.
When the program starts, it uses the following code to make a list of desktop windows.
// Make a list of the current Firefox windows. private List Handles = new List(); private List Titles = new List(); private void Form1_Load(object sender, EventArgs e) { // Get the desktop windows. List handles = new List(); List titles = new List(); DesktopWindowsStuff.GetDesktopWindowHandlesAndTitles(out handles, out titles);
// Save the Firefox windows. for (int i = 0; i < handles.Count; i++) { if (titles[i].EndsWith("Mozilla Firefox")) { Handles.Add(handles[i]); Titles.Add(titles[i]); } }
// Display the results. lstAllowedWindows.DataSource = Titles; }
The code first creates lists to hold the handles and titles of the current Firefox windows. Then the form's Load event handler calls the GetDesktopWindowHandlesAndTitles method described in the previous example. It loops through the returned windows and saves those with titles ending in "Mozilla Firefox."
The program's Timer uses the following code to check for new windows every second.
// Look for new Firefox windows. private void tmrCheckForPopups_Tick(object sender, EventArgs e) { // Get the desktop windows. List handles = new List(); List titles = new List(); DesktopWindowsStuff.GetDesktopWindowHandlesAndTitles(out handles, out titles);
// Look for new Firefox windows. for (int i = 0; i < handles.Count; i++) { if (titles[i].EndsWith("Mozilla Firefox")) { // See if this is a new window. if (!Handles.Contains(handles[i])) { // It's new. Stop it. DesktopWindowsStuff.StopWindow(handles[i]); lstStoppedWindows.Items.Add(titles[i]);
// Add it to the list of allowed handles so we don't // try to stop it again if it takes a while to close. Handles.Add(handles[i]); } } } }
The code calls GetDesktopWindowHandlesAndTitles again to get a list of the current windows. It then loops through them looking for windows with titles that end in "Mozilla Firefox." When it finds such a window, the program checks whether the Handles list already contains that window's handle. If the handle is not in the list, the program uses the StopWindow method described next to stop the window. It displays the window's title in the stopped windows ListBox and adds the window's handle to the Handles list so it won't try to stop the window again. This prevents the program from trying to stop a window several times if it takes a little while to close.
The following code shows how the StopWindow method works.
[return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); ... // Send a message to a process. private static void PostMessageToWindow(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { if (!PostMessage(hWnd, msg, wParam, lParam)) { // An error occured. throw new Win32Exception(Marshal.GetLastWin32Error()); } }
// Stop a window. public static void StopWindow(IntPtr hWnd) { PostMessageToWindow(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); }
This code fragment starts by declaring the PostMessage API function. This function sends a message to the window with the indicated handle.
The PostMessageToWindow method simply calls the PostMessage API function.
Finally the StopWindow method calls PostMessageToWindow to send a window the WM_CLOSE message to tell it to close.
Note that this method only works if the popup window's title ends with "Mozilla Firefox."
Comments