到目前为止,还没有正儿八经的写过MFC程序,虽然现在自己把MFC的框架看得差不多,但是还是不想
使用MFC写代码,做界面MFC是关键,这是谁也无法逃脱的框架,好了,废话少说,说说自己的实现。
List Control是Windows的控件,使用的时候要加#include #pragma comment(lib, "commctl32.lib"(
这是win32平台,若是wince平台则不需要,原因现在我还不清楚) .
1.创建List Control控件
m_hList = ::CreateWindow(L"syslistview32", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT | LVS_SHOWSELALWAYS, m_pt.x, m_pt.y, m_sz.cx, m_sz.cy, _hParent, NULL, _hInst, NULL);
2.创建加列项目
LVCOLUMN _lvcol; memset(&_lvcol, 0, sizeof(LVCOLUMN)); _lvcol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; _lvcol.pszText = "sdfsdfs" _lvcol.cx = 80; ::SendMessage(m_hList, LVM_INSERTCOLUMN, 0/*第一列*/, (LPARAM)&_lvcol);
3.添加列元素
a.第一种方法
LVITEM _lvitem; memset(&_lvitem, 0, sizeof(LVITEM)); _lvitem.mask = LVIF_TEXT; _lvitem.cchTextMax = MAX_PATH; _lvitem.iItem = _iItem; _lvitem.iSubItem = _iSubItem; _lvitem.pszText = _text; ::SendMessage(m_hList, LVM_INSERTITEM, _lvitem.iItem, (LPARAM)&_lvitem);
b.第二种方法
LVITEM _lvitem; memset(&_lvitem, 0, sizeof(LVITEM)); _lvitem.mask = LVIF_TEXT; _lvitem.cchTextMax = MAX_PATH; _lvitem.iItem = _iItem; _lvitem.iSubItem = _iSubItem; _lvitem.pszText = _text; ::SendMessage(m_hList, LVM_SETITEMTEXT, _lvitem.iItem, (LPARAM)&_lvitem);
4.删除已经选择项目
_lvitem.iItem = ::SendMessage(m_hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED); if (-1 != _lvitem.iItem) { ::SendMessage(m_hList, LVM_DELETEITEM, _lvitem.iItem, 0); }
我写了类,下面是代码:
1.头文件:
/* ** KListCtrl.h ** JieYang 2010-01-10 11:05 ** KListCtrl Class, Written By Win32 API ** When You Use This Class, You Must pragma commctl32.lib First */ #ifndef __KLISTCTRL_H__ #define __KLISTCTRL_H__ #include #include #include using namespace std; typedef struct _lstele LSTELE; typedef struct _lstele* PLSTELE; struct _lstele { int _item; TCHAR _path[MAX_PATH]; PLSTELE next; }; class KListCtrl { public: KListCtrl(); KListCtrl(HINSTANCE, HWND, POINT, SIZE); ~KListCtrl(); void CreateColumn(UINT _mask, int _cx, TCHAR* _text, int _col); void SetListItem(int _iItem, int _iSubItem, TCHAR* _text); void SetListItemText(int _iItem, int _iSubItem, TCHAR* _text); void AddListPath(LSTELE _ele); PLSTELE GetListPath(); BOOL RemoveListItem(); private: HWND m_hList; POINT m_pt; SIZE m_sz; PLSTELE p_head; }; #endif
2.CPP文件
/* ** KListCtrl.cpp ** JieYang 2010-01-10 11:05 ** KListCtrl Class, Written By Win32 API ** When You Use This Class, You Must pragma commctl32.lib First */ #include "KListCtrl.h" KListCtrl::KListCtrl() {} KListCtrl::KListCtrl(HINSTANCE _hInst, HWND _hParent, POINT _pt, SIZE _sz) { InitCommonControls(); m_pt = _pt; m_sz = _sz; p_head = NULL; m_hList = ::CreateWindow(L"syslistview32", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT | LVS_SHOWSELALWAYS, m_pt.x, m_pt.y, m_sz.cx, m_sz.cy, _hParent, NULL, _hInst, NULL); if (!m_hList) { ::MessageBox(NULL, TEXT("CreateWindow Error: KListCtrl.cpp 22"), TEXT("Error"), MB_OK); return ; } } KListCtrl::~KListCtrl() {} void KListCtrl::CreateColumn(UINT _mask, int _cx, TCHAR *_text, int _col) { LVCOLUMN _lvcol; memset(&_lvcol, 0, sizeof(LVCOLUMN)); _lvcol.mask = _mask; _lvcol.pszText = _text; _lvcol.cx = _cx; ::SendMessage(m_hList, LVM_INSERTCOLUMN, _col, (LPARAM)&_lvcol); } void KListCtrl::SetListItem(int _iItem, int _iSubItem, TCHAR *_text) { LVITEM _lvitem; memset(&_lvitem, 0, sizeof(LVITEM)); _lvitem.mask = LVIF_TEXT; _lvitem.cchTextMax = MAX_PATH; _lvitem.iItem = _iItem; _lvitem.iSubItem = _iSubItem; _lvitem.pszText = _text; ::SendMessage(m_hList, LVM_INSERTITEM, _lvitem.iItem, (LPARAM)&_lvitem); } void KListCtrl::SetListItemText(int _iItem, int _iSubItem, TCHAR *_text) { LVITEM _lvitem; memset(&_lvitem, 0, sizeof(LVITEM)); _lvitem.mask = LVIF_TEXT; _lvitem.cchTextMax = MAX_PATH; _lvitem.iItem = _iItem; _lvitem.iSubItem = _iSubItem; _lvitem.pszText = _text; ::SendMessage(m_hList, LVM_SETITEMTEXT, _lvitem.iItem, (LPARAM)&_lvitem); LSTELE _ele; _ele._item = _lvitem.iItem; lstrcpy(_ele._path, _text); _ele.next = NULL; AddListPath(_ele); } void KListCtrl::AddListPath(LSTELE _ele) { PLSTELE p = NULL; PLSTELE q = NULL; if (NULL == p_head) { p_head = new LSTELE; (*p_head) = _ele; } else { q = p_head; while(q->next) { q = q->next; } p = new LSTELE; (*p) = _ele; q->next = p; } } PLSTELE KListCtrl::GetListPath() { if (p_head) { return p_head; } return NULL; } BOOL KListCtrl::RemoveListItem() { LVITEM _lvitem; PLSTELE _p = p_head; PLSTELE _q = p_head; BOOL _state = FALSE; _lvitem.iItem = ::SendMessage(m_hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED); if (-1 != _lvitem.iItem) { ::SendMessage(m_hList, LVM_DELETEITEM, _lvitem.iItem, 0); while (_p) { if (_p->_item == _lvitem.iItem) { _state = TRUE; break; } _q = _p; _p = _p->next; } if (_p == p_head) { p_head = _p->next; delete _p; _p = NULL; } else if (_p != p_head) { _q->next = _p->next; delete _p; _p = NULL; } _p = p_head; int i = 0; while (_p) { _p->_item = i ++; _p = _p->next; } } return _state; }