Currently viewing: GipsySoft » Front Page» QHTM

How to catch hyperlinks

QHTM is often used to provide hyperlinks, some of which may go to the web and some may perform an internal function.

By default QHTM will handle invoking the users web browser for all links. However, if we want QHTM to behave something like a GUI or menu then we want to catch the links and invoke some internal command instead of launching a web brower.

Whenever a link is selected by the user QHTM sends a WM_NOTIFY message to it's parent. A typical handler for this might looks something like this (taken from the "demo" example):

case WM_NOTIFY:
	switch( wParam )
	{
	case IDC_LEFT_PANEL:
		{
			LPNMQHTM pnm = reinterpret_cast( lParam );
			HandleLeftPanelLink( hwnd, pnm );
		}
		break;

...

static bool IsExternal( LPCTSTR pcszURL )
{
	return !_tcsnicmp( pcszURL, _T("http:"), 5 )
		|| !_tcsnicmp( pcszURL, _T("mailto:"), 7 );
}


static void HandleLeftPanelLink( HWND hwnd, LPNMQHTM pnm )
{
	if( pnm->pcszLinkText )
	{
		if( IsExternal( pnm->pcszLinkText ) )
		{
			pnm->resReturnValue = TRUE;
		}
		else if( !_tcsnicmp( pnm->pcszLinkText, _T("command:"), 8 ) )
		{
			UINT uCommand = _ttol( pnm->pcszLinkText + 8 );
			PostMessage( hwnd, WM_COMMAND, MAKEWPARAM( uCommand, 0 ), 0 );
			pnm->resReturnValue = FALSE;
		}
		else
		{
			_tcscpy( g_szRightLink, pnm->pcszLinkText );

			LoadRightPanelHTML();

			pnm->resReturnValue = FALSE;
		}
	}
}

Note the two highlighted sections. For an external link we want QHTM to handle invoking the browser so we set the return value to TRUE and exit.

For URLs with "command:" at the begining we treat these commands like menu commands and simply post them on. Lastly telling QHTM that we handled it.

And finally, for anything else we take the URL and copy it to a buffer so later we can use them to load more HTML. Lastly telling QHTM that we handled it.