161 lines
4.3 KiB
C++
Raw Normal View History

2024-01-22 12:56:15 +08:00
// SimpleBrowser.cpp --- simple Win32 browser
// Copyright (C) 2019 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
// This file is public domain software.
#define _CRT_SECURE_NO_WARNINGS
// SimpleBrowser.cpp --- simple Win32 browser
// Copyright (C) 2019 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
// This file is public domain software.
#define _CRT_SECURE_NO_WARNINGS
2024-04-02 15:36:52 +08:00
#include "MWebBrowser.hpp"
2024-01-22 12:56:15 +08:00
2024-01-22 14:33:11 +08:00
BOOL GetIEVersion(LPWSTR pszVersion, DWORD cchVersionMax)
{
pszVersion[0] = 0;
HKEY hKey = NULL;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer", 0,
2024-04-02 15:36:52 +08:00
KEY_READ, &hKey);
2024-01-22 14:33:11 +08:00
if (hKey)
{
DWORD cb = cchVersionMax * sizeof(WCHAR);
LONG ret = RegQueryValueExW(hKey, L"svcVersion", NULL, NULL, (LPBYTE)pszVersion, &cb);
if (ret != ERROR_SUCCESS)
{
ret = RegQueryValueExW(hKey, L"Version", NULL, NULL, (LPBYTE)pszVersion, &cb);
}
RegCloseKey(hKey);
return ret == ERROR_SUCCESS;
}
return FALSE;
}
2024-04-02 15:36:52 +08:00
static DWORD getemulation()
{
2024-01-22 14:33:11 +08:00
DWORD m_emulation = 0;
WCHAR szVersion[32];
if (GetIEVersion(szVersion, ARRAYSIZE(szVersion)))
{
if (szVersion[1] == L'.')
{
switch (szVersion[0])
{
case '7':
m_emulation = 7000;
break;
case '8':
m_emulation = 8888;
break;
case '9':
m_emulation = 9999;
break;
}
}
else if (szVersion[2] == L'.')
{
if (szVersion[0] == L'1' && szVersion[1] == L'0')
{
m_emulation = 10001;
}
if (szVersion[0] == L'1' && szVersion[1] == L'1')
{
m_emulation = 11001;
}
}
}
return m_emulation;
}
2024-01-22 12:56:15 +08:00
BOOL DoSetBrowserEmulation(DWORD dwValue)
{
static const TCHAR s_szFeatureControl[] =
TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl");
TCHAR szPath[MAX_PATH], *pchFileName;
GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath));
pchFileName = PathFindFileName(szPath);
BOOL bOK = FALSE;
HKEY hkeyControl = NULL;
RegOpenKeyEx(HKEY_CURRENT_USER, s_szFeatureControl, 0, KEY_ALL_ACCESS, &hkeyControl);
if (hkeyControl)
{
HKEY hkeyEmulation = NULL;
RegCreateKeyEx(hkeyControl, TEXT("FEATURE_BROWSER_EMULATION"), 0, NULL, 0,
KEY_ALL_ACCESS, NULL, &hkeyEmulation, NULL);
if (hkeyEmulation)
{
if (dwValue)
{
DWORD value = dwValue, size = sizeof(value);
LONG result = RegSetValueEx(hkeyEmulation, pchFileName, 0,
REG_DWORD, (LPBYTE)&value, size);
bOK = (result == ERROR_SUCCESS);
}
else
{
RegDeleteValue(hkeyEmulation, pchFileName);
bOK = TRUE;
}
RegCloseKey(hkeyEmulation);
}
RegCloseKey(hkeyControl);
}
return bOK;
}
2024-04-02 15:36:52 +08:00
extern "C" __declspec(dllexport) void *html_new(HWND parent)
{
DoSetBrowserEmulation(getemulation());
auto s_pWebBrowser = MWebBrowser::Create(parent);
2024-01-22 12:56:15 +08:00
if (!s_pWebBrowser)
return NULL;
2024-04-02 15:36:52 +08:00
s_pWebBrowser->put_Silent(VARIANT_TRUE);
s_pWebBrowser->AllowInsecure(TRUE);
return s_pWebBrowser;
2024-01-22 12:56:15 +08:00
}
2024-04-02 15:36:52 +08:00
extern "C" __declspec(dllexport) void html_navigate(void *web, wchar_t *path)
{
if (!web)
return;
auto ww = static_cast<MWebBrowser *>(web);
ww->Navigate2(path);
2024-01-22 12:56:15 +08:00
}
2024-04-02 15:36:52 +08:00
extern "C" __declspec(dllexport) void html_resize(void *web, int x, int y, int w, int h)
{
if (!web)
return;
auto ww = static_cast<MWebBrowser *>(web);
RECT r;
r.left = x;
r.top = y;
r.right = x + w;
r.bottom = y + h;
ww->MoveWindow(r);
2024-01-22 12:56:15 +08:00
}
2024-04-02 15:36:52 +08:00
extern "C" __declspec(dllexport) void html_release(void *web)
{
if (!web)
return;
auto ww = static_cast<MWebBrowser *>(web);
2024-01-22 12:56:15 +08:00
ww->Destroy();
2024-04-02 15:36:52 +08:00
// ww->Release(); Destroy减少引用计数自动del
}
extern "C" __declspec(dllexport) void html_get_current_url(void *web, wchar_t *url)
{
if (!web)
return;
auto ww = static_cast<MWebBrowser *>(web);
ww->Destroy();
wchar_t *_u;
ww->get_LocationURL(&_u);
wcscpy(url, _u);
}