nim_duilib(8)之combo


introduction

  • 更多控件用法,請參考 here 和 源碼。
  • 本文的代碼基於這里
  • combo的更多用法,請參考源碼中combo.h提供的函數,文末添加其提供的公有函數

xml文件添加代碼

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

<!--第二行控件開始-->
<HBox height="85">
<VBox>
	<!--combobox-->
	<Combo class="list" name="combo" height="30" margin="0,12,0,0" padding="6" bkimage="file='../public/combo/normal.png' corner='5,5,30,5'"/>
</VBox>
</HBox>

這里,創建了一個combo控件,背景來自global.xml同級目錄下public文件夾中的資源。

代碼中關聯

BasicForm.h

  • 打開BasicForm.h,類中添加下面的代碼用於關聯界面控件。
/// combobox
ui::Combo	*pcombo_;

監聽選擇子項事件

類中繼續添加下面的代碼,用於監聽當選擇下拉選項

	// 監聽combo子項被選擇事件
	bool OnComboItemSelected(ui::EventArgs* mgs);

BasicForm.cpp

InitWindow函數

  • 轉到BasicForm.cpp,找到 InitWindow 函數,向其增加下面的代碼
void BasicForm::InitWindow()
{
  	......
	// 7.關聯combo控件
	//----------------------------------------------------------------------------------------
	pcombo_ = dynamic_cast<ui::Combo*>(FindControl(L"combo"));
	// 增加下拉選項
	if (pcombo_)
	{
		for (auto index = 0; index < 5; ++index)
		{
			ui::ListContainerElement* pelement = new(std::nothrow) ui::ListContainerElement;
			if (pelement)
			{
				// 設置屬性
				pelement->SetClass(L"listitem");
				pelement->SetFixedHeight(30);
				pelement->SetBkColor(L"white");
				pelement->SetTextPadding({ 6,0,6,0 });
				pelement->SetText(nbase::StringPrintf(L"index %d", index));

				pcombo_->Add(pelement);
			}
			else
			{
				;
			}
		}

		// 添加結束,設置默認選擇第一項
		int count = pcombo_->GetCount();
		if (0 < count)
			pcombo_->SelectItem(0);

		// 設置向上彈出下拉選項
		pcombo_->SetPopupTop(true);

		// 設置監聽下拉選項被監聽
		pcombo_->AttachSelect(nbase::Bind(&BasicForm::OnComboItemSelected, this, std::placeholders::_1));
	}
}

OnComboItemSelected

OnComboItemSelected函數源碼如下:

bool BasicForm::OnComboItemSelected(ui::EventArgs* msg)
{
	if (pcombo_)
	{
		std::wstring str = std::wstring(L"選擇的文本:") + pcombo_->GetText() + std::wstring(L"\n\n");
		LPCWSTR result = str.c_str();
		OutputDebugString(result);

		str = std::wstring(L"當前索引:") + nbase::StringPrintf(L"%d\n\n", pcombo_->GetCurSel()) ;
		result = str.c_str();
		OutputDebugString(result);
	}

	return false;
}

運行結果

combo的其他公有函數

其公有的函數如下,包括增加和刪除.....

	/// 重寫父類方法,提供個性化功能,請參考父類聲明
	virtual bool Add(Control* pControl) override;
	virtual bool Remove(Control* pControl) override;
	virtual bool RemoveAt(std::size_t iIndex) override;
	virtual void RemoveAll() override;
	virtual void Activate() override;
	virtual void SetAttribute(const std::wstring& strName, const std::wstring& strValue) override;
	virtual void PaintText(IRenderContext* pRender) override;

	/**
	 * @brief 獲取當前選擇項文本
	 * @return 返回當前選擇項文本
	 */
    std::wstring GetText() const;

	/**
	 * @brief 獲取當前所屬的 List 對象
	 * @return 返回所屬的 List 對象指針
	 */
	ListBox* GetListBox() { return m_pLayout.get(); }

	/**
	 * @brief 獲取下拉框屬性信息
	 * @return 返回字符串形式的屬性信息
	 */
    std::wstring GetDropBoxAttributeList();

	/**
	 * @brief 設置下拉框的屬性信息
	 * @param[in] pstrList 轉義后的 XML 格式屬性列表
	 * @return 無
	 */
    void SetDropBoxAttributeList(const std::wstring& pstrList);

	/**
	 * @brief 獲取下拉框容器大小
	 * @return 返回容器大小
	 */
    CSize GetDropBoxSize() const;

	/**
	 * @brief 設置下拉框容器大小
	 * @param[in] szDropBox 要設置的大小信息
	 * @return 無
	 */
    void SetDropBoxSize(CSize szDropBox);
	
	/**
	 * @brief 設置 Combobox 是否向上彈出
	 * @param[in] top 為 true 則向上彈出,false 為默認向下彈出
	 * @return 無
	 */
	void SetPopupTop(bool top) { m_bPopupTop = top; };
	
	/**
	 * @brief 判斷 Combobox 彈出模式是否是向上彈出
	 * @return 返回 true 表示向上彈出,否則為 false
	 */
	bool IsPopupTop() const { return m_bPopupTop; };

	/**
	 * @brief 選擇一個子項
	 * @param[in] iIndex 要選擇的子項索引
	 * @return 返回 true 表示成功,否則為 false
	 */
	bool SelectItem(int iIndex);

	/**
	 * @brief 獲取指定索引下的子項控件
	 * @param[in] iIndex 要獲取的子項索引
	 * @return 返回控件指針
	 */
	Control* GetItemAt(int iIndex);

	/**
	 * @brief 獲取當前選擇項索引
	 * @return 返回當前選擇項索引
	 */
	int GetCurSel() const { return m_iCurSel; }

	/**
	 * @brief 獲取所有子項數量
	 * @return 返回所有子項數量
	 */
	virtual int GetCount() const { return m_pLayout->GetCount(); }
    
	/**
	 * @brief 監聽子項被選擇事件
	 * @param[in] callback 子項被選擇后觸發的回調函數
	 * @return 無
	 */
	void AttachSelect(const EventCallback& callback) { m_pLayout->AttachSelect(callback); }

xml完整源碼

<?xml version="1.0" encoding="UTF-8"?>
<Window size="900,600" 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 height="120">
        <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>

        <!-- option-->
        <VBox>
          <Option class="circle_option_2" name="option1" group="option_group" text="option1" margin="0,3,0,10" selected="true"/>
          <Option class="circle_option_2" name="option2" group="option_group" text="option2" margin="0,3,0,10"/>
          <Option class="circle_option_2" name="option3" group="option_group" text="option3" margin="0,3,0,10"/>
        </VBox>

        <HBox>
          <!-- List -->
          <VListBox class="list" name="list" padding="5,3,5,3">
          </VListBox>
          <VBox>
            
            <!-- Buttons -->
            <CheckBox class="checkbox_font12" name="list_checkbox_add_to_top" text="add to top" margin="0,5,0,10"/>
            <Button class="btn_global_blue_80x30" name="list_btn_add" text="add" />
            
            <CheckBox class="checkbox_font12" name="list_checkbox_remove_all" text="del all?" margin="0,5,0,10"/>
            <Button class="btn_global_white_80x30" name="list_btn_remove" text="remove"/>
          </VBox>
        </HBox>

        <!-- TreeView -->
        <TreeView class="list" name="tree" padding="5,3,5,3" margin="20">
        </TreeView>
      </HBox>

      <!--第二行控件開始-->
      <HBox height="85">
        <VBox>
          <!--combobox-->
          <Combo class="list" name="combo" height="30" margin="0,12,0,0" padding="6" bkimage="file='../public/combo/normal.png' corner='5,5,30,5'"/>
        </VBox>
      </HBox>
      
      
    </VBox> <!--下面是中間的控件 結束-->
  </VBox>
</Window>


免責聲明!

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



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