sysListView是win32編程裡面的一個控制項,用於列表展示
基本介紹
- 中文名:sysListView
- 外文名:sysListView
使用說明,前提,開始,定義列,改變項,完整示例,
使用說明
前提
為使用ListView控制項,需要初始化公共控制項庫,需要在程式剛剛啟動時調用
InitCommonControls() 函式,如果發生連結錯誤,說明沒有連結擁有該函式的庫檔案,對應的
頭和庫 DLL分別為 #include <commctrl.h> comctl32.lib comctl32.dll
開始
調用CreateWindowEx函式來創建一個列表控制項,指定它的類名為SysListView32。還可以在此處指定控制項初次顯示時的方式。除了用公共風格WS_***外還可以用LVS_***是SysListView32的獨有風格。
定義列
增加列需要用列的結構LVCOLUMN,定義如下:
typedef struct _LVCOLUMN {
UINT mask; //定義該列可使用的類型
int fmt; //定義列格式
int cx; //定義列寬度(以像素為單位)
LPTSTR pszText; //列內容
int cchTextMax; //列內容最大長度
int iSubItem; //定義此列序號
#if (_WIN32_IE >= 0x0300)
int iImage; //指向圖像
int iOrder; //零基列偏移量。列偏移量是從左到右的順序。例如0列在最左邊。
#endif #if (_WIN32_WINNT >= 0x0600)
int cxMin; //指定列寬最小值
int cxDefault; //默認列寬
int cxIdeal; //唯讀,作為列寬的理想寬度自動調整寬度大小
#endif
} LVCOLUMN, *LPLVCOLUMN; //結構名稱
使用方法為SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn)
還有LVM_GETCOLUMN LVM_SETCOLUMN LVM_DELETECOLUMN
改變項
改變項需要結構體LVITEM,定義如下:
typedef struct {
UINT mask; //設定成員標誌,如格式等
int iItem; //項目索引
int iSubItem; //添加到的列序
UINT state; //指明項目狀態,如圖片,疊加圖片
UINT stateMask;//指state成員的有效位
LPTSTR pszText;//項目內容
int cchTextMax;//項目最大長度
int iImage; //指向圖片索引
LPARAM lParam;//特殊值,如果你用SORTITEM它就需要這個,或者別的特別的
#if (_WIN32_IE >= 0x0300)
int iIndent; //圖片寬度
#endif
#if (_WIN32_WINNT >= 0x0501)
int iGroupId; //指向所在的組id
UINT cColumns;//需要展示的列數,最大為20
UINT puColumns; //返回這個項目所指向的列
#endif
#if (_WIN32_WINNT >= 0x0600)
int piColFmt; //返回列屬性
int iGroup; //項目列
#endif
} LVITEM, *LPLVITEM;
使用方法:SendMessage(hres, LVM_INSERTITEM, 0, (LPARAM)&item);
完整示例
void InsertColumn(void)
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = "學號";
lvc. cx = 60;
SendMessage(hList, LVM_INSERTCOLUMN, 0, (long)&lvc);
lvc.pszText = "姓名";
lvc. cx = 80;
SendMessage(hList, LVM_INSERTCOLUMN, 1, (long)&lvc);
lvc.pszText = "性別";
lvc .cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 2, (long)&lvc);
lvc.pszText = "年齡";
lvc. cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 3, (long)&lvc);
lvc.pszText = "出生日期";
lvc. cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 4, (long)&lvc);
lvc.pszText = "籍貫";
lvc. cx = 150;
SendMessage(hList, LVM_INSERTCOLUMN, 5, (long)&lvc);
lvc.pszText = "入學時間";
lvc. cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 6, (long)&lvc);
lvc.pszText = "數學";
lvc. cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 7, (long)&lvc);
lvc.pszText = "英語";
lvc. cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 8, (long)&lvc);
lvc.pszText = "政治";
lvc. cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 9, (long)&lvc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;
HMENU hMenu;
LV_ITEM lvi;
int i;
switch (message) {
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam) -> hInstance;
hMenu = GetMenu(hwnd);
InitCommonControls();
hList = CreateWindowEx(NULL, TEXT("SysListView32"), NULL, LVS_REPORT | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, NULL, hInst, NULL);
InsertColumn();
SendMessage(hList, LVM_SETTEXTCOLOR, 0, #ffffff);
SendMessage(hList, LVM_SETBKCOLOR, 0, #646464);
SendMessage(hList, LVM_SETTEXTBKCOLOR, 0, #000000);
Refresh();
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDREFRESH:
Refresh();
break;
case IDINSERT:
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, InsertDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDEDIT:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "請選擇一條記錄!", "Edit Info", MB_OK | MB_ICONWARNING);
break;
}
lvi.mask = LVIF_TEXT;
lvi.cchTextMax = 20;
for (i = 0; i < 10; ++i) {
lvi.iSubItem = i;
lvi.pszText = buf[i];
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
}
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, EditDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDDELETE:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "請選擇一條記錄!", "Delete Info", MB_OK | MB_ICONWARNING);
break;
}
SendMessage(hList, LVM_DELETEITEM, lvi.iItem, 0);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 0;
lvi.pszText = buf[0];
lvi.cchTextMax = 20;
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
sprintf(query, "DELETE FROM student WHERE id = %s", buf[0]);
ODBCConnect(hwnd);
ODBCQuery(query);
ODBCQueryEnd();
ODBCDisconnect(hwnd);
break;
case IDEXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case IDABOUT:
MessageBox(hwnd, "Students Info Administration System Developed by anonymous", "About", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_SIZE:
MoveWindow(hList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}