📄 hhsupp.cpp
字号:
////////////////////////////////////////////////////////////////////
// HHSupp.cpp : HTML Help Support Module for MFC Applications
//
// by Oz Solomonovich (osolo@bigfoot.com)
#include "stdafx.h"
#include <afxpriv.h> // for WM_COMMANDHELP
#include <afxtempl.h>
//#include "htmlhelp.h" // from HTML Help SDK
#include "HHSupp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
LPCTSTR g_pszPopupWinTypeName = _T("PopupTopicWnd");
bool g_bHTMLHelp_ShowNoHelpMessage = true;
typedef HWND (WINAPI fnHtmlHelp)(HWND hwndCaller, LPCTSTR pszFile,
UINT uCommand, DWORD_PTR dwData);
static HMODULE s_modHH;
static fnHtmlHelp *s_pfnHtmlHelp;
static CMenu s_WhatsThis;
static CMap<int, int, CString, LPCTSTR> s_WinTypeMap;
#define WHATS_THIS _T("&What's This?")
#define FILE_NOT_FOUND _T("Cannot find the help file %s.\n") \
_T("Would you like to locate it yourself?")
#define NO_HELP_FNT _T("MS Sans Serif,8,,")
#define NO_HELP_TXT \
_T("No help information is available for this item.")
// forward declarations
static void ShowHelp(HWND hWnd, int ctrlID, DWORD helpID,
LPCTSTR pszHelpWndType = NULL, LPCTSTR pszHelpFile = APP_CHM);
static DWORD FindHelpID(const DWORD *ids, DWORD ctrlID,
bool bAllowImplicit);
static BOOL DoHelpInfo(CWnd *pDlg, const DWORD *pHelpIDs,
HELPINFO* pHelpInfo, bool bAllowImplicit);
static void DoContextMenu(CWnd *pDlg, const DWORD *pHelpIDs,
CWnd *pWnd, CPoint point, bool bAllowImplicit);
static CString& GetHelpFile();
void InitHHSupp()
{
s_modHH = /*Afx*/LoadLibrary(_T("HHCTRL.OCX"));
if (s_modHH)
s_pfnHtmlHelp = (fnHtmlHelp *)::GetProcAddress(s_modHH,
#ifdef UNICODE
(LPCSTR)ATOM_HTMLHELP_API_UNICODE);
#else
ATOM_HTMLHELP_API_ANSI);
#endif
else
s_pfnHtmlHelp = NULL;
if (s_pfnHtmlHelp)
{
s_WhatsThis.CreatePopupMenu();
s_WhatsThis.InsertMenu(0, MF_BYPOSITION, 1, WHATS_THIS);
}
}
void EndHHSupp()
{
if (s_modHH)
{
/*Afx*/FreeLibrary(s_modHH);
s_modHH = NULL;
}
}
// this auto initializing/destructing object will ensure that the
// that the initialization/cleanup code is called on program
// start/end
static struct auto_init
{
auto_init() { InitHHSupp(); }
~auto_init() { EndHHSupp(); }
} autoinit;
////////////////////////////////////////////////////////////////////
// Internal operations
static DWORD FindHelpID(const DWORD *ids, DWORD ctrlID,
bool bAllowImplicit)
{
while (*ids)
{
if (*ids == ctrlID)
return (*++ids);
ids += 2;
}
if (bAllowImplicit &&
(unsigned short)ctrlID != (unsigned short)IDC_STATIC)
return ctrlID;
return 0;
}
static BOOL DoHelpInfo(CWnd *pDlg, const DWORD *pHelpIDs,
HELPINFO* pHelpInfo, bool bAllowImplicit)
{
HWND hWnd = (HWND)pHelpInfo->hItemHandle;
TCHAR szClassName[256];
int ctrlID;
DWORD id;
// check to see if the user pressed F1 from inside a popup help
// window
::GetClassName(hWnd, (LPTSTR)szClassName, sizeof(szClassName));
if (_tcscmp(szClassName, _T("hh_popup")) == 0)
{
// we're inside a popup window, so exit
return FALSE;
}
if (pHelpInfo->iCtrlId >= 0)
ctrlID = pHelpInfo->iCtrlId;
else
ctrlID = (unsigned short)::GetDlgCtrlID(hWnd);
id = FindHelpID(pHelpIDs, (DWORD)ctrlID, bAllowImplicit);
if (id == 0)
{
// there's no help message for this item.
// display an error popup message?
if (!g_bHTMLHelp_ShowNoHelpMessage)
{
return FALSE; // no - exit
}
// Either:
// 1. The user pressed F1 - or -
// 2. The user pressed the dialog's '?' and used the mouse
// to click on a control.
// For the latter, we want to avoid displaying error
// messages for controls that aren't tab stops, otherwise
// an error message will be displayed needlessly for every
// static control and the likes
if ((::GetWindowLong(hWnd, GWL_STYLE) & WS_TABSTOP) !=
WS_TABSTOP)
{
return FALSE;
}
// Display the error message popup
HH_POPUP hhp;
CRect r;
hhp.cbStruct = sizeof(hhp);
hhp.idString = 0;
hhp.pszText = NO_HELP_TXT;
hhp.pszFont = NO_HELP_FNT;
hhp.clrBackground = -1;
hhp.clrForeground = -1;
::SetRect(&hhp.rcMargins, -1, -1, -1, -1);
::GetWindowRect(hWnd, r);
hhp.pt = r.CenterPoint();
HtmlHelp(pDlg->m_hWnd, NULL, HH_DISPLAY_TEXT_POPUP,
(DWORD)&hhp);
return TRUE;
}
ShowHelp(hWnd, ctrlID, id);
return TRUE;
}
static void DoContextMenu(CWnd *pDlg, const DWORD *pHelpIDs,
CWnd *pWnd, CPoint point, bool bAllowImplicit)
{
DWORD id;
// avoid showing the "What's This?" menu if we don't actually
// have the HTML help library loaded
if (!s_pfnHtmlHelp)
return;
/* static controls won't send this message, so if the user
right-clicks on a static control, the dialog's pointer - and
not the control's - will be passed in pWnd.
The code below tries to identify the actual control that
should be handled:
*/
if (pWnd->m_hWnd == pDlg->m_hWnd)
{
CPoint pt(GetMessagePos());
CRect r;
CWnd * pWndFound = NULL;
// it would have been nice to use the ChildWindowFromPoint
// function, but in case of one control over the other,
// ChildWindowFromPoint would return the handle to the
// control on the bottom, whereas we want the control on
// the top.
pWnd = pDlg->GetTopWindow();
while (pWnd)
{
if (pWnd->IsWindowVisible())
{
pWnd->GetWindowRect(r);
if (r.PtInRect(pt))
{
pWndFound = pWnd;
}
}
pWnd = pWnd->GetNextWindow();
}
if (pWndFound->GetSafeHwnd() == NULL) // nothing there?
return;
pWnd = pWndFound;
}
id = FindHelpID(pHelpIDs, (DWORD)pWnd->GetDlgCtrlID(),
bAllowImplicit);
if (id == 0)
return;
if (s_WhatsThis.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON |
TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
point.x, point.y, pDlg) == 1)
{
ShowHelp(pWnd->m_hWnd, pWnd->GetDlgCtrlID(), id);
}
}
static void ShowHelp(HWND hWnd, int ctrlID, DWORD helpID,
LPCTSTR pszHelpWndType /*= NULL*/,
LPCTSTR pszHelpFile /*=APP_CHM*/)
{
if (ctrlID)
{
if (helpID < 0x20000000) /* display a popup? */
{
DWORD ids[] = { (DWORD)ctrlID, helpID, 0 };
HtmlHelp(hWnd, APP_CHM, HH_TP_HELP_WM_HELP, (DWORD)ids);
return;
}
}
CString szHelpFile;
if (pszHelpFile == APP_CHM)
szHelpFile = GetHelpFile();
else
szHelpFile = pszHelpFile;
if (szHelpFile.IsEmpty())
return;
// determine which window type to use
if (!pszHelpWndType && helpID >= 0x20000000)
{
if (helpID < 0x30000000)
{
// 0x20000000...0x2FFFFFFF
pszHelpWndType = g_pszPopupWinTypeName;
}
else
{
if (helpID < 0x40000000)
{
// 0x30000000...0x3FFFFFFF (0x30nnXXXX)
int nn = (helpID >> 16) & 0xFF;
if (nn)
{
CString val;
// if this bombs, then you are trying to use an
// unregistered window type
VERIFY(s_WinTypeMap.Lookup(nn, val) != 0);
pszHelpWndType = s_WinTypeMap[nn];
}
}
}
}
if (pszHelpWndType)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -