duilib 借助 MiniBlink实现浏览器窗体

#ifndef __UIWEBCEF_H__
#define __UIWEBCEF_H__


namespace DuiLib
{
	class CWebCefWnd;
	class UILIB_API CWebCefUI : public CControlUI
	{
		friend class CWebCefWnd;
	public:
		CWebCefUI();
		virtual ~CWebCefUI();
	public:
		virtual LPCTSTR GetClass() const;
		virtual LPVOID GetInterface(LPCTSTR pstrName);
		virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
		void SetText(LPCTSTR text);
		void SetPos(RECT rc);
		void DoInit();
		virtual void SetVisible(bool bVisible = true);
		virtual void SetInternVisible(bool bVisible = true);
		HWND GetCurCtrHwnd();
		void SetHWND(HWND hWnd);
		void PaintStatusImage(HDC hDC);
		void SetBkColor(DWORD dwColor);
		CWebCefWnd* GetWebCefWnd();
		RECT CalPos();
	protected:
		HWND m_hWnd;
		CWebCefWnd* m_pWindow;
		DWORD m_dBkColor;
	public:
		bool m_IsFirst;

	};
}
#endif // __UIWEBCEF_H__
#include "StdAfx.h"
#include "UIWebCef.h"


namespace DuiLib
{
	LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

	class CWebCefWnd 
	{
	public:
		CWebCefWnd();

		void Init(CWebCefUI* pOwner);
		ATOM                MyRegisterClass(HINSTANCE hInstance);
		BOOL                InitInstance(HINSTANCE, int);
	protected:
		CWebCefUI* m_pOwner;
		HBRUSH m_hBkBrush;
		bool m_bInit;
	public:
		HWND m_hWnd;
	};

	CWebCefUI::CWebCefUI() :m_hWnd(NULL), m_pWindow(NULL), m_IsFirst(TRUE)
	{
	}

	CWebCefUI::~CWebCefUI()
	{
	}

	LPCTSTR CWebCefUI::GetClass() const
	{
		return _T("WebCefUI");
	}

	LPVOID CWebCefUI::GetInterface(LPCTSTR pstrName)
	{
		if (_tcscmp(pstrName, DUI_CTR_WEBCEF) == 0) return static_cast<CWebCefUI*>(this);
		return CControlUI::GetInterface(pstrName);
	}

	void CWebCefUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		if (_tcscmp(pstrName, _T("text")) == 0) {

		}
		else if (_tcscmp(pstrName, _T("bkcolor")) == 0)
		{
			if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetBkColor(clrColor);
		}
		CControlUI::SetAttribute(pstrName, pstrValue);
	}

	void CWebCefUI::SetText(LPCTSTR text)
	{
	}

	void CWebCefUI::SetPos(RECT rc)
	{
		
		CControlUI::SetPos(rc);
		{
			if (m_pWindow != NULL && m_pWindow->m_hWnd)
			{
				//SendMessage(m_pManager->GetPaintWindow(), WM_UPDATE_SIZE, (WPARAM)this, 0);
				RECT rc = CalPos();
				::SetWindowPos(m_pWindow->m_hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOACTIVATE);
			}
		}
	}

	void CWebCefUI::DoInit()
	{
		if (IsEnabled()) {
			GetManager()->ReleaseCapture();
			if (m_pWindow == NULL)
			{
				m_pWindow = new CWebCefWnd();
				ASSERT(m_pWindow);
				m_pWindow->Init(this);
				//m_pWindow->ShowWindow();
			}
		}

	}

	void CWebCefUI::SetVisible(bool bVisible)
	{
		CControlUI::SetVisible(bVisible);
		if (!IsVisible() && m_pWindow != NULL) m_pManager->SetFocus(NULL);

	}

	void CWebCefUI::SetInternVisible(bool bVisible)
	{
		if (!IsVisible() && m_pWindow != NULL) m_pManager->SetFocus(NULL);

	}

	HWND CWebCefUI::GetCurCtrHwnd()
	{
		return m_hWnd;
	}
	void CWebCefUI::SetHWND(HWND hWnd)
	{
		m_hWnd = hWnd;
	}
	void CWebCefUI::PaintStatusImage(HDC hDC)
	{
		if (m_dBkColor != 0)
		{
			CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dBkColor));
			return;
		}
	}
	void CWebCefUI::SetBkColor(DWORD dwColor)
	{
		m_dBkColor = dwColor;
	}

	CWebCefWnd * CWebCefUI::GetWebCefWnd()
	{
		return m_pWindow;
	}

	RECT CWebCefUI::CalPos()
	{
		CDuiRect rcPos = GetPos();
		RECT rcInset;// = m_pOwner->GetTextPadding();
		rcInset.left = 1;
		rcInset.top = 0;
		rcInset.right = 1;
		rcInset.bottom = 1;
		rcPos.left += rcInset.left;
		rcPos.top += rcInset.top;
		rcPos.right -= rcInset.right;
		rcPos.bottom -= rcInset.bottom;
		
		return rcPos;
	}
	
	CWebCefWnd::CWebCefWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false), m_hWnd(NULL)
	{
	}
	void CWebCefWnd::Init(CWebCefUI * pOwner)
	{
		m_pOwner = pOwner;
		m_bInit = true;
		if (m_hWnd == NULL)
		{
			//RECT rcPos = m_pOwner->GetPos();
			//UINT uStyle = UI_WNDSTYLE_CHILD;// UI_WNDSTYLE_CHILD | CS_HREDRAW | CS_VREDRAW;;

			//Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
			MyRegisterClass(m_pOwner->GetManager()->GetInstance());

			if (!InitInstance(m_pOwner->GetManager()->GetInstance(), 1))
			{
				return ;
			}
			ShowWindow(m_hWnd, SW_NORMAL);
			m_pOwner->SetHWND(m_hWnd);
		}
		
	}
	ATOM CWebCefWnd::MyRegisterClass(HINSTANCE hInstance)
	{
		WNDCLASSEXW wcex;

		wcex.cbSize = sizeof(WNDCLASSEX);

		wcex.style = CS_HREDRAW | CS_VREDRAW;
		wcex.lpfnWndProc = WndProc;
		wcex.cbClsExtra = 0;
		wcex.cbWndExtra = 0;
		wcex.hInstance = hInstance;
		wcex.hIcon = NULL; //LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT));
		wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
		wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 0)); //(HBRUSH)(COLOR_WINDOW+1);//这里要设置窗口背景色,又不然重绘有问题,能力有限不知道怎么解决
		wcex.lpszMenuName = NULL; //MAKEINTRESOURCEW(IDC_WIN32PROJECT);
		wcex.lpszClassName = _T("CWebCefWnd");
		wcex.hIconSm = NULL;// LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

		return RegisterClassExW(&wcex);
	}
	BOOL CWebCefWnd::InitInstance(HINSTANCE, int)
	{
		 m_hWnd = CreateWindow(_T("CWebCefWnd"), _T("CWebCefUI"), UI_WNDSTYLE_CHILD, 0, 0, 0, 0, m_pOwner->GetManager()->GetPaintWindow(), NULL, NULL, NULL);
		if (m_hWnd==NULL)
		{
			return FALSE;
		}
		return TRUE;
	}
	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		switch (message)
		{
		case WM_COMMAND:
		{
			
				return DefWindowProc(hWnd, message, wParam, lParam);
		
		}
		break;
		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code that uses hdc here...
			EndPaint(hWnd, &ps);
		}
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		return 0;
	}
}
//在miniblink的mb_demo中的函数添加一下代码:即duilib自定义的窗口类:LoginWnd

bool createWebWindow(Application* app)
{

	CLoginWnd *pLogin = new CLoginWnd(_T("WebCef.xml"));
	pLogin->Create(NULL, _T("xxxxxxxxxxxxxx"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	HWND hwnd = NULL;
	if (pLogin->m_pWebCef)
	{
		hwnd = pLogin->m_pWebCef->GetCurCtrHwnd();
	}
	g_Hwnd = pLogin->GetHWND();
	RECT rc;
	::GetClientRect(pLogin->m_pWebCef->GetCurCtrHwnd(), &rc);
    app->window = wkeCreateWebWindow(WKE_WINDOW_TYPE_CONTROL, hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
    if (!app->window)
        return false;
	pLogin->SetWkeView(app->window);
    //SetWindowSubclass(wkeGetWindowHandle(app->window), subClassProc, 0, 0);
   // setMoveWindowArea(10, 10, 640, 30); // 设置窗口可拖动区域,用于无边框窗体

    wkeSetWindowTitleW(app->window, L"miniblink demo");

    wkeOnDidCreateScriptContext(app->window, onDidCreateScriptContextCallback, app);
    wkeOnWindowClosing(app->window, handleWindowClosing, app);
    wkeOnWindowDestroy(app->window, handleWindowDestroy, app);
    wkeOnDocumentReady(app->window, handleDocumentReady, app);
    wkeOnTitleChanged(app->window, handleTitleChanged, app);
    wkeOnCreateView(app->window, onCreateView, app);
    wkeOnLoadUrlBegin(app->window, onLoadUrlBegin, app);
    wkeOnLoadUrlEnd(app->window, onLoadUrlEnd, app);
    wkeSetDebugConfig(app->window, "decodeUrlRequest", nullptr);

    wkeJsBindFunction("eMsg", &onMsg, nullptr, 5);
    wkeJsBindFunction("eShellExec", &onShellExec, nullptr, 3);
    wkeMoveToCenter(app->window);
    wkeLoadURLW(app->window, app->url.c_str());
//这个定时器是实时刷新浏览器窗口大小,实现与整个duilib窗口一样大。
	SetTimer(pLogin->GetHWND(), 1, 500, NULL);
    return true;
}
在LoginWnd类中的LRESULT CLoginWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)函数中处理这2个消息,保证浏览器窗体和窗体一样大小。
case WM_SIZE:
	{
		if (m_pWebCef)
		{
			if (m_wkeView)
			{
				{
					RECT rc;
					::GetClientRect(m_pWebCef->GetCurCtrHwnd(), &rc);
					wkeResizeWindow(m_wkeView, rc.right - rc.left, rc.bottom - rc.top);
				}

			}
			
		}
		break;
	}
	case WM_TIMER:
	{
		if (m_wkeView)
		{
			{
				RECT rc;
				::GetClientRect(m_pWebCef->GetCurCtrHwnd(), &rc);
				wkeResizeWindow(m_wkeView, rc.right - rc.left, rc.bottom - rc.top);
			}

		}
		break;
	}

MiniBlink网址:

https://weolar.github.io/miniblink/index.html

猜你喜欢

转载自blog.csdn.net/abqchina/article/details/83988574