Qt 集成miniblink浏览器库之5 支持独立窗口和子窗口

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

前面使用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()
    {
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值