3.7 单选框
<p>单选框由CSkinRadioButton类来代表,继承于CSkinButton,支持CSkinButton的所有属性和方法。</p>
<p>单选框的父组件的父组件必须为CSkinRadioGroup或其子类,用来将单选框分组。选中组框中的一个单选框时,自动取消其他单选框的选中状态。通过给单选框设置不同的布局文件,可以得到各种形态的单选框。</p>
<h2>1 属性和相关方法</h2>
<h4>1.1 设置单选框是否选中</h4>
<ul>
<li>通过XML属性控制如下:
<pre><code class="language-xml">Checked=&quot;true&quot;</code></pre></li>
<li>通过C++程序代码调用方法控制如下:
<pre><code class="language-c">virtual void SetChecked(BOOL bChecked);</code></pre></li>
</ul>
<h4>1.2 设置单选框绑定的视图</h4>
<p>通过给复选框设置不同的布局文件,可以得到不同形态的单选框。例如:通常使用单选框来实现Tab控件。
单选框被选中时,绑定的视图同时显示;单选框取消选中时,绑定的视图同时隐藏。</p>
<ul>
<li>通过XML属性控制如下:
<pre><code class="language-xml">BindView=&quot;1000&quot;</code></pre></li>
<li>通过C++程序代码调用方法控制如下:
<pre><code class="language-c">virtual void SetBindView(LONG nBindView);</code></pre></li>
</ul>
<h2>2 消息处理</h2>
<h4>2.1 WM_COMMAND消息</h4>
<p>单击鼠标左键按下时,如果单选框Id不为0,会给所在的窗口发送WM_COMMAND消息。</p>
<p>参考下面的示例代码:</p>
<pre><code class="language-cpp">void CSkinRadioButton::HandleLButtonDown(CPoint point, BOOL&amp; bHandle)
{
if(GetId() != 0)
{
GetOwner()-&gt;PostMessage(WM_COMMAND, GetId(), reinterpret_cast&lt;LPARAM&gt;(this));
}
CSkinRadioGroup* pGroupBox = static_cast&lt;CSkinRadioGroup*&gt;(GetParent());
if(pGroupBox)
{
if(!IsChecked())
{
CRedrawLocker locker(GetOwner());
pGroupBox-&gt;SetChecked(this);
}
}
CSkinButton::HandleLButtonDown(point, bHandle);
}</code></pre>
<h4>2.1 WM_CHECKBOX_STATE_CHANGE消息</h4>
<p>单选框选中状态发生变化时,会给所在窗口发送自定义消息WM_RADIO_GROUP_CHECKED_CHANGE。</p>
<p>参考下面的示例代码:</p>
<pre><code class="language-cpp">void CSkinRadioGroup::SetChecked(CSkinRadioButton* pRadioButton)
{
if(pRadioButton)
{
CSkinRadioButton* pChecked = GetChecked();
if(pChecked &amp;&amp; pChecked != pRadioButton)
{
pChecked-&gt;SetChecked(FALSE);
if(pChecked-&gt;IsVisible())
{
pChecked-&gt;SetState(VS_NORMAL);
}
CSkinView* pBindView = GetOwner()-&gt;GetChildById(pChecked-&gt;GetBindView());
if(pBindView)
{
pBindView-&gt;SetState(VS_HIDED);
}
}
pRadioButton-&gt;SetChecked(TRUE);
GetOwner()-&gt;SendMessage(WM_RADIO_GROUP_CHECKED_CHANGE, GetId(), reinterpret_cast&lt;LPARAM&gt;(pRadioButton));
CSkinView* pBindView = GetOwner()-&gt;GetChildById(pRadioButton-&gt;GetBindView());
if(pBindView)
{
pBindView-&gt;SetState(VS_NORMAL);
}
}
}</code></pre>
<h2>请看下面的示例</h2>
<h4>效果图</h4>
<p><img src="http://www.skinui.cn/doc/img/3/7/1.png" alt="单选框" /></p>
<h4>布局文件</h4>
<pre><code class="language-xml">&lt;SkinDialog DefaultWidth=&quot;400&quot; DefaultHeight=&quot;300&quot; SysButton=&quot;CLOSE&quot; Icon=&quot;128&quot; Caption=&quot;IDS_CONTROL_SHOW7&quot; Animation=&quot;SizeChange&quot;&gt;
&lt;SkinRadioGroup Id=&quot;100&quot; LayoutWidth=&quot;200&quot; LayoutHeight=&quot;60&quot; AlignParentLeft=&quot;50&quot; AlignParentTop=&quot;100&quot;&gt;
&lt;SkinRadioButton Id=&quot;101&quot; LayoutWidth=&quot;200&quot; LayoutHeight=&quot;30&quot; ChildText1=&quot;IDS_CONTROL_SHOW_TEXT10&quot; Layout=&quot;RadioButton.xml&quot; AlignParentTop=&quot;0&quot;/&gt;
&lt;SkinRadioButton Id=&quot;102&quot; LayoutWidth=&quot;200&quot; LayoutHeight=&quot;30&quot; ChildText2=&quot;IDS_CONTROL_SHOW_TEXT11&quot; ChildImage1=&quot;Icon.png&quot; Layout=&quot;RadioButtonWidthIcon.xml&quot; AlignParentTop=&quot;30&quot;/&gt;
&lt;/SkinRadioGroup&gt;
&lt;/SkinDialog&gt;</code></pre>
<h4>h文件</h4>
<pre><code class="language-cpp">#pragma once
class CRadioButtonDialog : public CSkinDialog
{
public:
enum
{
IDC_RADIOBUTTON1 = 101,
IDC_RADIOBUTTON2 = 102,
};
public:
CRadioButtonDialog(const tstring&amp; strXml);
public:
virtual void OnInitDialog();
protected:
void OnRadioButtonClicked(UINT uNotifyCode, int nID, CSkinView* pView);
LRESULT OnRadioGroupCheckedChange(WPARAM wParam, LPARAM lParam);
SKINUI_DECLARE_MESSAGE_MAP()
};</code></pre>
<h4>cpp文件</h4>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &quot;RadioButtonDialog.h&quot;
SKINUI_BEGIN_MESSAGE_MAP(CRadioButtonDialog, CSkinDialog)
ON_SKINUI_COMMAND(IDC_RADIOBUTTON1, OnRadioButtonClicked)
ON_SKINUI_COMMAND(IDC_RADIOBUTTON2, OnRadioButtonClicked)
ON_SKINUI_MESSAGE(WM_RADIO_GROUP_CHECKED_CHANGE, OnRadioGroupCheckedChange)
SKINUI_END_MESSAGE_MAP()
CRadioButtonDialog::CRadioButtonDialog(const tstring&amp; strXml)
: CSkinDialog(strXml)
{
}
void CRadioButtonDialog::OnInitDialog()
{
CSkinDialog::OnInitDialog();
}
void CRadioButtonDialog::OnRadioButtonClicked(UINT uNotifyCode, int nID, CSkinView* pView)
{
CSkinRadioButton* pRadioButton = static_cast&lt;CSkinRadioButton*&gt;(pView);
if(pRadioButton)
{
MsgBox(SkinUI::StringFormat(_T(&quot;OnRadioButtonClicked,Id=%d, IsChecked()=%s!&quot;), nID, pRadioButton-&gt;IsChecked() ? _T(&quot;true&quot;) : _T(&quot;false&quot;)));
}
}
LRESULT CRadioButtonDialog::OnRadioGroupCheckedChange(WPARAM wParam, LPARAM lParam)
{
CSkinRadioButton* pRadioButton = reinterpret_cast&lt;CSkinRadioButton*&gt;(lParam);
if(pRadioButton)
{
MsgBox(SkinUI::StringFormat(_T(&quot;OnRadioGroupCheckedChange,RadioGroup Id=%d,IsChecked()=%s!&quot;), wParam, pRadioButton-&gt;IsChecked() ? _T(&quot;true&quot;) : _T(&quot;false&quot;)));
}
return 1L;
}</code></pre>