139 lines
3.3 KiB
C++
139 lines
3.3 KiB
C++
![]() |
#include "stdafx.h"
|
|||
|
#include "cef_form.h"
|
|||
|
|
|||
|
const std::wstring CefForm::kClassName = L"CEF_Control_Example";
|
|||
|
|
|||
|
CefForm::CefForm()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
CefForm::~CefForm()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
std::wstring CefForm::GetSkinFolder()
|
|||
|
{
|
|||
|
return L"cef";
|
|||
|
}
|
|||
|
|
|||
|
std::wstring CefForm::GetSkinFile()
|
|||
|
{
|
|||
|
return L"cef.xml";
|
|||
|
}
|
|||
|
|
|||
|
std::wstring CefForm::GetWindowClassName() const
|
|||
|
{
|
|||
|
return kClassName;
|
|||
|
}
|
|||
|
|
|||
|
ui::Control* CefForm::CreateControl(const std::wstring& pstrClass)
|
|||
|
{
|
|||
|
// ɨ<><C9A8> XML <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ CefControl <20>Ľڵ㣬<DAB5><EFBFBD>һ<EFBFBD><D2BB> ui::CefControl <20>ؼ<EFBFBD>
|
|||
|
if (pstrClass == L"CefControl")
|
|||
|
{
|
|||
|
if (nim_cef::CefManager::GetInstance()->IsEnableOffsetRender())
|
|||
|
return new ui::CefControl;
|
|||
|
else
|
|||
|
return new ui::CefNativeControl;
|
|||
|
}
|
|||
|
|
|||
|
return NULL;
|
|||
|
}
|
|||
|
|
|||
|
void CefForm::InitWindow()
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>굥<EFBFBD><EAB5A5><EFBFBD>¼<EFBFBD>
|
|||
|
m_pRoot->AttachBubbledEvent(ui::kEventClick, nbase::Bind(&CefForm::OnClicked, this, std::placeholders::_1));
|
|||
|
|
|||
|
// <20><> XML <20>в<EFBFBD><D0B2><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ؼ<EFBFBD>
|
|||
|
cef_control_ = dynamic_cast<ui::CefControlBase*>(FindControl(L"cef_control"));
|
|||
|
cef_control_dev_ = dynamic_cast<ui::CefControlBase*>(FindControl(L"cef_control_dev"));
|
|||
|
btn_dev_tool_ = dynamic_cast<ui::Button*>(FindControl(L"btn_dev_tool"));
|
|||
|
edit_url_ = dynamic_cast<ui::RichEdit*>(FindControl(L"edit_url"));
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
edit_url_->SetSelAllOnFocus(true);
|
|||
|
edit_url_->AttachReturn(nbase::Bind(&CefForm::OnNavigate, this, std::placeholders::_1));
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ
|
|||
|
cef_control_->AttachLoadEnd(nbase::Bind(&CefForm::OnLoadEnd, this, std::placeholders::_1));
|
|||
|
|
|||
|
// <20><EFBFBD><F2BFAABF><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD>
|
|||
|
cef_control_->AttachDevTools(cef_control_dev_);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4>Ŀ¼<C4BF>µ<EFBFBD> html <20>ļ<EFBFBD>
|
|||
|
cef_control_->LoadURL(QPath::GetAppPath() + L"resources\\themes\\default\\cef\\cef.html");
|
|||
|
|
|||
|
if (!nim_cef::CefManager::GetInstance()->IsEnableOffsetRender())
|
|||
|
cef_control_dev_->SetVisible(false);
|
|||
|
}
|
|||
|
|
|||
|
LRESULT CefForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
|||
|
{
|
|||
|
nim_cef::CefManager::GetInstance()->PostQuitMessage(0L);
|
|||
|
return __super::OnClose(uMsg, wParam, lParam, bHandled);
|
|||
|
}
|
|||
|
|
|||
|
bool CefForm::OnClicked(ui::EventArgs* msg)
|
|||
|
{
|
|||
|
std::wstring name = msg->pSender->GetName();
|
|||
|
|
|||
|
if (name == L"btn_dev_tool")
|
|||
|
{
|
|||
|
if (cef_control_->IsAttachedDevTools())
|
|||
|
{
|
|||
|
cef_control_->DettachDevTools();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
cef_control_->AttachDevTools(cef_control_dev_);
|
|||
|
}
|
|||
|
|
|||
|
if (nim_cef::CefManager::GetInstance()->IsEnableOffsetRender())
|
|||
|
{
|
|||
|
cef_control_dev_->SetVisible(cef_control_->IsAttachedDevTools());
|
|||
|
}
|
|||
|
}
|
|||
|
else if (name == L"btn_back")
|
|||
|
{
|
|||
|
cef_control_->GoBack();
|
|||
|
}
|
|||
|
else if (name == L"btn_forward")
|
|||
|
{
|
|||
|
cef_control_->GoForward();
|
|||
|
}
|
|||
|
else if (name == L"btn_navigate")
|
|||
|
{
|
|||
|
OnNavigate(nullptr);
|
|||
|
}
|
|||
|
else if (name == L"btn_refresh")
|
|||
|
{
|
|||
|
cef_control_->Refresh();
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
bool CefForm::OnNavigate(ui::EventArgs* msg)
|
|||
|
{
|
|||
|
if (!edit_url_->GetText().empty())
|
|||
|
{
|
|||
|
cef_control_->LoadURL(edit_url_->GetText());
|
|||
|
cef_control_->SetFocus();
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
void CefForm::OnLoadEnd(int httpStatusCode)
|
|||
|
{
|
|||
|
FindControl(L"btn_back")->SetEnabled(cef_control_->CanGoBack());
|
|||
|
FindControl(L"btn_forward")->SetEnabled(cef_control_->CanGoForward());
|
|||
|
|
|||
|
// ע<><D7A2>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṩǰ<E1B9A9>˵<EFBFBD><CBB5><EFBFBD>
|
|||
|
cef_control_->RegisterCppFunc(L"ShowMessageBox", ToWeakCallback([this](const std::string& params, nim_cef::ReportResultFunction callback) {
|
|||
|
shared::Toast::ShowToast(nbase::UTF8ToUTF16(params), 3000, GetHWND());
|
|||
|
callback(false, R"({ "message": "Success." })");
|
|||
|
}));
|
|||
|
}
|