?? balloonhelp.cpp
字號:
// ******************************************************************************// BalloonHelp.cpp : implementation file// Copyright 2001-2002, Joshua Heyer// You are free to use this code for whatever you want, provided you// give credit where credit is due. (I seem to get a lot of questions// about that statement... All i mean is, don't copy huge bits of code// and then claim you wrote it. You don't have to put my name in an about// box or anything. Though i'm not going to stop you if that's really what// you want :~) )// I'm providing this code in the hope that it is useful to someone, as i have// gotten much use out of other peoples code over the years.// If you see value in it, make some improvements, etc., i would appreciate it // if you sent me some feedback.//// ******************************************************************************#include "stdafx.h"#include "BalloonHelp.h"// allow multimonitor-aware code on Win95 systems// comment out the first line if you have already define it in another file// comment out both lines if you don't care about Win95#define COMPILE_MULTIMON_STUBS#include "multimon.h"// debug helpers#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif//// constants that may not be defined if you don't have the latest SDK// (but i like to use them anyway)//#ifndef DFCS_HOT#define DFCS_HOT 0x1000#endif#ifndef AW_HIDE#define AW_HIDE 0x00010000
#define AW_BLEND 0x00080000
#endif#ifndef CS_DROPSHADOW#define CS_DROPSHADOW 0x00020000#endif#ifndef SPI_GETDROPSHADOW#define SPI_GETDROPSHADOW 0x1024#endif#ifndef SPI_GETTOOLTIPANIMATION#define SPI_GETTOOLTIPANIMATION 0x1016#endif#ifndef SPI_GETTOOLTIPFADE#define SPI_GETTOOLTIPFADE 0x1018#endif/////////////////////////////////////////////////////////////////////////////// CBalloonHelp// option constants (bits)const unsigned int CBalloonHelp::unCLOSE_ON_LBUTTON_UP = 0x0001;const unsigned int CBalloonHelp::unCLOSE_ON_MBUTTON_UP = 0x0002;const unsigned int CBalloonHelp::unCLOSE_ON_RBUTTON_UP = 0x0004;const unsigned int CBalloonHelp::unCLOSE_ON_LBUTTON_DOWN = 0x0008;const unsigned int CBalloonHelp::unCLOSE_ON_MBUTTON_DOWN = 0x0010;const unsigned int CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN = 0x0020;const unsigned int CBalloonHelp::unCLOSE_ON_MOUSE_MOVE = 0x0040;const unsigned int CBalloonHelp::unCLOSE_ON_KEYPRESS = 0x0080;const unsigned int CBalloonHelp::unCLOSE_ON_ANYTHING = CBalloonHelp::unCLOSE_ON_MOUSE_MOVE|CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_MBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_LBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_RBUTTON_UP|CBalloonHelp::unCLOSE_ON_MBUTTON_UP|CBalloonHelp::unCLOSE_ON_LBUTTON_UP;const unsigned int CBalloonHelp::unDELAY_CLOSE = 0x0100;const unsigned int CBalloonHelp::unDELETE_THIS_ON_CLOSE = 0x0200;const unsigned int CBalloonHelp::unSHOW_CLOSE_BUTTON = 0x0400;const unsigned int CBalloonHelp::unSHOW_INNER_SHADOW = 0x0800;const unsigned int CBalloonHelp::unSHOW_TOPMOST = 0x1000;const unsigned int CBalloonHelp::unDISABLE_XP_SHADOW = 0x2000;const unsigned int CBalloonHelp::unDISABLE_FADEIN = 0x4000;const unsigned int CBalloonHelp::unDISABLE_FADEOUT = 0x8000;const unsigned int CBalloonHelp::unDISABLE_FADE = CBalloonHelp::unDISABLE_FADEIN|CBalloonHelp::unDISABLE_FADEOUT;// layout constants (should prolly be configurable, but who's really gonna care?)const int CBalloonHelp::nTIP_TAIL = 20;const int CBalloonHelp::nTIP_MARGIN = 8;// class atom (why don't i do this the MFC way? Drop shadows!)ATOM CBalloonHelp::s_ClassAtom = NULL;ATOM CBalloonHelp::s_ClassAtomShadowed = NULL;// Kill timer#define ID_TIMER_CLOSE 1//// The launchers//
//
// Show a help balloon on screen// Parameters:// strTitle | Title of balloon// unTitle | Title of balloon (id of string resource)// strContent | Content of balloon// unContent | Content of balloon (id of string resource)// ptAnchor | point tail of balloon will be "anchor"ed to// szIcon | One of:// IDI_APPLICATION
// IDI_INFORMATION IDI_ASTERISK (same)
// IDI_ERROR IDI_HAND (same)
// IDI_EXCLAMATION IDI_WARNING (same)
// IDI_QUESTION
// IDI_WINLOGO// NULL (no icon)// unIconID | ID of icon to display (loaded from resources)// unOptions | One or more of:// : unCLOSE_ON_LBUTTON_UP | closes window on WM_LBUTTON_UP// : unCLOSE_ON_MBUTTON_UP | closes window on WM_MBUTTON_UP// : unCLOSE_ON_RBUTTON_UP | closes window on WM_RBUTTON_UP// : unCLOSE_ON_LBUTTON_DOWN | closes window on WM_LBUTTON_DOWN// : unCLOSE_ON_MBUTTON_DOWN | closes window on WM_MBUTTON_DOWN// : unCLOSE_ON_RBUTTON_DOWN | closes window on WM_RBUTTON_DOWN// : unCLOSE_ON_MOUSE_MOVE | closes window when user moves mouse past threshhold// : unCLOSE_ON_KEYPRESS | closes window on the next keypress message sent to this thread.// : unCLOSE_ON_ANYTHING | all of the above.// : unDELAY_CLOSE | when a user action triggers the close, begins timer. closes when timer expires.// : unSHOW_CLOSE_BUTTON | shows close button in upper right// : unSHOW_INNER_SHADOW | draw inner shadow in balloon// : unSHOW_TOPMOST | place balloon above all other windows// : unDISABLE_XP_SHADOW | disable Windows XP's drop-shadow effect (overrides system and user settings)// : unDISABLE_FADE | disable the fade-in/fade-out effects (overrides system and user settings)// : unDISABLE_FADEIN | disable the fade-in effect// : unDISABLE_FADEOUT | disable the fade-out effect// pParentWnd | Parent window. If NULL will be set to AfxGetMainWnd(), and anchor to screen// strURL | If not empty, when the balloon is clicked ShellExecute() will// | be called, with strURL passed in.// unTimeout | If not 0, balloon will automatically close after unTimeout milliseconds.//CBalloonHelp* CBalloonHelp::LaunchBalloon(const CString& strTitle, const CString& strContent,
const CPoint& ptAnchor,
LPCTSTR szIcon /*= IDI_EXCLAMATION*/,
unsigned int unOptions /*= unSHOW_CLOSE_BUTTON*/,
CWnd* pParentWnd /*= NULL*/,
const CString strURL /*= ""*/,
unsigned int unTimeout /*= 10000*/){ CBalloonHelp* pbh = new CBalloonHelp; if ( NULL != szIcon ) { // Note: Since i'm scaling the icon anyway, i'll allow it to become larger // than the standard small icon if the close button is. CSize sizeIcon(max(::GetSystemMetrics(SM_CXSIZE), ::GetSystemMetrics(SM_CXSMICON)), max(::GetSystemMetrics(SM_CYSIZE), ::GetSystemMetrics(SM_CYSMICON))); HICON hIcon = (HICON)::LoadImage(NULL, szIcon, IMAGE_ICON, sizeIcon.cx, sizeIcon.cy, LR_SHARED); if (NULL != hIcon) pbh->SetIconScaled(hIcon, sizeIcon.cx, sizeIcon.cy); } pbh->Create(strTitle, strContent, ptAnchor, unOptions|unDELETE_THIS_ON_CLOSE, pParentWnd, strURL, unTimeout, NULL);
return pbh;}
//
// The class
//
CBalloonHelp::CBalloonHelp(): m_fnAnimateWindow(NULL), m_unOptions(0), m_unTimeout(0), m_unTimerClose(0), m_strURL(""), m_ptAnchor(0,0), m_hwndAnchor(NULL), m_screenRect(0,0,0,0), m_strContent(""), m_nMouseMoveTolerance(3), // later retrieved from system m_ptMouseOrig(0,0), m_uCloseState(0), m_pTitleFont(NULL), m_pContentFont(NULL), m_crForeground(::GetSysColor(COLOR_INFOTEXT)), m_crBackground(::GetSysColor(COLOR_INFOBK)), m_hKeyboardHook(NULL),
m_hMouseHook(NULL),
m_hCallWndRetHook(NULL)
{ // retrieve window animation API if available HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL")); // can't imagine why that would fail, but might as well *look* safe... ;~) if ( NULL != hUser32 ) m_fnAnimateWindow = (FN_ANIMATE_WINDOW)GetProcAddress(hUser32, ("AnimateWindow")); else m_fnAnimateWindow = NULL; // get system tolerance values int nTol = 0; if ( ::SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH, 0, &nTol, 0) && nTol > 0 ) m_nMouseMoveTolerance = nTol; // setup hook procedures BHKeybHookThunk<CBalloonHelp>::InitThunk((TMFP)KeyboardHookProc, this);
BHMouseHookThunk<CBalloonHelp>::InitThunk((TMFP)MouseHookProc, this);
BHCallWndRetHookThunk<CBalloonHelp>::InitThunk((TMFP)CallWndRetProc, this);
}CBalloonHelp::~CBalloonHelp(){ if ( NULL != m_pTitleFont )
delete m_pTitleFont;
m_pTitleFont = NULL;
if ( NULL != m_pContentFont )
delete m_pContentFont; m_pContentFont = NULL;}
// Sets the font used for drawing the balloon title. Deleted by balloon, do not use CFont* after passing to this function.
void CBalloonHelp::SetTitleFont(CFont* pFont)
{
if ( NULL != m_pTitleFont )
delete m_pTitleFont;
m_pTitleFont = pFont;
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the font used for drawing the balloon content. Deleted by balloon, do not use CFont* after passing to this function.
void CBalloonHelp::SetContentFont(CFont* pFont)
{
if ( NULL != m_pContentFont )
delete m_pContentFont;
m_pContentFont = pFont;
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon (pass NULL to hide icon)
void CBalloonHelp::SetIcon(HICON hIcon)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
ICONINFO iconinfo;
if ( NULL != hIcon && ::GetIconInfo(hIcon, &iconinfo) )
{
SetIcon(iconinfo.hbmColor, iconinfo.hbmMask);
::DeleteObject(iconinfo.hbmColor);
::DeleteObject(iconinfo.hbmMask);
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon (pass NULL to hide icon)
void CBalloonHelp::SetIconScaled(HICON hIcon, int cx, int cy)
{
// i now have two device contexts and two bitmaps.
// i will select a bitmap in each device context,
// draw the icon into the first one,
// scale it into the second one,
// and set the second one as the balloon icon.
// This is a rather long process to get a scaled icon,
// but ensures maximum compatibility between different
// versions of Windows, while producing the best possible
// results on each version (quite good in WinNT and better, sorta ok in Win9x).
ICONINFO iconinfo;
if ( NULL != hIcon && ::GetIconInfo(hIcon, &iconinfo) )
{
BITMAP bm;
if (::GetObject(iconinfo.hbmColor, sizeof(bm),(LPVOID)&bm))
{
CDC dc;
CDC dcTmp1;
CDC dcTmp2;
CBitmap bmpIcon;
CBitmap bmpIconScaled;
dc.Attach(::GetDC(NULL));
dcTmp1.CreateCompatibleDC(&dc);
dcTmp2.CreateCompatibleDC(&dc);
bmpIcon.CreateCompatibleBitmap(&dc, bm.bmWidth, bm.bmHeight);
bmpIconScaled.CreateCompatibleBitmap(&dc, cx, cy);
::ReleaseDC(NULL, dc.Detach());
CBitmap* pbmpOld1 = dcTmp1.SelectObject(&bmpIcon);
CBitmap* pbmpOld2 = dcTmp2.SelectObject(&bmpIconScaled);
dcTmp1.FillSolidRect(0,0,bm.bmWidth,bm.bmHeight, m_crBackground);
::DrawIconEx(dcTmp1, 0,0,hIcon,bm.bmWidth,bm.bmHeight,0,NULL,DI_NORMAL);
dcTmp2.SetStretchBltMode(HALFTONE);
dcTmp2.StretchBlt(0,0,cx,cy,&dcTmp1, 0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
dcTmp1.SelectObject(pbmpOld1);
dcTmp2.SelectObject(pbmpOld2);
SetIcon(bmpIconScaled, m_crBackground);
}
::DeleteObject(iconinfo.hbmColor);
::DeleteObject(iconinfo.hbmMask);
}
}
// Sets the icon displayed in the top left of the balloon (pass NULL hBitmap to hide icon)
void CBalloonHelp::SetIcon(HBITMAP hBitmap, COLORREF crMask)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
if ( NULL != hBitmap )
{
BITMAP bm;
if (::GetObject(hBitmap, sizeof(bm),(LPVOID)&bm))
{
m_ilIcon.Create(bm.bmWidth, bm.bmHeight, ILC_COLOR24|ILC_MASK,1,0);
m_ilIcon.Add(CBitmap::FromHandle(hBitmap), crMask);
}
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon
void CBalloonHelp::SetIcon(HBITMAP hBitmap, HBITMAP hMask)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
ASSERT(NULL != hBitmap);
ASSERT(NULL != hMask);
BITMAP bm;
if (::GetObject(hBitmap, sizeof(bm),(LPVOID)&bm))
{
m_ilIcon.Create(bm.bmWidth, bm.bmHeight, ILC_COLOR24|ILC_MASK,1,0);
m_ilIcon.Add(CBitmap::FromHandle(hBitmap), CBitmap::FromHandle(hMask));
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Set icon displayed in the top left of the balloon to image # nIconIndex from pImageList
void CBalloonHelp::SetIcon(CImageList* pImageList, int nIconIndex)
{
// sanity checks
ASSERT_VALID(pImageList);
ASSERT(nIconIndex >= 0 && nIconIndex < pImageList->GetImageCount() );
HICON hIcon = NULL;
if ( NULL != pImageList && nIconIndex >= 0 && nIconIndex < pImageList->GetImageCount() )
hIcon = pImageList->ExtractIcon(nIconIndex);
SetIcon(hIcon);
if ( NULL != hIcon )
::DestroyIcon(hIcon);
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -