前面使用GDI绘制解决了集成到Qt的系统冲突和QWebEngineView的冲突,但仅支持作为qt的子窗体,现在将其修改为支持独立窗口的创建。
首先判断create接口传入的父窗口句柄是否是空,为空表示创建一个独立窗口,代码如下:
_hWnd = CreateWindow(wkeWebViewClassName, 0,
WS_CAPTION | WS_VISIBLE | WS_BORDER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_TILEDWINDOW,
x, y, nWeight, nHeight,
hParent,
0,
0, 0);
增加了标题和标题栏相关按钮。
后面的与之前的代码类似
wkeSetHandle(_hWebView, _hWnd);
resize(x, y, nWeight, nHeight);
SetWindowLong(_hWnd, GWL_USERDATA, (LONG)this);
++QtMiniblinkWebView::_viewCount;
return _pRender->init(_hWnd, nWeight, nHeight);
去掉对父窗口过程的截获。
使用也很简单:
1.创建独立窗口
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#if 1
MainWindow w;
w.show();
#else
int scrWidth,scrHeight;
RECT rect;
//获得屏幕尺寸
scrWidth = GetSystemMetrics(SM_CXSCREEN);
scrHeight = GetSystemMetrics(SM_CYSCREEN);
//重新设置rect里的值
rect.left = (scrWidth-800)/2;
rect.top = (scrHeight-600)/2;
QtMiniblinkWebView *view = new QtMiniblinkWebView();
view->create(rect.left ,rect.top, 800, 600, NULL);
view->load("https://www.baidu.com");
view->setVisible(true);
#endif
int result = a.exec();
return result;
}
2.作为子窗口
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 1
QWebEngineView *webview = new QWebEngineView(this);
webview->load(QUrl("http://baidu.com"));
setCentralWidget(webview);
view = new QtMiniblinkWebView();
view->create(0, 0, 300, 400, (HWND)webview->winId());
view->load("https://www.baidu.com");
#else
view = new QtMiniblinkWebView();
view->create(0, 0, width(), height(), (HWND)this->winId());
view->setRectFollowParent();
view->load("https://www.baidu.com");
QWebEngineView *webview = new QWebEngineView();
webview->setWindowFlags(Qt::FramelessWindowHint);
SetParent((HWND)webview->winId(), view->getHandle());
webview->resize(300, 300);
webview->load(QUrl("http://baidu.com"));
webview->move(300, 100);
webview->setVisible(true);
#endif
}
完整代码如下:
#ifndef QTMINIblinkWEBVIEW_DEFINE_H
#define QTMINIblinkWEBVIEW_DEFINE_H
#include <QObject>
#include "wke.h"
#define wkeWebViewClassName L"WKE_QtMiniblinkWebView_CLASSNAME"
/**
* CRenderGDI class
* draw image from wke callback
*/
class CRenderGDI {
public:
CRenderGDI()
:m_hView(NULL)
,m_hDC(NULL)
,m_hBitmap(NULL)
,m_pixels(NULL)
,m_width(0)
,m_height(0)
{}
virtual ~CRenderGDI()
{
if (m_hDC)
::DeleteDC(m_hDC);
if (m_hBitmap)
::DeleteObject(m_hBitmap);
}
/**
* init
* @param[in] hView a HWND to draw view
* @return true if suc
*/
virtual bool init(HWND hView, int width, int height)
{
m_hView = hView;
m_hDC = ::CreateCompatibleDC(0);
resize(width, height);
return true;
}
/**
* destroy this
* @return void
*/
virtual void destroy()
{
delete this;
}
/**
* resize
* @param[in] width of window
* @param[in] height of window
* @return true if suc
*/
virtual void resize(unsigned int w, unsigned int h)
{
if (m_width == w && m_height == h)
return;
m_width = w;
m_height = h;
m_pixels = NULL;
}
/**
* renderOnBlinkPaint
* @param[in] webView view of wke
* @param[in] hBlinkDC hdc of wke
* @param[in] x x pos of wke wnd
* @param[in] y y pos of wke wnd
* @param[in] cx width of wke wnd
* @param[in] cy height of wke wnd
* @return true if suc
*/
void renderOnBlinkPaint(wkeWebView webView, HDC hBlinkDC, int x, int y, int cx, int cy)
{
if (m_pixels == NULL) {
if (m_width != cx || m_height != cy)
return;
createBitmap();
}
HDC hScreenDC = ::GetDC(m_hView);
::BitBlt(m_hDC, x, y, m_width, m_height, hBlinkDC, x, y, SRCCOPY);
::BitBlt(hScreenDC, x, y, m_width, m_height, m_hDC, x, y, SRCCOPY);
::ReleaseDC(m_hView, hScreenDC);
}
/**
* renderOnBlinkPaint
* @param[in] webView view of wke
* @param[in] hBlinkDC hdc of wke
* @return true if suc
*/
void renderOnWindowPaint(wkeWebView webView, HDC hScreenDC)
{
if (m_pixels == NULL) {
return;
}
::BitBlt(hScreenDC, 0, 0, m_width, m_height, m_hDC, 0, 0, SRCCOPY);
}
/**
* createBitmap create NULL BITMAP
* @return void
*/
void createBitmap()
{
BITMAPINFO bi;
memset(&bi, 0, sizeof(bi));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = int(m_width);
bi.bmiHeader.biHeight = -int(m_height);
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
HBITMAP hbmp = ::CreateDIBSection(0, &bi, DIB_RGB_COLORS, &m_pixels, NULL, 0);
::SelectObject(m_hDC, hbmp);
if (m_hBitmap)
::DeleteObject(m_hBitmap);
m_hBitmap = hbmp;
}
private:
HWND m_hView;
HBITMAP m_hBitmap;
HDC m_hDC;
unsigned int m_width;
unsigned int m_height;
void* m_pixels;
};
/**
* JS CALL C++ callback function
* @param[in] jsExecState param
* @param[in] void* use data
*/
typedef jsValue (WKE_CALL_TYPE*BindFuction)(jsExecState , void* );
/**
* QtMiniblinkWebView class
* deal wke view and event
*/
class QtMiniblinkWebView : public QObject
{
Q_OBJECT
public:
QtMiniblinkWebView(void){_pRender = new CRenderGDI; _cursorInfoType = 0;}
~QtMiniblinkWebView(void);
/**
* registerControlerClass register window class
* @return true if suc
*/
BOOL registerControlerClass();
/**
* create create wke view
* @param[in] x x pos
* @param[in] y y pos
* @param[in] nWeight of view
* @param[in] nHeight of view
* @param[in] hParent of view
* @return true if suc
*/
BOOL create(int x, int y, int nWeight, int nHeight, HWND hParent);
/**
* resize resize wke view
* @param[in] x x pos
* @param[in] y y pos
* @param[in] nWeight of view
* @param[in] nHeight of view
* @return void
*/
void resize(int x, int y, int nWeight, int nHeight);
__inline operator wkeWebView(){return _hWebView;}
HWND getHandle(){return _hWnd;}
void setRectFollowParent();
/**
* setVisible set view visible
* @param[in] b visible
* @return void
*/
void setVisible(bool b);
/**
* load load url
* @param[in] url path of url encode utf-8
* @return void
*/
void load(const QString &url);
/**
* loadFile load local file
* @param[in] filePath path of local file encode utf-8
* @return void
*/
void loadFile(const QString &filePath);
/**
* loadHtml load html content
* @param[in] html content of html encode utf-8
* @return void
*/
void loadHtml(const QString &html);
/**
* reload reload view
* @return void
*/
void reload();
/**
* reload back view
* @return void
*/
void back();
/**
* forward back view
* @return void
*/
void forward();
/**
* clearCookie clear cookie of view
* @return void
*/
void clearCookie();
/**
* setCookieEnabled enable cookie of view
* @param[in] enbale ture or false
* @return void
*/
void setCookieEnabled(bool enbale);
/**
* isCookieEnabled is cookie enabled
* @return bool true if enbale
*/
bool isCookieEnabled();
/**
* setCookieFullPath set cookie storage path
* @param[in] path path of cookie storage
* @return void
*/
void setCookieFullPath(const QString &path);
/**
* setLocalStorageFullPath set cookie local storage path
* @param[in] path local path of cookie storage
* @return void
*/
void setLocalStorageFullPath(const QString &path);
/**
* setCookie set cookie for url
* @param[in] url need set cookie url
* @param[in] cookie cookie of url
* @return void
*/
void setCookie(const QString &url, const QString &cookie);
/**
* getCookie get cookie by view
* @return QString string of view's cookie
*/
QString getCookie();
/**
* setZoomFactor set ZoomFactor for wke HWND
* @param[in] value scale value
* @return void
*/
void setZoomFactor(qreal value);
/**
* runJavaScript C++ Call js function
* @param[in] scriptSourcer like window.open('start');
* @return void
*/
void runJavaScript(const QString& scriptSourcer);
/**
* bindJsFunction bind function to js window
* @param[in] functionName function name
* @param[in] js callback function
* @param[in] function param
* @return void
*/
void bindJsFunction(const QString &functionName, BindFuction fun, int paracount);
signals:
/**
* loadStarted url start load sig
* @return void
*/
void loadStarted();
/**
* loadFinished url load loadFinished sig
* @param[out] b true if suc
* @return void
*/
void loadFinished(bool b);
/**
* loadUrlFail url load fail sig
* @return void
*/
void loadUrlFail();
/**
* loadProgress url load progress sig
* @return void
*/
void loadProgress(int progress);
protected:
//消息函数
/**
* deal window msg WM_COMMAND WM_SIZE WM_PAINT WM_KEYDOWN.......
* @param[in] WPARAM
* @param[in] LPARAM
* @return LRESULT ret
*/
LRESULT onCommand(WPARAM wParam, LPARAM lParam);
LRESULT onSize(WPARAM wParam, LPARAM lParam);
LRESULT onPaint(WPARAM wParam, LPARAM lParam);
LRESULT onKeyDown(WPARAM wParam, LPARAM lParam);
LRESULT onKeyUp(WPARAM wParam, LPARAM lParam);
LRESULT onChar(WPARAM wParam, LPARAM lParam);
LRESULT onMouseEvent(UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT onSetCursor(WPARAM wParam, LPARAM lParam);
LRESULT onMouseWheel(WPARAM wParam, LPARAM lParam);
LRESULT onSetFocus(WPARAM wParam, LPARAM lParam);
LRESULT onKillFocus(WPARAM wParam, LPARAM lParam);
LRESULT onIMEStartComposition(WPARAM wParam, LPARAM lParam);
LRESULT webViewClassWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK subClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
static void onPaintUpdatedCallback(wkeWebView webView, void* param, const HDC hdc, int x, int y, int cx, int cy);
static LRESULT CALLBACK webViewWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static bool onLoadUrlBegin(wkeWebView webView, void* param, const char* url, void *job);
static void onLoadUrlEnd(wkeWebView webView, void* param, const char *url, void *job, void* buf, int len);
static void onLoadUrlFailed(wkeWebView webView, void* param, const utf8* url, wkeNetJob job);
static void onLoadingFinish(wkeWebView webView, void* param, const wkeString url, wkeLoadingResult result, const wkeString failedReason);
private:
/**
* initialize init miniblink
* @return LRESULT ret
*/
void initialize();
/**
* finalize release miniblink
* @return
*/
void finalize();
protected:
HWND _hParent; //父窗口句柄
wkeWebView _hWebView{nullptr}; //wkeWebView 句柄
HWND _hWnd{0}; //容纳wkeWebView 窗口句柄
CRenderGDI *_pRender;
int _cursorInfoType;
static bool _isInit;
static int _viewCount;
};
#endif
#include "QMiniblinkWebView.h"
#include <windowsx.h>
#include <windows.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <stdbool.h>
#include <stdio.h>
#include <Commctrl.h>
#include <Imm.h>
#include <vector>
#include <QDebug>
#include <QFile>
#include <QApplication>
#pragma comment(lib, "Imm32.lib")
bool QtMiniblinkWebView::_isInit = false;
int QtMiniblinkWebView::_viewCount = 0;
BOOL QtMiniblinkWebView::registerControlerClass()
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_DBLCLKS;// CS_HREDRAW | CS_VREDRAW; //
wcex.lpfnWndProc = webViewWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
// wcex.hInstance = AfxGetInstanceHandle();
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = wkeWebViewClassName;
wcex.hIconSm = 0;
return !!RegisterClassEx(&wcex);
}
void QtMiniblinkWebView::initialize()
{
QString nodePath = QApplication::applicationDirPath() + "/node.dll";
#ifdef _WIN64
nodePath = QApplication::applicationDirPath() + "/miniblink_x64.dll";
#endif
if(!QFile::exists(nodePath))
{
qDebug()<<QStringLiteral("请将node.dll拷贝到运行目录");
return;
}
std::vector<wchar_t> tempPath;
tempPath.resize(MAX_PATH);
nodePath.toWCharArray(&tempPath[0]);
wkeSetWkeDllPath(&tempPath[0]);
wkeInitialize();
_isInit = true;
}
void QtMiniblinkWebView::finalize()
{
if(--QtMiniblinkWebView::_viewCount == 0)
{
wkeFinalize();
_isInit = false;
}
}
QtMiniblinkWebView::~QtMiniblinkWebView(void){
if(NULL != _pRender)
{
delete _pRender;
_pRender = nullptr;
}
if(_hWebView)
{
wkeOnPaintUpdated(_hWebView, nullptr, 0);
wkeOnLoadUrlBegin(_hWebView, nullptr, 0);
wkeOnLoadUrlEnd(_hWebView, nullptr, 0);
wkeOnLoadUrlFail(_hWebView, nullptr, 0);
wkeOnLoadingFinish(_hWebView, nullptr, 0);
wkeDestroyWebView(_hWebView);
_hWebView = nullptr;
}
if(_hWnd)
{
::DestroyWindow(_hWnd);
_hWnd = nullptr;
}
if(_hParent)
{
SetWindowSubclass(_hParent, nullptr, 0, 0);
}
UnregisterClass(wkeWebViewClassName, 0);
finalize();
}
BOOL QtMiniblinkWebView::create(int x, int y, int nWeight, int nHeight, HWND hParent)
{
if(!_isInit)
initialize();
if(_hWebView)
return true;
_hWebView = wkeCreateWebView();
wkeSetTransparent(_hWebView, false);
wkeOnPaintUpdated(_hWebView, onPaintUpdatedCallback, this);
wkeOnLoadUrlBegin(_hWebView, onLoadUrlBegin, (void *)this);
wkeOnLoadUrlEnd(_hWebView, onLoadUrlEnd, (void *)this);
wkeOnLoadUrlFail(_hWebView, onLoadUrlFailed, (void *)this);
wkeOnLoadingFinish(_hWebView, onLoadingFinish, (void *)this);
if (!_hWebView)
{
return FALSE;
}
if (!registerControlerClass())
{
return FALSE;
}
if(hParent)
{
_hWnd = CreateWindow(wkeWebViewClassName, 0,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
x, y, nWeight, nHeight,
hParent,
0,
0, 0);
}
else
{
_hWnd = CreateWindow(wkeWebViewClassName, 0,
WS_CAPTION | WS_VISIBLE | WS_BORDER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_TILEDWINDOW,
x, y, nWeight, nHeight,
hParent,
0,
0, 0);
}
if (INVALID_HANDLE_VALUE == _hWnd)
{
return FALSE;
}
_hParent = hParent;
wkeSetHandle(_hWebView, _hWnd);
resize(x, y, nWeight, nHeight);
SetWindowLong(_hWnd, GWL_USERDATA, (LONG)this);
//win7最小化问题
connect(qApp, &QApplication::applicationStateChanged, this, [=](Qt::ApplicationState state)
{
if (Qt::ApplicationActive == state)
{
RECT rc;
::GetClientRect(_hWnd, &rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
//repaint
resize(0, 0, width-1, height-1);
resize(0, 0, width, height);
}
}
);
++QtMiniblinkWebView::_viewCount;
return _pRender->init(_hWnd, nWeight, nHeight);
}
void QtMiniblinkWebView::setRectFollowParent()
{
if(_hParent)
{
::SetWindowSubclass(_hParent, subClassProc, 0, (DWORD_PTR)this);
}
}
LRESULT CALLBACK QtMiniblinkWebView::webViewWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
QtMiniblinkWebView* pView = (QtMiniblinkWebView*)GetWindowLong(hWnd, GWL_USERDATA);
if (pView)
{
return pView->webViewClassWndProc(uMsg, wParam, lParam);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK QtMiniblinkWebView::subClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
QtMiniblinkWebView *obj = (QtMiniblinkWebView *)dwRefData;
if(obj == nullptr)
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
switch (uMsg) {
case WM_SIZE:
{
RECT rc = { 0 };
::GetClientRect(hWnd, &rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
obj->resize(0, 0, width, height);
}
break;
default:
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
bool QtMiniblinkWebView::onLoadUrlBegin(wkeWebView webView, void* param, const char* url, void *job)
{
QtMiniblinkWebView *obj = (QtMiniblinkWebView *)param;
if(obj != nullptr)
{
emit obj->loadStarted();
}
return true;
}
void QtMiniblinkWebView::onLoadUrlEnd(wkeWebView webView, void* param, const char *url, void *job, void* buf, int len)
{
QtMiniblinkWebView *obj = (QtMiniblinkWebView *)param;
if(obj != nullptr)
{
emit obj->loadFinished(true);
}
}
void QtMiniblinkWebView::onLoadUrlFailed(wkeWebView webView, void* param, const utf8* url, wkeNetJob job)
{
QtMiniblinkWebView *obj = (QtMiniblinkWebView *)param;
if(obj != nullptr)
{
emit obj->loadUrlFail();
}
}
void QtMiniblinkWebView::onLoadingFinish(wkeWebView webView, void* param, const wkeString url, wkeLoadingResult result, const wkeString failedReason)
{
QtMiniblinkWebView *obj = (QtMiniblinkWebView *)param;
if(obj != nullptr && result == WKE_LOADING_SUCCEEDED)
{
emit obj->loadFinished(true);
}
}
void QtMiniblinkWebView::resize(int x, int y, int nWeight, int nHeight)
{
if (_hWebView)
{
MoveWindow(_hWnd, x, y, nWeight, nHeight, TRUE);
wkeResize(_hWebView, nWeight, nHeight);
}
}
void QtMiniblinkWebView::setVisible(bool b)
{
wkeShowWindow(_hWebView, b);
ShowWindow(_hWnd, b?SW_SHOW:SW_HIDE);
}
void QtMiniblinkWebView::load(const QString &url)
{
wkeLoadURL(*this, url.toStdString().c_str());
}
void QtMiniblinkWebView::loadFile(const QString &filePath)
{
wkeLoadFileW(*this, filePath.toStdWString().c_str());
}
void QtMiniblinkWebView::loadHtml(const QString &html)
{
wkeLoadHTMLW(*this, html.toStdWString().c_str());
}
void QtMiniblinkWebView::reload()
{
wkeReload(*this);
}
void QtMiniblinkWebView::back()
{
wkeGoBack(*this);
}
void QtMiniblinkWebView::forward()
{
wkeGoForward(*this);
}
void QtMiniblinkWebView::clearCookie()
{
wkeClearCookie(*this);
}
void QtMiniblinkWebView::setCookieEnabled(bool enbale)
{
wkeSetCookieEnabled(*this, enbale);
}
bool QtMiniblinkWebView::isCookieEnabled()
{
return wkeIsCookieEnabled(*this);
}
void QtMiniblinkWebView::setCookieFullPath(const QString &path)
{
wkeSetCookieJarFullPath(*this, path.toStdWString().c_str());
}
void QtMiniblinkWebView::setLocalStorageFullPath(const QString &path)
{
wkeSetLocalStorageFullPath(*this, path.toStdWString().c_str());
}
void QtMiniblinkWebView::setCookie(const QString &url, const QString &cookie)
{
wkeSetCookie(*this, url.toUtf8().data(), cookie.toUtf8().data());
}
QString QtMiniblinkWebView::getCookie()
{
return QString::fromUtf8(wkeGetCookie(*this));
}
void QtMiniblinkWebView::setZoomFactor(qreal value)
{
wkeSetZoomFactor(*this, value);
}
void QtMiniblinkWebView::runJavaScript(const QString& scriptSourcer)
{
wkeRunJSW(*this, scriptSourcer.toStdWString().c_str());
}
void QtMiniblinkWebView::bindJsFunction(const QString &functionName, BindFuction fun, int paracount)
{
wkeJsBindFunction(functionName.toStdString().c_str(), fun, nullptr, paracount);
}
LRESULT QtMiniblinkWebView::webViewClassWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SETCURSOR:
return onSetCursor(wParam, lParam);
case WM_COMMAND:
return onCommand(wParam, lParam);
case WM_SIZE:
return onSize(wParam, lParam);
case WM_PAINT:
return onPaint(wParam, lParam);
case WM_KEYDOWN:
return onKeyDown(wParam, lParam);
case WM_KEYUP:
return onKeyUp(wParam, lParam);
case WM_CHAR:
return onChar(wParam, lParam);
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_MOUSEMOVE:
return onMouseEvent(uMsg, wParam, lParam);
case WM_MOUSEWHEEL:
return onMouseWheel(wParam, lParam);
case WM_SETFOCUS:
return onSetFocus(wParam, lParam);
case WM_KILLFOCUS:
return onKillFocus(wParam, lParam);
case WM_IME_STARTCOMPOSITION:
return onIMEStartComposition(wParam, lParam);
case WM_GETDLGCODE:
return DLGC_WANTARROWS | DLGC_WANTALLKEYS | DLGC_WANTCHARS;
default:
return DefWindowProc(_hWnd, uMsg, wParam, lParam);
}
return 0;
}
LRESULT QtMiniblinkWebView::onCommand(WPARAM wParam, LPARAM lParam)
{
SendMessage(wkeGetWindowHandle(_hWebView), WM_COMMAND, wParam, lParam);
return 0;
}
LRESULT QtMiniblinkWebView::onSize(WPARAM wParam, LPARAM lParam)
{
if (NULL != _hWebView && NULL != _pRender)
{
wkeResize(_hWebView, LOWORD(lParam), HIWORD(lParam));
_pRender->resize(LOWORD(lParam), HIWORD(lParam));
}
return 0;
}
LRESULT QtMiniblinkWebView::onPaint(WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintInfo;
BeginPaint(_hWnd, &paintInfo);
_pRender->renderOnWindowPaint(_hWebView, paintInfo.hdc);
EndPaint(_hWnd, &paintInfo);
return 0;
}
LRESULT QtMiniblinkWebView::onKeyDown(WPARAM wParam, LPARAM lParam)
{
unsigned int virtualKeyCode = wParam;
unsigned int flags = 0;
if (HIWORD(lParam) & KF_REPEAT)
flags |= WKE_REPEAT;
if (HIWORD(lParam) & KF_EXTENDED)
flags |= WKE_EXTENDED;
wkeFireKeyDownEvent(_hWebView, virtualKeyCode, flags, false);
return 0;
}
LRESULT QtMiniblinkWebView::onKeyUp(WPARAM wParam, LPARAM lParam)
{
unsigned int virtualKeyCode = wParam;
unsigned int flags = 0;
if (HIWORD(lParam) & KF_REPEAT)
flags |= WKE_REPEAT;
if (HIWORD(lParam) & KF_EXTENDED)
flags |= WKE_EXTENDED;
wkeFireKeyUpEvent(_hWebView, virtualKeyCode, flags, false);
return 0;
}
LRESULT QtMiniblinkWebView::onChar(WPARAM wParam, LPARAM lParam)
{
unsigned int charCode = wParam;
unsigned int flags = 0;
if (HIWORD(lParam) & KF_REPEAT)
flags |= WKE_REPEAT;
if (HIWORD(lParam) & KF_EXTENDED)
flags |= WKE_EXTENDED;
wkeFireKeyPressEvent(_hWebView, charCode, flags, false);
return 0;
}
LRESULT QtMiniblinkWebView::onMouseEvent(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
_cursorInfoType = wkeGetCursorInfoType(_hWebView);
if (uMsg == WM_LBUTTONDOWN || uMsg == WM_MBUTTONDOWN || uMsg == WM_RBUTTONDOWN) {
SetFocus(_hWnd);
SetCapture(_hWnd);
} else if (uMsg == WM_LBUTTONUP || uMsg == WM_MBUTTONUP || uMsg == WM_RBUTTONUP) {
ReleaseCapture();
}
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
unsigned int flags = 0;
if (wParam & MK_CONTROL)
flags |= WKE_CONTROL;
if (wParam & MK_SHIFT)
flags |= WKE_SHIFT;
if (wParam & MK_LBUTTON)
flags |= WKE_LBUTTON;
if (wParam & MK_MBUTTON)
flags |= WKE_MBUTTON;
if (wParam & MK_RBUTTON)
flags |= WKE_RBUTTON;
//flags = wParam;
wkeFireMouseEvent(_hWebView, uMsg, x, y, flags);
return 0;
}
LRESULT QtMiniblinkWebView::onSetCursor(WPARAM wParam, LPARAM lParam)
{
RECT rc;
::GetClientRect(_hWnd, &rc);
POINT pt;
::GetCursorPos(&pt);
::ScreenToClient(_hWnd, &pt);
if (!::PtInRect(&rc, pt))
return false;
HCURSOR hCur = NULL;
switch (_cursorInfoType) {
case WkeCursorInfoPointer:
hCur = ::LoadCursor(NULL, IDC_ARROW);
break;
case WkeCursorInfoIBeam:
hCur = ::LoadCursor(NULL, IDC_IBEAM);
break;
case WkeCursorInfoHand:
hCur = ::LoadCursor(NULL, IDC_HAND);
break;
case WkeCursorInfoWait:
hCur = ::LoadCursor(NULL, IDC_WAIT);
break;
case WkeCursorInfoHelp:
hCur = ::LoadCursor(NULL, IDC_HELP);
break;
case WkeCursorInfoEastResize:
hCur = ::LoadCursor(NULL, IDC_SIZEWE);
break;
case WkeCursorInfoNorthResize:
hCur = ::LoadCursor(NULL, IDC_SIZENS);
break;
case WkeCursorInfoSouthWestResize:
case WkeCursorInfoNorthEastResize:
hCur = ::LoadCursor(NULL, IDC_SIZENESW);
break;
case WkeCursorInfoSouthResize:
case WkeCursorInfoNorthSouthResize:
hCur = ::LoadCursor(NULL, IDC_SIZENS);
break;
case WkeCursorInfoNorthWestResize:
case WkeCursorInfoSouthEastResize:
hCur = ::LoadCursor(NULL, IDC_SIZENWSE);
break;
case WkeCursorInfoWestResize:
case WkeCursorInfoEastWestResize:
hCur = ::LoadCursor(NULL, IDC_SIZEWE);
break;
case WkeCursorInfoNorthEastSouthWestResize:
case WkeCursorInfoNorthWestSouthEastResize:
hCur = ::LoadCursor(NULL, IDC_SIZEALL);
break;
default:
hCur = ::LoadCursor(NULL, IDC_ARROW);
break;
}
if (hCur) {
::SetCursor(hCur);
}
return 0;
}
LRESULT QtMiniblinkWebView::onMouseWheel(WPARAM wParam, LPARAM lParam)
{
POINT pt;
pt.x = GET_X_LPARAM(lParam);
pt.y = GET_Y_LPARAM(lParam);
ScreenToClient(_hWnd, &pt);
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
unsigned int flags = 0;
if (wParam & MK_CONTROL)
flags |= WKE_CONTROL;
if (wParam & MK_SHIFT)
flags |= WKE_SHIFT;
if (wParam & MK_LBUTTON)
flags |= WKE_LBUTTON;
if (wParam & MK_MBUTTON)
flags |= WKE_MBUTTON;
if (wParam & MK_RBUTTON)
flags |= WKE_RBUTTON;
//flags = wParam;
wkeFireMouseWheelEvent(_hWebView, pt.x, pt.y, delta, flags);
return 0;
}
LRESULT QtMiniblinkWebView::onSetFocus(WPARAM wParam, LPARAM lParam)
{
wkeSetFocus(_hWebView);
return 0;
}
LRESULT QtMiniblinkWebView::onKillFocus(WPARAM wParam, LPARAM lParam)
{
wkeKillFocus(_hWebView);
return 0;
}
LRESULT QtMiniblinkWebView::onIMEStartComposition(WPARAM wParam, LPARAM lParam)
{
wkeRect caret = wkeGetCaretRect(_hWebView);
CANDIDATEFORM form;
form.dwIndex = 0;
form.dwStyle = CFS_EXCLUDE;
form.ptCurrentPos.x = caret.x;
form.ptCurrentPos.y = caret.y + caret.h;
form.rcArea.top = caret.y;
form.rcArea.bottom = caret.y + caret.h;
form.rcArea.left = caret.x;
form.rcArea.right = caret.x + caret.w;
HIMC hIMC = ImmGetContext(_hWnd);
ImmSetCandidateWindow(hIMC, &form);
ImmReleaseContext(_hWnd, hIMC);
return 0;
}
void QtMiniblinkWebView::onPaintUpdatedCallback(wkeWebView webView, void* param, const HDC hdc, int x, int y, int cx, int cy)
{
QtMiniblinkWebView* pView = (QtMiniblinkWebView*)param;
if (NULL != pView)
{
pView->_pRender->renderOnBlinkPaint(pView->_hWebView, hdc, x, y, cx, cy);
}
}