Introduction
Ever wanted to open a URL without blatting the contents of an existing browser window? Here's how - and it will only take you seconds.
First, include the header file:
#include "url.h"
Then simply declare a CURL
object and call the Open
method:
CURL url;
url.Open(_T("http:"));
If you want to re-use an existing browser window, then pass false
as the second parameter:
CURL url;
url.Open(_T("http:"), false);
That's it! Easy - and you can use this code with any CString
friendly framework (MFC, WTL, ATL7).
CURL
#pragma once
class CURL
{
private:
CString m_strBrowser;
public:
void Open(LPCTSTR lpszURL, bool bNewWindow = true)
{
if (bNewWindow)
::ShellExecute(NULL, NULL, GetBrowser(), lpszURL, NULL, SW_SHOWNORMAL);
else
::ShellExecute(NULL, NULL, lpszURL, NULL, NULL, SW_SHOWNORMAL);
}
LPCTSTR GetBrowser(void)
{
if (m_strBrowser.IsEmpty())
{
HKEY hKey = NULL;
if (::RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("http\\shell\\open\\command"),
0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD cbData = 0;
if (::RegQueryValueEx(hKey, NULL, NULL, NULL, NULL, &cbData)
== ERROR_SUCCESS && cbData > 0)
{
TCHAR* psz = new TCHAR [cbData];
if (psz != NULL)
{
if (::RegQueryValueEx(hKey, NULL, NULL,
NULL, (LPBYTE)psz, &cbData) ==
ERROR_SUCCESS)
{
m_strBrowser = psz;
}
delete [] psz;
}
}
::RegCloseKey(hKey);
}
if (m_strBrowser.GetLength() > 0)
{
int nStart = m_strBrowser.Find('"');
int nEnd = m_strBrowser.ReverseFind('"');
if (nStart >= 0 && nEnd >= 0)
{
if (nStart != nEnd)
{
m_strBrowser = m_strBrowser.Mid(nStart + 1, nEnd - nStart - 1);
}
}
else
{
int nIndex = m_strBrowser.ReverseFind('\\');
if (nIndex > 0)
{
int nSpace = m_strBrowser.Find(' ', nIndex);
if (nSpace > 0)
m_strBrowser = m_strBrowser.Left(nSpace);
}
}
}
}
return m_strBrowser;
}
};