Click here to Skip to main content
15,886,067 members
Articles / Web Development / HTML

SurfHelper: A popup window killer and history cleaner

Rate me:
Please Sign up or sign in to vote.
4.77/5 (13 votes)
15 Nov 2001 190.1K   3.7K   46  
A free tool to remove popup windows, clear history, control window properties of IE, and more.
#ifndef __PopupManagerTab_h__
#define __PopupManagerTab_h__

extern HWND g_hMainWnd;

class CPopupManagerTab : public CWindowImpl< CPopupManagerTab , CWindow, 
									CWinTraits < WS_CHILD | WS_VISIBLE, 0> >
{
public:
	CPopupTitleDlg	m_TitleDlg;
	CPopupUrlDlg	m_UrlDlg;
	CPopupLogDlg	m_LogDlg;
	int				m_nTabCurrent;

	CPopupManagerTab()
	{
	}

	~CPopupManagerTab()
	{
		if (m_hWnd)
		{
			DestroyWindow();
		}
	}

	void OnFinalMessage(HWND hWnd)
	{
		::PostQuitMessage(0);
	}

	BEGIN_MSG_MAP(CMySubclassedWindow)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
		COMMAND_ID_HANDLER(ID_LOG_EXCLUDE, OnLogExclude)
		COMMAND_ID_HANDLER(ID_LOG_GOTO_FILTER, OnLogGotoFilter)
		COMMAND_ID_HANDLER(ID_LOG_CLEAR, OnLogClear)
		COMMAND_ID_HANDLER(ID_LOG_COPY_URL, OnCopyUrl)
		COMMAND_ID_HANDLER(ID_POPUP_SAVE_LOG, OnSaveLog)
	END_MSG_MAP()

	void Init()
	{
		LONG lStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
		::SetWindowLong(m_hWnd, GWL_STYLE, lStyle | WS_CLIPCHILDREN);

		m_nTabCurrent = 0;

		m_TitleDlg.Create(m_hWnd);
		m_TitleDlg.ShowWindow(SW_SHOW);

		m_UrlDlg.Create(m_hWnd);
		m_UrlDlg.ShowWindow(SW_HIDE);

		m_LogDlg.Create(m_hWnd);
		m_LogDlg.ShowWindow(SW_HIDE);

		SendMessage(m_hWnd, TCM_SETCURFOCUS, 0, 0L);
		ReSize();
	}

	void ReSize()
	{
		CRect rectTab, rectItem;
		int nX, nY, nXc, nYc;

		GetClientRect(&rectTab);
		SendMessage(m_hWnd, TCM_GETITEMRECT, 0, (LPARAM)&rectItem);

		nX=rectItem.left;
		nY=rectItem.bottom+1;
		nXc=rectTab.right-rectItem.left-1;
		nYc=rectTab.bottom-nY-1;

		m_TitleDlg.SetWindowPos(HWND_TOP, nX, nY, nXc, nYc, SWP_NOREDRAW);
		m_UrlDlg.SetWindowPos(HWND_TOP, nX, nY, nXc, nYc, SWP_NOREDRAW);
		m_LogDlg.SetWindowPos(HWND_TOP, nX, nY, nXc, nYc, SWP_NOREDRAW);
	}

	LRESULT OnClick()
	{
		m_nTabCurrent = SendMessage(m_hWnd, TCM_GETCURFOCUS, 0, 0L);

		switch (m_nTabCurrent)
		{
		case 0:
			m_UrlDlg.ShowWindow(SW_HIDE);
			m_LogDlg.ShowWindow(SW_HIDE);
			if (!m_TitleDlg.IsWindowVisible())
			{
				m_TitleDlg.ShowWindow(SW_SHOW);
			}
			break;
		case 1:
			m_TitleDlg.ShowWindow(SW_HIDE);
			m_LogDlg.ShowWindow(SW_HIDE);
			m_UrlDlg.ShowWindow(SW_SHOW);
			break;
		case 2:
			m_TitleDlg.ShowWindow(SW_HIDE);
			m_UrlDlg.ShowWindow(SW_HIDE);
			m_LogDlg.ShowWindow(SW_SHOW);
			break;
		}

		return 0;
	}

	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		ReSize();
		return 0;
	}

	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		HBRUSH hBrush;
		CRect rectTab, rectItem;

		GetClientRect(&rectTab);
		SendMessage(m_hWnd, TCM_GETITEMRECT, 0, (LPARAM)&rectItem);

		rectItem.left = rectTab.left;
		rectItem.top = rectTab.top;
		rectItem.right = rectTab.right;

		hBrush = GetSysColorBrush(COLOR_BTNFACE);
		FillRect((HDC)wParam, &rectItem, hBrush);
		return 0;
	}

	void AddLog(long lIndex)
	{
		CPopupLog* pLog;
		if (lIndex >= 0 && lIndex < g_Global.m_vecPopupLog.size())
		{
			pLog = g_Global.m_vecPopupLog[lIndex];

			TCHAR szBuffer[128];
			_stprintf(szBuffer, _T("%d-%02d-%02d %02d:%02d:%02d"),
				pLog->m_timeOccur.wYear, pLog->m_timeOccur.wMonth,
				pLog->m_timeOccur.wDay, pLog->m_timeOccur.wHour,
				pLog->m_timeOccur.wMinute, pLog->m_timeOccur.wSecond);

			LV_ITEM	lvi;
			lvi.iItem = lIndex;
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT | LVIF_PARAM;
			lvi.lParam = lIndex;
			lvi.pszText = szBuffer;
			int nIndex = m_LogDlg.m_listLog.InsertItem(&lvi);

			CString sType;
			if (pLog->m_lKillType == KILLED_BY_SIZE)
			{
				sType.LoadString(IDS_KILLED_BY_SIZE);
			}
			else if (pLog->m_lKillType == KILLED_BY_TITLE)
			{
				sType.LoadString(IDS_KILLED_BY_TITLE);
			}
			else //if (pLog->m_lKillType == KILLED_BY_URL)
			{
				sType.LoadString(IDS_KILLED_BY_URL);
			}

			m_LogDlg.m_listLog.SetItemText(nIndex, 1, pLog->m_sTitle);
			m_LogDlg.m_listLog.SetItemText(nIndex, 2, pLog->m_sUrl);
			m_LogDlg.m_listLog.SetItemText(nIndex, 3, sType);
			m_LogDlg.m_listLog.SetItemText(nIndex, 4, pLog->m_sFilter);

			//play sound
			if (g_Global.m_bEnableSound)
			{
				MessageBeep(MB_OK);
			}
		}
	}

	void AddPopupFilter(CFilterSource* pSource)
	{
		CAddFilterDlg dlg;
		if (pSource)
		{
			dlg.m_sTitle = pSource->m_sTitle;
			dlg.m_sUrl = pSource->m_sUrl;
			dlg.m_bAddToExclusions = pSource->m_bAddToExclusions;
			dlg.m_bExclusionsOnly = pSource->m_bExclusionsOnly;
			dlg.m_bShowPopupList = FALSE;
		}
		else
		{
			dlg.m_bAddToExclusions = FALSE;
			dlg.m_bExclusionsOnly = FALSE;
			dlg.m_bShowPopupList = TRUE;
		}

		if (dlg.DoModal() == IDOK)
		{
			if (dlg.m_bTitleFilter)
			{
				if (dlg.m_bAddToExclusions)
				{
					m_TitleDlg.ExclusionAdd(dlg.m_sTitle);
				}
				else
				{
					m_TitleDlg.BlacklistAdd(dlg.m_sTitle);
				}
			}
			else
			{
				if (dlg.m_bAddToExclusions)
				{
					m_UrlDlg.ExclusionAdd(dlg.m_sUrl);
				}
				else
				{
					m_UrlDlg.BlacklistAdd(dlg.m_sUrl);
				}
			}
		}
	}


/////////////////////////////////////////////////////////////////////////////
//	Command handler for CLogDlg
/////////////////////////////////////////////////////////////////////////////

	LRESULT OnLogExclude(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		int nIndex = m_LogDlg.m_listLog.GetNextItem(-1,LVNI_SELECTED);
		DWORD i = m_LogDlg.m_listLog.GetItemData(nIndex);

		CFilterSource source;
		g_Global.m_lockPopupLog.ReadLock();
		CPopupLog* pLog = g_Global.m_vecPopupLog[i];
		source.m_sTitle = pLog->m_sTitle;
		source.m_sUrl = pLog->m_sUrl;
		source.m_bAddToExclusions = TRUE;
		source.m_bExclusionsOnly = TRUE;
		g_Global.m_lockPopupLog.ReadUnlock();

		AddPopupFilter(&source);
		return 0;
	}

	LRESULT OnLogGotoFilter(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		int nIndex = m_LogDlg.m_listLog.GetNextItem(-1,LVNI_SELECTED);
		DWORD i = m_LogDlg.m_listLog.GetItemData(nIndex);

		g_Global.m_lockPopupLog.ReadLock();
		CPopupLog* pLog = g_Global.m_vecPopupLog[i];

		CListViewCtrl* pListView;
		if (pLog->m_lKillType == KILLED_BY_TITLE)
		{
			m_nTabCurrent = 0;
			pListView = &m_TitleDlg.m_listBlackList;
		}
		else if (pLog->m_lKillType == KILLED_BY_URL)
		{
			m_nTabCurrent = 1;
			pListView = &m_UrlDlg.m_listBlackList;
		}

		LVFINDINFO lvfi;
		lvfi.flags = LVFI_STRING;
		lvfi.psz = pLog->m_sFilter.LockBuffer();
		int nItem = pListView->FindItem(&lvfi, -1);
		if (nItem >= 0)
		{
			pListView->SetFocus();
			pListView->SetItemState(nItem,
				LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
			pListView->EnsureVisible(nItem, FALSE);
			SendMessage(m_hWnd, TCM_SETCURFOCUS, m_nTabCurrent, 0L);
			OnClick();
		}
		pLog->m_sFilter.UnlockBuffer();
		g_Global.m_lockPopupLog.ReadUnlock();
		return 0;
	}

	LRESULT OnLogClear(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		g_Global.ClearLog();
		m_LogDlg.m_listLog.DeleteAllItems();
		return 0;
	}

	LRESULT OnCopyUrl(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		//get url
		CString sUrl;
		LPTSTR pUrl;
		int nIndex = m_LogDlg.m_listLog.GetNextItem(-1,LVNI_SELECTED);
		pUrl = sUrl.GetBuffer(MAX_PATH);
		m_LogDlg.m_listLog.GetItemText(nIndex, 2, pUrl, MAX_PATH);
		sUrl.ReleaseBuffer();

		//put url in clipboard
		HGLOBAL hClipBuffer;
		LPTSTR pBuffer = NULL;

		if(::OpenClipboard(NULL))
		{
			::EmptyClipboard();
			hClipBuffer = GlobalAlloc(GMEM_DDESHARE, 
				(sUrl.GetLength() + 1)*sizeof(TCHAR));
			pBuffer = (LPTSTR)GlobalLock(hClipBuffer);
			_tcscpy(pBuffer, sUrl);
			GlobalUnlock(hClipBuffer);
			::SetClipboardData(CF_TEXT, hClipBuffer);
			::CloseClipboard();
		}
		return 0;
	}

	LRESULT OnSaveLog(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		::PostMessage(g_hMainWnd, WM_COMMAND, ID_POPUP_SAVE_LOG, 0L);
		return 0;
	}
};


#endif //__PopupManagerTab_h__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions