1. 程式人生 > >duilib 如果獲取listui 當前選擇的項GetCursel

duilib 如果獲取listui 當前選擇的項GetCursel

<pre name="code" class="cpp">class CDuiFrameWnd : public WindowImplBase
{
public:
	virtual LPCTSTR    GetWindowClassName() const   {   return _T("DUIMainFrame");  }
	virtual CDuiString GetSkinFile()                {   return _T("duilib1.xml");  }
	virtual CDuiString GetSkinFolder()              {   return _T("");  }

	virtual void  InitWindow()
	{

		CDuiString str;
		CListUI* pList = static_cast<CListUI*>(m_PaintManager.FindControl(_T("ListDemo1")));

		// 新增List列表內容,必須先Add(pListElement),再SetText
		for (int i = 0; i < 5; i++)
		{
			CListTextElementUI* pListElement = new CListTextElementUI;
			pListElement->SetTag(i);
			pList->Add(pListElement);

			str.Format(_T("%d"), i);
			pListElement->SetText(0, str);
			pListElement->SetText(1, _T("haha"));
		}
	}

	void Notify(TNotifyUI& msg)
	{
		if (msg.sType == _T("itemclick"))
		{
				CListUI* pList = static_cast<CListUI*>(m_PaintManager.FindControl(_T("ListDemo1")));
				if (pList != NULL)
				{
					int nIndex  = pList->GetCurSel();
					//對於列表框大於2列的情況 用以下方式進行獲取兩列的值 和進行設定
					//切記 列表框初始化的時候 一定要兩列都初始化,否則SetText函式會執行失敗
					//如果有大於2列的列表框,xml資源裡面切記不要
					/*
					<List name="ListDemo1" float="true" pos="30,314,0,0" width="139" height="218" bkcolor="#FFFFFFFF" itemtextcolor="#FF000000" itembkcolor="#FFE2DDDF" itemselectedtextcolor="#FF000000" itemselectedbkcolor="#FFC1E3FF" itemhottextcolor="#FF000000" itemhotbkcolor="#FFE9F5FF" itemdisabledtextcolor="#FFCCCCCC" itemdisabledbkcolor="#FFFFFFFF" >
					<ListHeader name="domain" bkimage="List/list_header_bg.png">
					<ListHeaderItem text="序號" width="40" height="23" minwidth="16"  sepwidth="1" align="center" hotimage="List/list_header_hot.png" pushedimage="List/list_header_pushed.png" sepimage="List/list_header_sep.png" />
					<ListHeaderItem text="檔名稱" width="84" height="23" minwidth="16"  sepwidth="1" align="center" hotimage="List/list_header_hot.png" pushedimage="List/list_header_pushed.png" sepimage="List/list_header_sep.png" />
					</ListHeader>
					<ListLabelElement text="張三1" selected="true" />
					<ListLabelElement text="李四2" />不要這樣寫 
					否則會導致 pListElement->SetText(0,_T("test1"));pListElement->SetText(1,_T("test2"));執行失敗 切記!!!!!
					正確的做法是 將ListLabelElement 初始化去掉 在InitWindow 裡面初始化 
					</List>
					*/
					CListTextElementUI* pListElement = static_cast<CListTextElementUI*>(pList->GetItemAt(nIndex));
					if (pListElement != NULL)
					{
						LPCTSTR pstr1 = pListElement->GetText(0);
						LPCTSTR pstr2 = pListElement->GetText(1);

						pListElement->SetText(0,_T("test1"));
						pListElement->SetText(1,_T("test2"));
					}
				}
		}
		return __super::Notify(msg);
	}

 };

int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	HRESULT Hr = ::CoInitialize(NULL);
	if( FAILED(Hr) ) return 0;


	CPaintManagerUI::SetInstance(hInstance);

	CDuiFrameWnd duiFrame;
	duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	duiFrame.CenterWindow();
	duiFrame.ShowModal();
	CPaintManagerUI::MessageLoop();
	::CoUninitialize();
	return 0;
}


//xml配置如下:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="700,400" caption="0,0,0,30">
    <HorizontalLayout width="400" height="400" bkcolor="#FF0000FF" bkcolor2="#00FF00FF" bordercolor="#00808000" focusbordercolor="#00008080">
      <List name="ListDemo1" float="true" pos="30,314,0,0" width="139" height="218" bkcolor="#FFFFFFFF" itemtextcolor="#FF000000" itembkcolor="#FFE2DDDF" itemselectedtextcolor="#FF000000" itemselectedbkcolor="#FFC1E3FF" itemhottextcolor="#FF000000" itemhotbkcolor="#FFE9F5FF" itemdisabledtextcolor="#FFCCCCCC" itemdisabledbkcolor="#FFFFFFFF" >
        <ListHeader name="domain" bkimage="List/list_header_bg.png">
          <ListHeaderItem text="序號" width="40" height="23" minwidth="16"  sepwidth="1" align="center" hotimage="List/list_header_hot.png" pushedimage="List/list_header_pushed.png" sepimage="List/list_header_sep.png" />
          <ListHeaderItem text="檔名稱" width="84" height="23" minwidth="16"  sepwidth="1" align="center" hotimage="List/list_header_hot.png" pushedimage="List/list_header_pushed.png" sepimage="List/list_header_sep.png" />
        </ListHeader>
      </List>
      
    </HorizontalLayout>
</Window>