3.20 Cef浏览器控件
<p>Cef浏览器控件由CSkinCefView类来代表,继承于CSkinView,支持CSkinView的所有属性和方法。</p>
<h2>1 属性和相关方法</h2>
<h4>1.1 设置URL</h4>
<ul>
<li>通过XML属性控制如下:
<pre><code class="language-xml">URL=&quot;www.baidu.com&quot;</code></pre></li>
<li>通过C++程序代码调用方法控制如下:
<pre><code class="language-c">void LoadURL(const tstring&amp; strURL);</code></pre></li>
</ul>
<h2>请看下面的示例</h2>
<h4>效果图</h4>
<p><img src="http://www.skinui.cn/doc/img/3/20/1.png" alt="Cef浏览器控件" /></p>
<h4>布局文件</h4>
<pre><code class="language-xml">&lt;SkinDialog DefaultWidth=&quot;500&quot; DefaultHeight=&quot;480&quot; SysButton=&quot;CLOSE&quot; Icon=&quot;128&quot; Caption=&quot;IDS_CONTROL_SHOW20&quot;&gt;
&lt;CefWebView Id=&quot;101&quot; LayoutWidth=&quot;225&quot; AlignParentLeft=&quot;15&quot; AlignParentTop=&quot;45&quot; AlignParentBottom=&quot;15&quot;/&gt;
&lt;SkinRelativeLayout Id=&quot;102&quot; ToRightOf=&quot;101,15&quot; AlignParentRight=&quot;15&quot; AlignParentTop=&quot;61&quot; AlignParentBottom=&quot;15&quot;&gt;
&lt;SkinButton Id=&quot;201&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(1个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;0&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;202&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(2个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;42&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;203&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(3个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;84&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;204&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(4个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;126&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;205&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(5个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;168&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;206&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(6个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;210&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;207&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(7个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;252&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;208&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(8个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;294&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;SkinButton Id=&quot;209&quot; LayoutWidth=&quot;FillParent&quot; LayoutHeight=&quot;22&quot; ChildText1=&quot;C++调用JS方法(9个参数)&quot; Image=&quot;Button.png&quot; Layout=&quot;Button.xml&quot; AlignParentTop=&quot;336&quot; PressDownOffset=&quot;1&quot;/&gt;
&lt;/SkinRelativeLayout&gt;
&lt;/SkinDialog&gt;</code></pre>
<h4>h文件</h4>
<pre><code class="language-cpp">#pragma once
class CCefWebView : public CSkinCefView
{
public:
CCefWebView(CSkinView* pParent);
SKINUI_DECLARE_DYNCREATE()
public:
void OnFunction1Param(const tstring&amp; param1);
void OnFunction2Param(const tstring&amp; param1, const tstring&amp; param2);
void OnFunction3Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3);
void OnFunction4Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4);
void OnFunction5Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5);
void OnFunction6Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6);
void OnFunction7Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7);
void OnFunction8Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7, const tstring&amp; param8);
void OnFunction9Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7, const tstring&amp; param8, const tstring&amp; param9);
SKINUI_DECLARE_EXTENSION_MESSAGE_MAP()
};
class CCefWebDialog : public CSkinDialog
{
public:
enum
{
IDC_WEBVIEW = 101,
IDC_BUTTON1 = 201,
IDC_BUTTON2 = 202,
IDC_BUTTON3 = 203,
IDC_BUTTON4 = 204,
IDC_BUTTON5 = 205,
IDC_BUTTON6 = 206,
IDC_BUTTON7 = 207,
IDC_BUTTON8 = 208,
IDC_BUTTON9 = 209,
};
public:
CCefWebDialog(const tstring&amp; strXml);
public:
virtual void OnInitDialog();
protected:
void OnBtnClicked(UINT uNotifyCode, int nID, CSkinView* pView);
SKINUI_DECLARE_MESSAGE_MAP()
private:
CCefWebView* m_pCefWebView;
};</code></pre>
<h4>CefWebView.cpp</h4>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &quot;CefWebDialog.h&quot;
SKINUI_IMPLEMENT_DYNCREATE(CCefWebView)
SKINUI_BEGIN_EXTENSION_MESSAGE_MAP(CCefWebView, CSkinCefView)
ON_SKINUI_EXTENSION_MESSAGE(Function1Param, OnFunction1Param)
ON_SKINUI_EXTENSION_MESSAGE(Function2Param, OnFunction2Param)
ON_SKINUI_EXTENSION_MESSAGE(Function3Param, OnFunction3Param)
ON_SKINUI_EXTENSION_MESSAGE(Function4Param, OnFunction4Param)
ON_SKINUI_EXTENSION_MESSAGE(Function5Param, OnFunction5Param)
ON_SKINUI_EXTENSION_MESSAGE(Function6Param, OnFunction6Param)
ON_SKINUI_EXTENSION_MESSAGE(Function7Param, OnFunction7Param)
ON_SKINUI_EXTENSION_MESSAGE(Function8Param, OnFunction8Param)
ON_SKINUI_EXTENSION_MESSAGE(Function9Param, OnFunction9Param)
SKINUI_END_EXTENSION_MESSAGE_MAP()
CCefWebView::CCefWebView(CSkinView* pParent)
: CSkinCefView(pParent)
{
}
void CCefWebView::OnFunction1Param(const tstring&amp; param1)
{
tstring str = param1;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction2Param(const tstring&amp; param1, const tstring&amp; param2)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction3Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction4Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction5Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
str += _T(&quot;\r\n&quot;) + param5;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction6Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
str += _T(&quot;\r\n&quot;) + param5;
str += _T(&quot;\r\n&quot;) + param6;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction7Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
str += _T(&quot;\r\n&quot;) + param5;
str += _T(&quot;\r\n&quot;) + param6;
str += _T(&quot;\r\n&quot;) + param7;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction8Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7, const tstring&amp; param8)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
str += _T(&quot;\r\n&quot;) + param5;
str += _T(&quot;\r\n&quot;) + param6;
str += _T(&quot;\r\n&quot;) + param7;
str += _T(&quot;\r\n&quot;) + param8;
GetOwner()-&gt;MsgBox(str);
}
void CCefWebView::OnFunction9Param(const tstring&amp; param1, const tstring&amp; param2, const tstring&amp; param3, const tstring&amp; param4, const tstring&amp; param5, const tstring&amp; param6, const tstring&amp; param7, const tstring&amp; param8, const tstring&amp; param9)
{
tstring str = param1;
str += _T(&quot;\r\n&quot;) + param2;
str += _T(&quot;\r\n&quot;) + param3;
str += _T(&quot;\r\n&quot;) + param4;
str += _T(&quot;\r\n&quot;) + param5;
str += _T(&quot;\r\n&quot;) + param6;
str += _T(&quot;\r\n&quot;) + param7;
str += _T(&quot;\r\n&quot;) + param8;
str += _T(&quot;\r\n&quot;) + param9;
GetOwner()-&gt;MsgBox(str);
}</code></pre>
<h4>CefWebDialog.cpp</h4>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &quot;CefWebDialog.h&quot;
SKINUI_BEGIN_MESSAGE_MAP(CCefWebDialog, CSkinDialog)
ON_SKINUI_COMMAND_RANGE(IDC_BUTTON1, IDC_BUTTON9, OnBtnClicked)
SKINUI_END_MESSAGE_MAP()
CCefWebDialog::CCefWebDialog(const tstring&amp; strXml)
: CSkinDialog(strXml)
, m_pCefWebView(NULL)
{
}
void CCefWebDialog::OnInitDialog()
{
CSkinDialog::OnInitDialog();
m_pCefWebView = static_cast&lt;CCefWebView*&gt;(GetChildById(IDC_WEBVIEW));
if(m_pCefWebView)
{
tstring strURL = GetCurModulePath() + _T(&quot;web\\SkinUI.html&quot;);
m_pCefWebView-&gt;LoadURL(strURL);
}
}
void CCefWebDialog::OnBtnClicked(UINT uNotifyCode, int nID, CSkinView* pView)
{
if(m_pCefWebView)
{
tstring strJavaScript;
if(nID == IDC_BUTTON1)
{
strJavaScript = _T(&quot;Function1Param('11111');&quot;);
}
else if(nID == IDC_BUTTON2)
{
strJavaScript = _T(&quot;Function2Param('11111', '22222');&quot;);
}
else if(nID == IDC_BUTTON3)
{
strJavaScript = _T(&quot;Function3Param('11111', '22222', '33333');&quot;);
}
else if(nID == IDC_BUTTON4)
{
strJavaScript = _T(&quot;Function4Param('11111', '22222', '33333', '44444');&quot;);
}
else if(nID == IDC_BUTTON5)
{
strJavaScript = _T(&quot;Function5Param('11111', '22222', '33333', '44444', '55555');&quot;);
}
else if(nID == IDC_BUTTON6)
{
strJavaScript = _T(&quot;Function6Param('11111', '22222', '33333', '44444', '555555', '66666');&quot;);
}
else if(nID == IDC_BUTTON7)
{
strJavaScript = _T(&quot;Function7Param('11111', '22222', '33333', '44444', '55555', '66666', '77777');&quot;);
}
else if(nID == IDC_BUTTON8)
{
strJavaScript = _T(&quot;Function8Param('11111', '22222', '33333', '44444', '55555', '66666', '77777', '88888');&quot;);
}
else if(nID == IDC_BUTTON9)
{
strJavaScript = _T(&quot;Function9Param('11111', '22222', '33333', '44444', '55555', '66666', '77777', '88888', '99999');&quot;);
}
m_pCefWebView-&gt;RunJavaScript(strJavaScript);
}
}</code></pre>