前面使用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()
{

博客讲述了此前用GDI绘制解决了集成到Qt的系统冲突和QWebEngineView的冲突,但仅支持作为qt子窗体。现在将其修改为支持独立窗口创建,介绍了判断创建独立窗口的方法,增加标题和相关按钮,去掉对父窗口过程截获,还说明了使用方式并给出完整代码。
最低0.47元/天 解锁文章
2518

被折叠的 条评论
为什么被折叠?



