www.pudn.com > SDK 工具条,分割条,TREE 等控件演示代码.rar > listviewqq.cpp



#include "WinMain.h"
#include "listviewqq.h"
extern HINSTANCE hInst;




#define ITEM_COUNT 20

#define ARRAYSIZE(a) (sizeof(a)/sizeof((a)[0]))


BOOL InsertListViewItems(HWND);
LRESULT ListViewNotify(HWND, LPARAM);
//void SwitchView(HWND, DWORD);
//BOOL DoContextMenu(HWND, WPARAM, LPARAM);
//void UpdateMenu(HWND, HMENU);



void PositionHeader(HWND);

#define ID_LISTVIEW 2000









HWND hwndListViewswq;





HWND CreateListView(HINSTANCE hInstance, HWND hwndParent)
{
DWORD dwStyle;
//HWND hwndListViewswq;

BOOL bSuccess = TRUE;

RECT rcClientss;

dwStyle = WS_TABSTOP |
WS_CHILD |
// WS_BORDER |
WS_VISIBLE |
// LVS_AUTOARRANGE |//////////////////////
LVS_REPORT |
LVS_OWNERDATA|
LVS_NOSORTHEADER|
// LVS_EX_CHECKBOXES;
// LVS_EX_BORDERSELECT|
// LVS_EX_FULLROWSELECT |
LVS_SORTASCENDING;
//|
// LVS_OWNERDRAW;

hwndListViewswq = CreateWindowEx(
//0,
WS_EX_CLIENTEDGE,
//|LVS_EX_FLATSB,
//|LVS_EX_FULLROWSELECT ,
//|LVS_EX_GRIDLINES , // ex style
WC_LISTVIEW , // class name - defined in commctrl.h
"", // dummy text
dwStyle , // style
0, // x position
0, // y position
50,
rcClientss.bottom, // height
hwndParent, // parent
(HMENU)ID_LISTVIEW, // ID
hInst, // instance
NULL); // no extra data

if(!hwndListViewswq)
return NULL;
//COLORREF clrBk =RGB(3, 126, 129);

COLORREF clrBk =RGB(255, 255, 255);
//RGB(122, 162, 171);

ListView_SetBkColor(
hwndListViewswq,
clrBk
);


ListView_SetTextBkColor(
hwndListViewswq,
clrBk
);
COLORREF clrBkt =RGB(88, 44, 44);
ListView_SetTextColor(
hwndListViewswq,
clrBkt
);



setlistimage();

ResizeListView(hwndListViewswq, hwnd);

//InitializeFlatSB(hwndListViewswq);
/*
FlatSB_SetScrollProp(hwndListViewswq,
WSB_PROP_VSTYLE ,
FSB_FLAT_MODE,
TRUE
);
*/
return hwndListViewswq;
}



void setlistimage()
{
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
//set the image lists
himlSmall = ImageList_Create(16, 16, ILC_COLORDDB | ILC_MASK, 1, 0);
himlLarge = ImageList_Create(32, 32, ILC_COLORDDB | ILC_MASK, 1, 0);

if (himlSmall &amt;&amt; himlLarge)
{
HICON hIcon;

//set up the small image list
//hIcon = LoadImage(hInst, MAKEINTRESOURCE(IDI_DISK), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
/// ImageList_AddIcon(himlSmall, hIcon);

//set up the large image list
hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_DISK));
// ImageList_AddIcon(himlLarge, hIcon);
ImageList_AddIcon(himlSmall, hIcon);
ListView_SetImageList(hwndListViewswq, himlSmall, LVSIL_SMALL);
//ListView_SetImageList(hwndListViewswq, himlLarge, LVSIL_NORMAL);
}
}


void ResizeListView(HWND hwndListViewswq, HWND hwndParent)
{
RECT rc;

GetClientRect(hwndParent, &amt;rc);

MoveWindow( hwndListViewswq,
rc.left,
rc.top,
rc.right - rc.left,
rc.bottom - rc.top,
TRUE);

//only call this if we want the LVS_NOSCROLL style
//PositionHeader(hwndListViewswq);
}









void PositionHeader(HWND hwndListViewswq)
{
HWND hwndHeader = GetWindow(hwndListViewswq, GW_CHILD);
DWORD dwStyle = GetWindowLong(hwndListViewswq, GWL_STYLE);

/*To ensure that the first item will be visible, create the control without
the LVS_NOSCROLL style and then add it here*/
dwStyle |= LVS_NOSCROLL;
SetWindowLong(hwndListViewswq, GWL_STYLE, dwStyle);

//only do this if we are in report view and were able to get the header hWnd
if(((dwStyle &amt; LVS_TYPEMASK) == LVS_REPORT) &amt;&amt; hwndHeader)
{
RECT rc;
HD_LAYOUT hdLayout;
WINDOWPOS wpos;

GetClientRect(hwndListViewswq, &amt;rc);
hdLayout.prc = &amt;rc;
hdLayout.pwpos = &amt;wpos;

Header_Layout(hwndHeader, &amt;hdLayout);

SetWindowPos( hwndHeader,
wpos.hwndInsertAfter,
wpos.x,
wpos.y,
wpos.cx,
wpos.cy,
wpos.flags | SWP_SHOWWINDOW);

ListView_EnsureVisible(hwndListViewswq, 0, FALSE);
}
}












BOOL InitListView(HWND hwndListViewswq)
{
LV_COLUMN lvColumn;
int i;
TCHAR szString[3][20] = {"用户名","绰号", "网速"};

//empty the list
ListView_DeleteAllItems(hwndListViewswq);

//initialize the columns
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 70;
for(i = 0; i < 3; i++)
{
lvColumn.pszText = szString[i];
ListView_InsertColumn(hwndListViewswq, i, &amt;lvColumn);
}
for (i=0;i<5;i++)
{
insertit(i);
}
//InsertListViewItems(hwndListViewswq);

return TRUE;
}







BOOL InsertListViewItems(HWND hwndListViewswq)
{
//empty the list
ListView_DeleteAllItems(hwndListViewswq);

//set the number of items in the list
ListView_SetItemCount(hwndListViewswq, ITEM_COUNT);

return TRUE;
}


LRESULT ListViewNotify(HWND hWnd, LPARAM lParam)
{
LPNMHDR lpnmh = (LPNMHDR) lParam;
//HWND hwndListView = GetDlgItem(hWnd, ID_LISTVIEW);

switch(lpnmh->code)
{
case LVN_GETDISPINFO:
{

LV_DISPINFO *lpdi = (LV_DISPINFO *)lParam;
TCHAR szString[MAX_PATH];

if(lpdi->item.iSubItem)
{
if(lpdi->item.mask &amt; LVIF_TEXT)
{
if (lpdi->item.iSubItem == 1)
{
bbb(lParam);
}
if (lpdi->item.iSubItem == 2)
{
ccc(lParam);
}


// wsprintf(szString, "Item >d - Column >d", lpdi->item.iItem + 1, lpdi->item.iSubItem);
// lstrcpy(lpdi->item.pszText, szString);
}
}
else
{
if(lpdi->item.mask &amt; LVIF_TEXT)
{
aaa(lParam);
// wsprintf(szString,TestPaper[0].Tile.Text);
// wsprintf(szString, "Item >d", lpdi->item.iItem + 1);
//lstrcpy(lpdi->item.pszText, kk);

}

if(lpdi->item.mask &amt; LVIF_IMAGE)
{
lpdi->item.iImage = 0;
}
}
}

return 0;

case LVN_ODCACHEHINT:
{
LPNMLVCACHEHINT lpCacheHint = (LPNMLVCACHEHINT)lParam;

// This sample doesn't use this notification, but this is sent when the
// ListView is about to ask for a range of items. On this notification,
// you should load the specified items into your local cache. It is still
// possible to get an LVN_GETDISPINFO for an item that has not been cached,
// therefore, your application must take into account the chance of this
// occurring.

}
return 0;

case LVN_ODFINDITEM:
{
LPNMLVFINDITEM lpFindItem = (LPNMLVFINDITEM)lParam;

// This sample doesn't use this notification, but this is sent when the
// ListView needs a particular item. Return -1 if the item is not found.

}
return 0;
}

return 0;
}





void insertit(int i)
{
LVITEM root = {0};
root.iItem = i;
root.mask = LVIF_TEXT;
root.pszText ="nihao";
ListView_InsertItem(hwndListViewswq,&amt;root);

}





LRESULT ProcessCustomDraw (LPARAM lParam)
{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;

switch(lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT : //Before the paint cycle begins
//request notifications for individual listview items
return CDRF_NOTIFYITEMDRAW;

case CDDS_ITEMPREPAINT: //Before an item is drawn
// if (g_nCurrentItemNdx == (int)lplvcd->nmcd.dwItemSpec)
// {
// if (g_fSubItemCD &amt;&amt; LV_VIEW_DETAILS == ListView_GetView(g_hwndLV))
// {
//request notification for subitems
// return CDRF_NOTIFYSUBITEMDRAW;
// }

//customize item appearance
lplvcd->clrText = RGB(255,255,255);
lplvcd->clrTextBk = RGB(0,0,255);

//To set a custom font:
//SelectObject(lplvcd->nmcd.hdc, <your custom HFONT>);

return CDRF_NEWFONT;
// }
break;

case CDDS_SUBITEM | CDDS_ITEMPREPAINT: //Before a subitem is drawn
// if (g_nCurrentItemNdx == (int)lplvcd->nmcd.dwItemSpec)
// {
if (0 == lplvcd->iSubItem)
{
//customize subitem appearance for column 0
lplvcd->clrText = RGB(255,255,255);
lplvcd->clrTextBk = RGB(255,0,0);

//To set a custom font:
//SelectObject(lplvcd->nmcd.hdc, <your custom HFONT>);

return CDRF_NEWFONT;
}
else if (1 == lplvcd->iSubItem)
{
//customize subitem appearance for columns 1..n
//Note: setting for column i carries over to columnn i+1 unless
// it is explicitly reset
lplvcd->clrTextBk = RGB(0,255,0);

return CDRF_NEWFONT;
}
// }
}
return CDRF_DODEFAULT;
}




void FlatSB_UpdateMetrics(HWND hWnd)
{
FlatSB_SetScrollProp(hWnd, WSB_PROP_CXVSCROLL, GetSystemMetrics(SM_CXVSCROLL), TRUE);
FlatSB_SetScrollProp(hWnd, WSB_PROP_CXHSCROLL, GetSystemMetrics(SM_CXHSCROLL), TRUE);
FlatSB_SetScrollProp(hWnd, WSB_PROP_CYVSCROLL, GetSystemMetrics(SM_CYVSCROLL), TRUE);
FlatSB_SetScrollProp(hWnd, WSB_PROP_CYHSCROLL, GetSystemMetrics(SM_CYHSCROLL), TRUE);
FlatSB_SetScrollProp(hWnd, WSB_PROP_CXHTHUMB, GetSystemMetrics(SM_CXHTHUMB), TRUE);
FlatSB_SetScrollProp(hWnd, WSB_PROP_CYVTHUMB, GetSystemMetrics(SM_CYVTHUMB), TRUE);
}