nim_duilib(4)之CheckBox


introduction

  • 更多控件用法,請參考 here 和 源碼。
  • 本文的代碼基於這里

xml文件添加代碼

基於上一篇, 繼續向basic.xml中添加下面關於CheckBox的代碼。 xml完整源碼在文末。

<!--checkbox-->
<VBox>
  <CheckBox class="checkbox_font12" name="checkbox1" text="checkbox1" margin="0,5,0,10" selected="true"/>
  <CheckBox class="checkbox_font12" name="checkbox2" text="checkbox2" margin="0,5,0,10"/>
  <CheckBox class="checkbox_font12" name="checkbox3" text="checkbox3" margin="0,5,0,10"/>
</VBox>

class屬性來自global.xml中設置的樣式,name是代碼中使用該空間用的,text是控件展示的文字,margin從左到右分別是當前控件距左、上、右、下的距離,selected為true,設置為選中狀態。 可以調用函數IsSelected獲取是否選中。

代碼中關聯

BasicForm.h

  • 打開BasicForm.h,類中添加下面的代碼用於關聯界面控件。
	// 關聯3個checkbox
	ui::CheckBox	*pcb_arr_[count_3];

同時,類中再額外添加兩個函數,用於監聽CheckBox的選中和反選。

	// checkbox點擊處理函數
	bool OnCheckBoxSelected(ui::EventArgs * msg);
	bool OnCheckBoxUnSelected(ui::EventArgs * msg);

BasicForm.cpp

InitWindow函數

  • 轉到BasicForm.cpp,找到 InitWindow 函數,向其增加下面的代碼
void BasicForm::InitWindow()
{
  ......
	// 2.查找界面的3個checkbox控件
	//----------------------------------------------------------------------------------------
	pcb_arr_[0] = dynamic_cast<ui::CheckBox*>(FindControl(L"checkbox1"));
	pcb_arr_[1] = dynamic_cast<ui::CheckBox*>(FindControl(L"checkbox2"));
	pcb_arr_[2] = dynamic_cast<ui::CheckBox*>(FindControl(L"checkbox3"));

	for (auto item : pcb_arr_)
	{
		if (item)
		{
			// 監聽選中
			item->AttachSelect(nbase::Bind(&BasicForm::OnCheckBoxSelected, this, std::placeholders::_1));
			// 監聽反選
			item->AttachUnSelect(nbase::Bind(&BasicForm::OnCheckBoxUnSelected, this, std::placeholders::_1));
		}
	}
}

OnCheckBoxSelected

函數體代碼如下

bool BasicForm::OnCheckBoxSelected(ui::EventArgs * msg)
{
	std::wstring str = msg->pSender->GetName() + std::wstring(L" is selected\n");
	LPCWSTR result = str.c_str();
	OutputDebugString(result);

	return false;
}

OnCheckBoxUnSelected

函數體如下

bool BasicForm::OnCheckBoxUnSelected(ui::EventArgs * msg)
{
	std::wstring str = msg->pSender->GetName() + std::wstring(L" is unselected\n");
	LPCWSTR result = str.c_str();
	OutputDebugString(result);

	return false;
}

運行結果

當選中checkbox和反選時,VS的輸出對話框中將輸出我們設置的監聽處理結果。

xml完整源碼

<?xml version="1.0" encoding="UTF-8"?>
<Window size="600,400" caption="0,0,0,35">
  <VBox bkcolor="bk_wnd_darkcolor">
    <HBox width="stretch" height="35" bkcolor="bk_wnd_lightcolor">
      <Control />
        <Button class="btn_wnd_min" name="minbtn" margin="4,6,0,0" />
        <Box width="21" margin="4,6,0,0">
          <Button class="btn_wnd_max" name="maxbtn"/>
          <Button class="btn_wnd_restore" name="restorebtn" visible="false"/>
        </Box>
      <Button class="btn_wnd_close" name="closebtn" margin="4,6,8,0"/>
    </HBox>

    <!--下面是中間的控件-->
    <VBox padding="30, 30, 30, 30" >   
      <HBox>
        <VBox>
          <!-- Buttons -->
          <Button class="btn_global_blue_80x30" name="btn_blue" text="blue" />
          <Button class="btn_global_white_80x30" name="btn_white" text="white"/>
          <Button class="btn_global_red_80x30" name="btn_red" text="red"/>
        </VBox>
        
        <!--checkbox-->
        <VBox>
          <CheckBox class="checkbox_font12" name="checkbox1" text="checkbox1" margin="0,5,0,10" selected="true"/>
          <CheckBox class="checkbox_font12" name="checkbox2" text="checkbox2" margin="0,5,0,10"/>
          <CheckBox class="checkbox_font12" name="checkbox3" text="checkbox3" margin="0,5,0,10"/>
        </VBox>

      </HBox>
    </VBox> <!--下面是中間的控件 結束-->
  </VBox>
</Window>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM