Click here to Skip to main content
15,885,278 members
Articles / Web Development / HTML

Catch All Bugs with BugTrap!

Rate me:
Please Sign up or sign in to vote.
4.34/5 (84 votes)
31 Jan 2009MIT5 min read 1.8M   9.1K   292  
A tool that can catch unhandled errors and exceptions, and deliver error reports to remote support servers
/*
 * This is a part of the BugTrap package.
 * Copyright (c) 2005-2007 IntelleSoft.
 * All rights reserved.
 *
 * Description: Floating information message.
 * Author: Maksim Pyatkovskiy.
 *
 * This source code is only intended as a supplement to the
 * BugTrap package reference and related electronic documentation
 * provided with the product. See these sources for detailed
 * information regarding the BugTrap package.
 */

#include "StdAfx.h"
#include "MessageTip.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CMessageTip::~CMessageTip()
{
	if (m_hWnd)
		DestroyWindow();
}

/**
 * @param hwndParent - parent window handle.
 * @return true if tool-tip window was normally created.
 */
bool CMessageTip::Create(HWND hwndParent)
{
	HWND hwndToolTip = CWindowImpl<CMessageTip, CToolTipCtrl>::Create(hwndParent, NULL, NULL,
																	  WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP | TTS_BALLOON,
																	  WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
	if (hwndToolTip != NULL)
	{
		int nHorDlgUnit = LOWORD(GetDialogBaseUnits());
		SetMaxTipWidth(nHorDlgUnit * 50);
		TOOLINFO tinfo;
		ZeroMemory(&tinfo, sizeof(tinfo));
		tinfo.cbSize = sizeof(tinfo);
		if (AddTool(&tinfo) == FALSE)
			DestroyWindow();
		else
			return true;
	}
	return false;
}

/**
 * @param hwndCtrl - input control handle.
 * @param uMessageID - message resource identifier.
 */
void CMessageTip::ShowMessage(HWND hwndCtrl, UINT uMessageID)
{
	CWindow ctlWnd(hwndCtrl);
	ctlWnd.SetFocus();
	CRect rcWnd;
	ctlWnd.GetWindowRect(&rcWnd);
	ShowMessage(uMessageID, rcWnd.CenterPoint());
}

/**
 * @param hwndCtrl - input control handle.
 * @param pszMessage - message text.
 */
void CMessageTip::ShowMessage(HWND hwndCtrl, PCTSTR pszMessage)
{
	CWindow ctlWnd(hwndCtrl);
	ctlWnd.SetFocus();
	CRect rcWnd;
	ctlWnd.GetWindowRect(&rcWnd);
	ShowMessage(pszMessage, rcWnd.CenterPoint());
}

/**
 * @param uMessageID - message resource identifier.
 * @param ptStem - stem point.
 */
void CMessageTip::ShowMessage(UINT uMessageID, const POINT& ptStem)
{
	TOOLINFO tinfo;
	ZeroMemory(&tinfo, sizeof(tinfo));
	tinfo.cbSize = sizeof(tinfo);
	tinfo.hinst = _Module.GetResourceInstance();
	tinfo.uFlags = TTF_TRACK;
	tinfo.lpszText = (PTSTR)UIntToPtr(uMessageID);
	SetToolInfo(&tinfo);
	TrackPosition(ptStem.x, ptStem.y);
	TrackActivate(&tinfo, TRUE);
	SetTimer(HIDE_TIP_TIMER_ID, HIDE_TIP_TIMEOUT);
}

/**
 * @param pszMessage - message text.
 * @param ptStem - stem point.
 */
void CMessageTip::ShowMessage(PCTSTR pszMessage, const POINT& ptStem)
{
	TOOLINFO tinfo;
	ZeroMemory(&tinfo, sizeof(tinfo));
	tinfo.cbSize = sizeof(tinfo);
	tinfo.uFlags = TTF_TRACK;
	tinfo.lpszText = (PTSTR)pszMessage;
	SetToolInfo(&tinfo);
	TrackPosition(ptStem.x, ptStem.y);
	TrackActivate(&tinfo, TRUE);
	SetTimer(HIDE_TIP_TIMER_ID, HIDE_TIP_TIMEOUT);
}

/**
 * @param uTimerID - timer identifier.
 * @param pfnTimerProc - timer handler.
 */
void CMessageTip::OnTimer(UINT uTimerID, TIMERPROC /*pfnTimerProc*/)
{
	if (uTimerID == HIDE_TIP_TIMER_ID)
	{
		KillTimer(HIDE_TIP_TIMER_ID);
		HideMessage();
	}
	else
		SetMsgHandled(FALSE);
}

void CMessageTip::HideMessage()
{
	TOOLINFO tinfo;
	ZeroMemory(&tinfo, sizeof(tinfo));
	tinfo.cbSize = sizeof(tinfo);
	TrackActivate(&tinfo, FALSE);
}

namespace MsgTip
{
	/// Global information message.
	static CMessageTip g_MessageTip;

	/**
	 * Initialize global message tip object.
	 * @return true if message tip has been initialized.
	 */
	inline static BOOL CreateMessage()
	{
		return (g_MessageTip.m_hWnd == NULL ? g_MessageTip.Create(NULL) : TRUE);
	}

	/**
	 * @param hwndCtrl - input control handle.
	 * @param uMessageID - message resource identifier.
	 */
	void ShowMessage(HWND hwndCtrl, UINT uMessageID)
	{
		if (CreateMessage())
			g_MessageTip.ShowMessage(hwndCtrl, uMessageID);
	}

	/**
	 * @param hwndCtrl - input control handle.
	 * @param pszMessage - message text.
	 */
	void ShowMessage(HWND hwndCtrl, PCTSTR pszMessage)
	{
		if (CreateMessage())
			g_MessageTip.ShowMessage(hwndCtrl, pszMessage);
	}

	/**
	 * @param uMessageID - message resource identifier.
	 * @param ptStem - stem point.
	 */
	void ShowMessage(UINT uMessageID, const POINT& ptStem)
	{
		if (CreateMessage())
			g_MessageTip.ShowMessage(uMessageID, ptStem);
	}

	/**
	 * @param pszMessage - message text.
	 * @param ptStem - stem point.
	 */
	void ShowMessage(PCTSTR pszMessage, const POINT& ptStem)
	{
		if (CreateMessage())
			g_MessageTip.ShowMessage(pszMessage, ptStem);
	}

	void HideMessage()
	{
		if (g_MessageTip.m_hWnd != NULL)
			g_MessageTip.HideMessage();
	}
}

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, along with any associated source code and files, is licensed under The MIT License


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

Comments and Discussions