1。先來(lái)介紹REPORT類(lèi)型的CListCtrl: 首先使用下面的語(yǔ)句設(shè)置CListCtrl的style: DWORD SetExtendedStyle( DWORD dwNewStyle ); 其中 LVS_EX_CHECKBOXES 表示添加CheckBox LVS_EX_FULLROWSELECT 表示選擇整行 LVS_EX_GRIDLINES 表示添加表格線(xiàn)
如果設(shè)置了LVS_EX_CHECKBOXES屬性,則可以用 BOOL GetCheck( int nItem ) const; 來(lái)得到某一行是否Checked。
可以先用下面的語(yǔ)句來(lái)刪除以前的東西: for(int k=2;k>=0;k--) //注意要從后往前刪,否則出錯(cuò) m_ListCtrl.DeleteColumn(k); m_ListCtrl.DeleteAllItems();
用下面的語(yǔ)句新建列: m_ListCtrl.InsertColumn(0,_T("文件名"),LVCFMT_IMAGE|LVCFMT_LEFT); m_ListCtrl.InsertColumn(1,_T("儀器類(lèi)別")); m_ListCtrl.InsertColumn(2,_T("項(xiàng)目類(lèi)別")); 其中LVCFMT_IMAGE表示可以在第一列加入圖標(biāo)。如果不要圖標(biāo)可以刪去。
然后設(shè)置列寬: for(j=0;j<3;j++) m_ListCtrl.SetColumnWidth(j ,100); 以下為列表加入圖標(biāo),如果不需要圖標(biāo),可以跳過(guò)這一步。注意只在第一次加入,如果多次加入會(huì)出錯(cuò)! 先在頭文件中加入聲明: CImageList m_ImageList; 這是必要的,如果在cpp的某個(gè)函數(shù)中加入由于生命期結(jié)束,CImageList自動(dòng)釋放,則效果是列表中看不到圖標(biāo),只看到一個(gè)白方塊。 下面生成CImageList,并將其綁定到CListCtrl中,這是CImageList中還沒(méi)有圖標(biāo),只是一個(gè)容器: static int flag=2; if(flag==2){//只調(diào)用一次SetImageList,否則出錯(cuò) m_ImageList.Create(128, 128, ILC_COLORDDB|ILC_MASK, 20, 1); m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL); } flag=(flag+1)%2; 如果CListCtrl已經(jīng)用過(guò),曾經(jīng)加過(guò)圖標(biāo)進(jìn)去,這時(shí)就要?jiǎng)h除上次放進(jìn)m_ImageList中的Image for(int kk=0;kk<m_ImageList.GetImageCount();kk++) m_ImageList.Remove(k); 下面介紹如何向CListCtrl里面加入行,并同時(shí)為每一行動(dòng)態(tài)加入圖標(biāo): 假設(shè)m_listRowCount為要加入的行數(shù)。 CBitmap* bitmap; bitmap=new CBitmap[m_list1rowCount]; HBITMAP hbitmap; for(int i = 0; i < m_listRowCount; i++) { //為每一行插入相應(yīng)的縮略圖 CFile f; CFileException e; if( !f.Open(m_FileName, CFile::modeRead, &e )){ //m_FileName為bmp文件名,由你來(lái)定 hbitmap = (HBITMAP)LoadImage(NULL,path+"blank.bmp",IMAGE_BITMAP,0,0, LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE); }else{ f.Close(); hbitmap = (HBITMAP)LoadImage(NULL,bmpFile,IMAGE_BITMAP,0,0, LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE); } bitmap[i].Attach(hbitmap); m_ImageList.Add(&bitmap[i], RGB(0, 128, 128)); //插入行 m_ListCtrl.InsertItem(i,m_FileName,i); m_ListCtrl.SetItemText(i,1,type); m_ListCtrl.SetItemText(i,2,m_Path); } //記得刪除已經(jīng)沒(méi)用的臨時(shí)文件 if(m_list1rowCount!=0) delete[] bitmap;
2。如果是ICON類(lèi)型的CListCtrl,則要做一點(diǎn)點(diǎn)改動(dòng): 把綁定圖標(biāo)集的代碼由 SetImageList(&m_ImageList,LVSIL_SMALL); 改為 SetImageList(&m_ImageList,LVSIL_NORMAL);
插入行時(shí)只用 InsertItem(i,mainSet.m_FileName,i); 不用 SetItemText(i,1,type); 之類(lèi)的代碼。
|
設(shè)置報(bào)表的樣式
選中一整行:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_FULLROWSELECT);
繪制表格:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_GRIDLINES);
帶復(fù)選框:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_CHECKBOXES);
自動(dòng)切換:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_TRACKSELECT);
選定一行:
設(shè)置CListCtrl的Show selection always選項(xiàng)
SetItemState (iIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED)
選中一個(gè)或多個(gè)項(xiàng)目時(shí),會(huì)發(fā)送LVN_ITEMCHANGED消息,可以使用
GetSelectedCount()方法得到被選定的項(xiàng)的數(shù)目。
點(diǎn)擊列頭的消息響應(yīng):
ON_NOTIFY(HDN_ITEMCLICKW, 0, ResponseFunc)
消息,需要自己添加
或者:
ON_NOTIFY(LVN_COLUMNCLICK, ID_yourCtrl, ResponseFunc)//向?qū)砑?br>前者后響應(yīng),后者先響應(yīng)
響應(yīng)函數(shù):
ResponseFunc(NMHDR *pNMHDR, LRESULT *pResult)
雙擊CListCtrl中的ITEM的消息是及消息函數(shù):
ON_NOTIFY(NM_DBLCLK, ID_yourCtrl, ResponseFunc)
單擊ITEM的消息響應(yīng):
ON_NOTIFY(NM_CLICK, ID_yourCtrl, ResponseFunc)
ResponseFunc(NMHDR *pNMHDR, LRESULT *pResult)
HDN_ITEMCLICK 就是Header control Notify message for mouse left click on the Header control!
而HDN_ITEMCLICK是當(dāng)List View中存在一個(gè)Header Contrl時(shí),Header Ctrl通知父窗口List View的!
CListCtrl中的Item被選中觸發(fā)LBN_SELCHANGE(通過(guò)WM_COMMAND)消息!
刪除CListCtrl中選定的項(xiàng):
POSITION pos;
int nIndex;
for(; pos= GetFirstSelectedItemPosition();)
{
nIndex = GetNextSelectedItem(pos);
DeleteItem(nIndex);
}
在ListCtrl中進(jìn)行排序
列表控件(CListCtrl)的頂部有一排按鈕,用戶(hù)可以通過(guò)選擇不同的列來(lái)對(duì)記錄進(jìn)行排序。但是 CListCtrl并沒(méi)有自動(dòng)排序的功能,我們需要自己添加一個(gè)用于排序的回調(diào)函數(shù)來(lái)比較兩個(gè)數(shù)據(jù)的大小,此外還需要響應(yīng)排序按鈕被點(diǎn)擊的消息。下面講述一下具體的做法。
CListCtrl提供了用于排序的函數(shù),函數(shù)原型為:BOOL CListCtrl::SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData )。其中第一個(gè)參數(shù)為全局排序函數(shù)的地址,第二個(gè)參數(shù)為用戶(hù)數(shù)據(jù),你可以根據(jù)你的需要傳遞一個(gè)數(shù)據(jù)或是指針。該函數(shù)返回-1代表第一項(xiàng)排應(yīng)在第二項(xiàng)前面,返回1代表第一項(xiàng)排應(yīng)在第二項(xiàng)后面,返回0代表兩項(xiàng)相等。
用于排序的函數(shù)原形為:int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort),其中第三個(gè)參數(shù)為調(diào)用者傳遞的數(shù)據(jù)(即調(diào)用SortItems時(shí)的第二個(gè)參數(shù)dwData)。第一和第二個(gè)參數(shù)為用于比較的兩項(xiàng)的ItemData,你可以通過(guò)DWORD CListCtrl::GetItemData( int nItem )/BOOL CListCtrl::SetItemData( int nItem, DWORD dwData )來(lái)對(duì)每一項(xiàng)的ItemData進(jìn)行存取。在添加項(xiàng)時(shí)選用特定的CListCtrl::InsertItem也可以設(shè)置該值。由于你在排序時(shí)只能通過(guò)該值來(lái)確定項(xiàng)的位置所以你應(yīng)該比較明確的確定該值的含義。
最后一點(diǎn),我們需要知道什么時(shí)候需要排序,實(shí)現(xiàn)這點(diǎn)可以在父窗口中對(duì)LVN_COLUMNCLICK消息進(jìn)行處理來(lái)實(shí)現(xiàn)。
下面我們看一個(gè)例子,這個(gè)例子是一個(gè)派生類(lèi),并支持順序/倒序兩種方式排序。為了簡(jiǎn)單我對(duì)全局?jǐn)?shù)據(jù)進(jìn)行排序,而在實(shí)際應(yīng)用中會(huì)有多組需要排序的數(shù)據(jù),所以需要通過(guò)傳遞參數(shù)的方式來(lái)告訴派序函數(shù)需要對(duì)什么數(shù)據(jù)進(jìn)行排序。
//全局?jǐn)?shù)據(jù)
struct DEMO_DATA
{
char szName[20];
int iAge;
}strAllData[5]={{"王某",30},{"張某",40},{"武某",32},{"陳某",20},{"李某",36}};
//CListCtrl派生類(lèi)定義
class CSortList : public CListCtrl
{
// Construction
public:
CSortList();
BOOL m_fAsc;//是否順序排序
int m_nSortedCol;//當(dāng)前排序的列
protected:
//{{AFX_MSG(CSortList)
//}}AFX_MSG
...
};
//父窗口中包含該CListCtrl派生類(lèi)對(duì)象
class CSort_in_list_ctrlDlg : public CDialog
{
// Construction
public:
CSort_in_list_ctrlDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CSort_in_list_ctrlDlg)
enum { IDD = IDD_SORT_IN_LIST_CTRL_DIALOG };
CSortList m_listTest;
//}}AFX_DATA
}
//在父窗口中定義LVN_COLUMNCLICK消息映射
BEGIN_MESSAGE_MAP(CSort_in_list_ctrlDlg, CDialog)
//{{AFX_MSG_MAP(CSort_in_list_ctrlDlg)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//初始化數(shù)據(jù)
BOOL CSort_in_list_ctrlDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//初始化ListCtrl中數(shù)據(jù)列表
m_listTest.InsertColumn(0,"姓名");
m_listTest.InsertColumn(1,"年齡");
m_listTest.SetColumnWidth(0,80);
m_listTest.SetColumnWidth(1,80);
for(int i=0;i<5;i++)
{
m_listTest.InsertItem(i,strAllData[i].szName);
char szAge[10];
sprintf(szAge,"%d",strAllData[i].iAge);
m_listTest.SetItemText(i,1,szAge);
//設(shè)置每項(xiàng)的ItemData為數(shù)組中數(shù)據(jù)的索引
//在排序函數(shù)中通過(guò)該ItemData來(lái)確定數(shù)據(jù)
m_listTest.SetItemData(i,i);
}
return TRUE; // return TRUE unless you set the focus to a control
}
//處理消息
void CSort_in_list_ctrlDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
//設(shè)置排序方式
if( pNMListView->iSubItem == m_listTest.m_nSortedCol )
m_listTest.m_fAsc = !m_listTest.m_fAsc;
else
{
m_listTest.m_fAsc = TRUE;
m_listTest.m_nSortedCol = pNMListView->iSubItem;
}
//調(diào)用排序函數(shù)
m_listTest.SortItems( ListCompare, (DWORD)&m_listTest );
*pResult = 0;
}
//排序函數(shù)實(shí)現(xiàn)
int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
//通過(guò)傳遞的參數(shù)來(lái)得到CSortList對(duì)象指針,從而得到排序方式
CSortList* pV=(CSortList*)lParamSort;
//通過(guò)ItemData來(lái)確定數(shù)據(jù)
DEMO_DATA* pInfo1=strAllData+lParam1;
DEMO_DATA* pInfo2=strAllData+lParam2;
CString szComp1,szComp2;
int iCompRes;
switch(pV->m_nSortedCol)
{
case(0):
//以第一列為根據(jù)排序
szComp1=pInfo1->szName;
szComp2=pInfo2->szName;
iCompRes=szComp1.Compare(szComp2);
break;
case(1):
//以第二列為根據(jù)排序
if(pInfo1->iAge == pInfo2->iAge)
iCompRes = 0;
else
iCompRes=(pInfo1->iAge < pInfo2->iAge)?-1:1;
break;
default:
ASSERT(0);
break;
}
//根據(jù)當(dāng)前的排序方式進(jìn)行調(diào)整
if(pV->m_fAsc)
return iCompRes;
else
return iCompRes*-1;
}
排序最快:
CListCtrl::SortItems
Example
// Sort the item in reverse alphabetical order.
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
// The lParam of an item is just its index.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
return strcmp(strItem2, strItem1);
}
void snip_CListCtrl_SortItems()
{
// The pointer to my list view control.
extern CListCtrl* pmyListCtrl;
// Sort the list view items using my callback procedure.
pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}
If you don’t want to allow the users to sort the list by clicking on the header, you can use the style LVS_NOSORTHEADER. However, if you do want to allow sorting, you do not specify the LVS_NOSORTHEADER. The control, though, does not sort the items. You have to handle the HDN_ITEMCLICK notification from the header control and process it appropriately. In the code below, we have used the sorting function SortTextItems() developed in a previous section. You may choose to sort the items in a different manner.
Step 1: Add two member variables
Add two member variables to the CListCtrl. The first variable to track which column has been sorted on, if any. The second variable to track if the sort is ascending or descending.
int nSortedCol;
BOOL bSortAscending;
Step 2: Initialize them in the constructor.
Initialize nSortedCol to -1 to indicate that no column has been sorted on. If the list is initially sorted, then this variable should reflect that.
nSortedCol = -1;
bSortAscending = TRUE;
Step 3: Add entry in message map to handle HDN_ITEMCLICK
Actually you need to add two entries. For HDN_ITEMCLICKA and HDN_ITEMCLICKW. Do not use the class wizard to add the entry. For one, you need to add two entries whereas the class wizard will allow you only one. Secondly, the class wizard uses the wrong macro in the entry. It uses ON_NOTIFY_REFLECT() instead of ON_NOTIFY(). Since the HDN_ITEMCLICK is a notification from the header control to the list view control, it is a direct notification and not a reflected one.
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
Note that we specify the same function for both the notification. Actually the program will receive one or the other and not both. What notification it receives will depend on the OS. The list view control on Windows 95 will send the ANSI version and the control on NT will send the UNICODE version.
Also, note that the second argument is zero. This value filters for the id of the control and we know that header control id is zero.
Step 4: Write the OnHeaderClicked() function
Here’s where you decide what to do when the user clicks on a column header. The expected behaviour is to sort the list based on the values of the items in that column. In this function we have used the SortTextItems() function developed in a previous section. If any of the columns displays numeric or date values, then you would have to provide custom sorting for them.
void CMyListCtrl::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
if( phdn->iButton == 0 )
{
// User clicked on header using left mouse button
if( phdn->iItem == nSortedCol )
bSortAscending = !bSortAscending;
else
bSortAscending = TRUE;
nSortedCol = phdn->iItem;
SortTextItems( nSortedCol, bSortAscending );
}
*pResult = 0;
}
讓CListCtrl的SubItem也具有編輯功能:
要重載一個(gè)文本框,然后在LVN_BEGINLABELEDIT時(shí)改變文本框位置。
CInEdit m_InEdit;
if( ( GetStyle() & LVS_TYPEMASK ) == LVS_REPORT && ( m_nEditSubItem != 0 ) )
{
HWND hwndEdit;
CRect rtBound;
CString strText;
hwndEdit = (HWND)SendMessage( LVM_GETEDITCONTROL );
GetSubItemRect( pDispInfo->item.iItem, m_nEditSubItem, LVIR_LABEL, rtBound );
m_InEdit.SubclassWindow( hwndEdit );
m_InEdit.m_left = rtBound.left;
strText = GetItemText( pDispInfo->item.iItem, m_nEditSubItem );
m_InEdit.SetWindowText( strText );
}
void CInEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CRect rtClient;
lpwndpos->x = m_left; // m_left在LVN_BEGINLABELEDIT中設(shè)置
CEdit::OnWindowPosChanging(lpwndpos);
// TODO: Add your message handler code here
}
posted on 2008-06-14 02:48
幽幽 閱讀(6568)
評(píng)論(0) 編輯 收藏 引用