ListBox - 多选,插入,删除示例.

说明

ListBox多选,插入,删除示例.

ListBox_Item.xml

<?xml version="1.0" encoding="UTF-8" ?>
<xc_template_listBox>
<layoutEleUI type="item" layout.horizon="true" layout.alignH="left" layout.alignV="top" layout.width="auto" layout.height="fill" layout.space="2" layout.padding="2,2,2,2" >
<shapeText itemID="1" layout.width="auto" layout.height="fill" content="{binding field=name1 source=data bSync=false}" />
<shapeText itemID="2" layout.width="auto" layout.height="fill" content="{binding field=name2 source=data bSync=false}" />
<shapeText itemID="3" layout.width="auto" layout.height="fill" content="{binding field=name3 source=data bSync=false}" />
</layoutEleUI>
</xc_template_listBox>

示例代码

//包含炫彩界面库文件
#pragma comment(lib, "XCGUI.lib")
#include "xcgui.h"
class CMyWindowListBox
{
public:
HWINDOW m_hWindow;
HELE m_hListBox;
HELE m_hEidt;
CMyWindowListBox()
{
Init();
}
void Init()
{
m_hWindow = XWnd_Create(0, 0, 500, 400, L"炫彩界面库窗口",NULL, window_style_default);
m_hListBox=XListBox_Create(20,40,300,310,m_hWindow);
XListBox_SetItemTemplateXML(m_hListBox,L"XML-template-test\\ListBox_Item.xml");
XSView_SetLineSize(m_hListBox,20,20);
HXCGUI hAdapter=XAdTable_Create();
XListBox_BindAdapter(m_hListBox,hAdapter);
XAdTable_AddColumn(hAdapter,XC_NAME1);
XAdTable_AddColumn(hAdapter,XC_NAME2);
XAdTable_AddColumn(hAdapter,XC_NAME3);
wchar_t buf[256]={0};
int index=0;
for (int i=0;i<20;i++)
{
wsprintf(buf,L"item-%d",i);
index=XAdTable_AddItemTextEx(hAdapter,XC_NAME1,buf);
if(XC_ID_ERROR!=index)
{
wsprintf(buf,L"item-1-%d",i);
XAdTable_SetItemTextEx(hAdapter,index,(XC_NAME2),buf);
wsprintf(buf,L"item-2-%d",i);
XAdTable_SetItemTextEx(hAdapter,index,(XC_NAME3),buf);
}
}
HELE hButton1=XBtn_Create(330,40,100,18,L"插入index=1",m_hWindow);
XEle_RegEventCPP(hButton1,XE_BNCLICK,&CMyWindowListBox::OnBtnClickAdd);
HELE hButton2=XBtn_Create(330,62,100,18,L"删除index=1",m_hWindow);
XEle_RegEventCPP(hButton2,XE_BNCLICK,&CMyWindowListBox::OnBtnClickDel);
HELE hButton3=XBtn_Create(330,84,100,18,L"删除index=(2-3)",m_hWindow);
XEle_RegEventCPP(hButton3,XE_BNCLICK,&CMyWindowListBox::OnBtnClickDelEx);
m_hEidt=XEdit_Create(330,106,120,200,m_hWindow);
XSView_ShowSBarV(m_hEidt,TRUE);
XEdit_EnableMultiLine(m_hEidt,TRUE);
XEle_RegEventCPP(m_hListBox,XE_LISTBOX_TEMP_CREATE,&CMyWindowListBox::OnTemplateCreate);
XEle_RegEventCPP(m_hListBox,XE_LISTBOX_TEMP_DESTROY,&CMyWindowListBox::OnTemplateDestroy);
XEle_RegEventCPP(m_hListBox,XE_LISTBOX_SELECT,&CMyWindowListBox::OnListBoxSelect);
XWnd_ShowWindow(m_hWindow,SW_SHOW);
}
int OnBtnClickAdd(BOOL *pbHandled)
{
HXCGUI hAdapter=XListBox_GetAdapter(m_hListBox);
XAdTable_InsertItemText(hAdapter,1,L"test - insert");
XEle_Redraw(m_hListBox);
return 0;
}
int OnBtnClickDel(BOOL *pbHandled)
{
HXCGUI hAdapter=XListBox_GetAdapter(m_hListBox);
if(XAdTable_DeleteItem(hAdapter,1))
XEle_Redraw(m_hListBox);
return 0;
}
int OnBtnClickDelEx(BOOL *pbHandled)
{
HXCGUI hAdapter=XListBox_GetAdapter(m_hListBox);
if(XAdTable_DeleteItemEx(hAdapter,2,2))
XEle_Redraw(m_hListBox);
return 0;
}
int OnListBoxSelect(int iItem,BOOL *pbHandled)
{
XEdit_DeleteAll(m_hEidt);
int array_[100]={0};
int count =XListBox_GetSelectAll(m_hListBox,array_,100);
wstring szItemList;
wchar_t name[MAX_PATH]={0};
HXCGUI hAdapter=XListBox_GetAdapter(m_hListBox);
for (int i=0;i<count;i++)
{
if(XAdTable_GetItemText(hAdapter,array_[i],0,name,MAX_PATH))
{
szItemList+=name;
szItemList+=L"\n";
}
}
XEdit_SetText(m_hEidt,szItemList.c_str());
return 0;
}
int OnTemplateCreate(listBox_item_i *pItem,int nFlag,BOOL *pbHandled)
{
return 0;
}
int OnTemplateDestroy(listBox_item_i *pItem,int nFlag,BOOL *pbHandled)
{
return 0;
}
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
XInitXCGUI(TRUE);
CMyWindowListBox MyWindow;
return TRUE;
}
#define XE_BNCLICK
按钮点击事件.
Definition: XCGUI.h:939
#define XE_LISTBOX_TEMP_DESTROY
列表框元素,项模板销毁.
Definition: XCGUI.h:998
#define XE_LISTBOX_TEMP_CREATE
列表框元素-项模板创建事件, 模板复用机制需先启用; 替换模板无效判断nFlag,因为内部会检查模板是否改变,不用担心重复
Definition: XCGUI.h:987
#define XE_LISTBOX_SELECT
列表框元素,项选择事件.
Definition: XCGUI.h:1010
@ window_style_default
窗口样式-控制按钮: 居中 图标, 标题, 关闭按钮, 最大化按钮, 最小化按钮
Definition: XCGUI.h:398
HELE WINAPI XBtn_Create(int x, int y, int cx, int cy, const wchar_t *pName, HXCGUI hParent)
按钮_创建 创建按钮元素
Definition: ButtonUI.cpp:1765
void WINAPI XEdit_SetText(HELE hEle, const wchar_t *pString)
@编辑框_置文本
Definition: EditUI.cpp:6856
HELE WINAPI XEdit_Create(int x, int y, int cx, int cy, HXCGUI hParent)
@编辑框_创建 创建
Definition: EditUI.cpp:6572
void WINAPI XEdit_EnableMultiLine(HELE hEle, BOOL bEnable)
@编辑框_启用多行
Definition: EditUI.cpp:6620
void WINAPI XEle_Redraw(HELE hEle, BOOL bImmediate)
元素_重绘 重绘元素.
Definition: EleUI.cpp:4179
#define XC_ID_ERROR
ID错误
Definition: XCGUI.h:462
void WINAPI XSView_ShowSBarV(HELE hEle, BOOL bShow)
滚动视_显示垂直滚动条 显示垂直滚动条.
Definition: ScrollViewUI.cpp:836
BOOL WINAPI XSView_SetLineSize(HELE hEle, int nWidth, int nHeight)
滚动视_置滚动单位大小 设置滚动单位大小.
Definition: ScrollViewUI.cpp:694
void WINAPI XExitXCGUI()
炫彩_退出 退出界面库释放资源. 不可在dllmain()中卸载, 否则会死锁
Definition: App.cpp:572
void WINAPI XRunXCGUI()
炫彩_运行 运行消息循环,当炫彩窗口数量为0时退出.
Definition: App.cpp:564
BOOL WINAPI XInitXCGUI(BOOL bD2D)
炫彩_初始化 初始化界面库.
Definition: App.cpp:557
void * HXCGUI
资源句柄
Definition: XCGUI.h:92
XEle_RegEventCPP(hEle, nEvent, memberFunction)
元素_注册事件CPP 注册元素事件,将类成员函数作为事件响应函数.函数省略元素自身句柄hEle.
int WINAPI XAdTable_AddColumn(HXCGUI hAdapter, const wchar_t *pName)
数据适配器表_添加列 添加数据列.
Definition: DataAdapter.cpp:1908
HXCGUI WINAPI XAdTable_Create()
数据适配器表_创建 创建列表框元素数据适配器.数据适配器存储数据, UI对象根据绑定的字段名显示数据适配器中对应的数据;
Definition: DataAdapter.cpp:1863
const wchar_t *WINAPI XAdTable_GetItemText(HXCGUI hAdapter, int iRow, int iColumn)
数据适配器表_取项文本 获取项文本内容.
Definition: DataAdapter.cpp:2190
BOOL WINAPI XAdTable_SetItemTextEx(HXCGUI hAdapter, int iRow, const wchar_t *pName, const wchar_t *pValue)
数据适配器表_置项文本扩展 设置项数据.
Definition: DataAdapter.cpp:2043
void WINAPI XListBox_BindAdapter(HELE hEle, HXCGUI hAdapter)
列表框_绑定数据适配器 绑定数据适配器.
Definition: ListBoxUI.cpp:3035
int WINAPI XListBox_GetSelectAll(HELE hEle, out_buffer_ int *pArray, int nArraySize)
列表框_取全部选择 获取所有选择项.
Definition: ListBoxUI.cpp:2809
HELE WINAPI XListBox_Create(int x, int y, int cx, int cy, HXCGUI hParent)
列表框_创建 创建列表框元素.
Definition: ListBoxUI.cpp:2562
BOOL WINAPI XListBox_SetItemTemplateXML(HELE hEle, const wchar_t *pXmlFile)
列表框_置项模板文件 设置列表项模板文件.
Definition: ListBoxUI.cpp:2935
HXCGUI WINAPI XListBox_GetAdapter(HELE hEle)
列表框_取数据适配器 获取绑定的数据适配器.
Definition: ListBoxUI.cpp:3045
HWINDOW WINAPI XWnd_Create(int x, int y, int cx, int cy, const wchar_t *pTitle, HWND hWndParent, int XCStyle)
窗口_创建 创建窗口
Definition: WindowUI.cpp:5964
BOOL WINAPI XWnd_ShowWindow(HWINDOW hWindow, int nCmdShow)
@窗口_显示扩展 显示隐藏及控制窗口最大化还原等
Definition: WindowUI.cpp:6387

程序截图