#ifndef _MCONTROLS_HPP_
#define _MCONTROLS_HPP_
#include <windows.h>
#include <commctrl.h>
#include <iostream>
#include <string>
using namespace std;
// ListView
int lv_GetSelectedItem( HWND hwnd )
{
for (int i = ListView_GetItemCount(hwnd); i>=0; i--)
if ( ListView_GetItemState(hwnd, i, LVIS_SELECTED) == LVIS_SELECTED )
return i;
return -1;
}
int lv_InsertColum( HWND hList,
LPSTR szColumnName,
int iSubItem,
int iWidth,
UINT uiMask,
int iFormat )
{
LV_COLUMN lvc;
memset(&lvc, 0, sizeof(lvc));
lvc.mask = uiMask;
lvc.fmt = iFormat;
lvc.cx = iWidth;
lvc.iSubItem = iSubItem;
lvc.pszText = szColumnName;
return ListView_InsertColumn(hList, iSubItem, &lvc);
}
int lv_addItem( HWND hwndList, const char *wsCaption )
{
LV_ITEM lvi;
memset(&lvi, 0, sizeof(lvi));
int iIndex = ListView_GetItemCount(hwndList);
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.cchTextMax = 40;
lvi.iItem = iIndex;
//lvi.iImage = ImageList_GetImageCount(hImageList)-1;
lvi.iSubItem = 0;
lvi.pszText = LPSTR(wsCaption);
ListView_InsertItem(hwndList, &lvi);
return iIndex;
}
bool lv_addSubItem( HWND hwndList,
int iIndex,
int iSubItem,
const char *wsText )
{
if (iIndex >= 0 && iIndex < ListView_GetItemCount(hwndList))
{
ListView_SetItemText(hwndList, iIndex, iSubItem, (LPSTR)wsText);
}
else
{
return false;
}
return true;
}
std::string lv_getItemCaption( HWND hwndList, int iIndex )
{
std::string wsCaption;
if ( iIndex > 0 && iIndex < ListView_GetItemCount(hwndList) )
{
char szCaption[256] = {0};
ListView_GetItemText(hwndList, iIndex, 0, szCaption, 256);
wsCaption = szCaption;
}
return wsCaption;
}
std::string lv_getSubItemText( HWND hwndList, int iIndex, int iSubItem )
{
std::string wsText;
if ( iIndex > 0 && iIndex < ListView_GetItemCount(hwndList) )
{
char szText[256] = {0};
ListView_GetItemText(hwndList, iIndex, iSubItem, szText, 256);
wsText = szText;
}
return wsText;
}
int lv_findItemByCaption( HWND hwndList, const string & wsCaption )
{
for ( int i=0; i<ListView_GetItemCount(hwndList); i++ )
if ( lv_getItemCaption(hwndList, i) == wsCaption )
return i;
return -1;
}
int lv_findItem( HWND hwndList, int iSubItem, const string & wsString )
{
for ( int i = 0; i<ListView_GetItemCount(hwndList); i++ )
if ( lv_getSubItemText(hwndList, i, iSubItem) == wsString )
return i;
return -1;
}
#endif // _MCONTROLS_HPP_