Thursday, 26 August 2010

List View Item Control.

Here is a sample listing of code that will add bitmap IDs(win32 application) or ICON Ids to the list view control. few month back I made this to add a feature to my existing application module.

You cannot blindly add this to ur workspace since it need some modifications. Treat this as a code snippet or an example to make the target.!! Happy coding

/**

     class/structure:     SListControl

author: renjith



creation: 25-march-2008



Modification History

---------------------------------------------------------------------------------

Date comments Reason



---------------------------------------------------------------------------------

27-Mar-08 Added ne method AddBmpImage() which To add this class in

takes a vector of Ids and Names as Input. main dialog of CADPartDB





16-Jun-08 Added new methods SetProductName() and Support for new fron-end

GetProductName() to support the new front interface (insert/update

end Interface the contents of DB)





---------------------------------------------------------------------------------

**/




#pragma once



#include

#include "../resource.h"

#include "../stdafx.h"





struct SListControl

{





private:

LV_ITEM lvi;

LV_COLUMN lvc;



std::vector<_str> m_strProdName;





public:



//int AddString ( LPCTSTR str ) { return ::SendMessage( handle, LB_ADDSTRING, 0, (LPARAM)str ); };



int GetCurSel();

void SetProductName(const std::vector<_str> & strVecName);



std::vector<_str> GetProductName();



void AddBmpImage(std::vector<int> nVecIDs,std::vector<_str> strVecName);

bool AddBmpImage(int nIDStart,int nIDEnd, _str strName);



bool AddIconImage(int nIDStart,int nIDEnd, _str strName);

};

















/**

class/structure: SListControl

author: renjith



creation: 25-march-2008



Modification History

---------------------------------------------------------------------------------

Date comments Reason



---------------------------------------------------------------------------------

27-Mar-08 Added ne method AddBmpImage() which To add this class in

takes a vector of Ids and Names as Input. main dialog of CADPartDB





16-Jun-08 Added new methods SetProductName() and Support for new fron-end

GetProductName() to support the new front interface (insert/update

end Interface the contents of DB)





---------------------------------------------------------------------------------

**/








#include "guicontrolenhanced.h"

#include "StdAfx.h"



#include



#include "../resource.h"

/*



#include


#include


using namespace std;*/








void SListControl::SetProductName(const std::vector<_str> & strVecName)



{

m_strProdName = strVecName;

}



std::vector<_str> SListControl::GetProductName()

{

return m_strProdName;



}



void SListControl::AddBmpImage(std::vector<int> nVecIDs,std::vector<_str> strVecName)

{

int nMAX = nVecIDs.size();



HWND hList = handle;

RECT Rect;

HIMAGELIST himlLarge;



SetProductName(strVecName);



//LV_ITEM lvi;

//LV_COLUMN lvc;



int iStatus;

int i;

char szStr[ 32 ];



//Registers and initializes certain common control window classes.



InitCommonControls();



if( ! hList )

MessageBox(NULL,
"hList is Null","",MB_OK);





// Create an Image list, of 80x80 pixels

himlLarge = ImageList_Create(80,80,ILC_COLOR32,100,100);

//4th argument is the number of images that the image list initially contains.

//5th argument is the the number of images by which the image list can grow when

//the system needs to make room for new images.





for( i = 0; i < color="#008000">// Add Images to each image list

{

HBITMAP hBitMap;
// bitmap handler



int imlindex=-1;



hBitMap=LoadBitmap(g_hModule,MAKEINTRESOURCE(nVecIDs[i]));

if(!hBitMap)

MessageBox(NULL,
"hBitMap is null","Msg",MB_OK);





imlindex = ImageList_Add(himlLarge,hBitMap,NULL);

}



// Make the listview use the images lists and exit



//ListView_SetImageList( hList, himlLarge, LVSIL_NORMAL);



// this is # defined in the header as...

SendMessage(hList, LVM_SETIMAGELIST, (WPARAM)(LVSIL_NORMAL), (LPARAM)(HIMAGELIST)(himlLarge));



GetClientRect( hList, &Rect );
// Get the listview RECT size



lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;



lvc.fmt = LVCFMT_LEFT;

lvc.cx = Rect.right - Rect.left;
// Make bar width of window

lvc.pszText =
"Item Status";

lvc.iSubItem = 0;
// Add display column (for Report View)



//iStatus = ListView_InsertColumn( hList, 0, &lvc );

iStatus = SendMessage((hList), LVM_INSERTCOLUMN, (WPARAM)(
int)(0), (LPARAM)(const LV_COLUMN *)(&lvc));





lvi.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT | LVIF_STATE;

lvi.state = 0;

lvi.stateMask = LVIS_FOCUSED;
// Set generic structure values

lvi.pszText = szStr;



for( i = 0; i < color="#008000">// Add 20 items to the control



{



wsprintf( szStr,
"%s",strVecName[i].c_str() ); // Name depends on number

lvi.iImage = i ;
// 5 images



lvi.iItem = i;

lvi.iSubItem = 0;

lvi.lParam = 1;

//iStatus = ListView_InsertItem( hList, &lvi ); // Add item to control



iStatus = ::SendMessage((hList), LVM_INSERTITEM, 0, (LPARAM)(
const LV_ITEM *)( &lvi));

}

}



bool SListControl::AddBmpImage(int nIDStart,int nIDEnd, _str strName)



{

int nMAX = nIDEnd - nIDStart;

HWND hList = handle;

RECT Rect;

HIMAGELIST himlLarge;

// HIMAGELIST himlSmall;





//LV_ITEM lvi;

//LV_COLUMN lvc;

int iStatus;

int i;



char szStr[ 32 ];





InitCommonControls();



if( ! hList )

MessageBox(NULL,
"hList is Null","",MB_OK);



// Create two lists, one for 32x32 and another for 16x16 icons



himlLarge = ImageList_Create(80,80,ILC_COLOR32,100,100);
//100, 100, ILC_COLOR32, 0, 10



for( i = 0; i <= nMAX; i++ ) // Add icons to each image list



{

HBITMAP hBitMap;
// bitmap handler

int imlindex=-1;





hBitMap=LoadBitmap(g_hModule,MAKEINTRESOURCE(nIDStart+i));

if(!hBitMap)

MessageBox(NULL,
"hBitMap is null","Msg",MB_OK);





imlindex = ImageList_Add(himlLarge,hBitMap,NULL);

}



// Make the listview use the images lists and exit



//ListView_SetImageList( hList, himlLarge, LVSIL_NORMAL);



SendMessage(hList, LVM_SETIMAGELIST, (WPARAM)(LVSIL_NORMAL), (LPARAM)(HIMAGELIST)(himlLarge));



GetClientRect( hList, &Rect );
// Get the listview RECT size



lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

lvc.fmt = LVCFMT_LEFT;



lvc.cx = Rect.right - Rect.left;
// Make bar width of window

lvc.pszText =
"Item Status";

lvc.iSubItem = 0;
// Add display column (for Report View)

//iStatus = ListView_InsertColumn( hList, 0, &lvc );



iStatus = SendMessage((hList), LVM_INSERTCOLUMN, (WPARAM)(
int)(0), (LPARAM)(const LV_COLUMN *)(&lvc));



lvi.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT | LVIF_STATE;

lvi.state = 0;



lvi.stateMask = LVIS_FOCUSED;
// Set generic structure values

lvi.pszText = szStr;



for( i = 0; i <= nMAX; i++ ) // Add 20 items to the control

{





wsprintf( szStr,
"%s #%d",strName.c_str(), i + 1 ); // Name depends on number

lvi.iImage = i ;
// 5 images



lvi.iItem = i;

lvi.iSubItem = 0;

lvi.lParam = 1;

//iStatus = ListView_InsertItem( hList, &lvi ); // Add item to control



iStatus = ::SendMessage((hList), LVM_INSERTITEM, 0, (LPARAM)(
const LV_ITEM *)( &lvi));

}

// SendMessage(hList,LVM_DELETEITEM,3,0);



return true;



}



bool SListControl::AddIconImage(int nIDStart,int nIDEnd, _str strName)

{

int nMAX = nIDEnd - nIDStart;



HWND hList = handle;

RECT Rect;

HICON hiconItem;

HIMAGELIST himlLarge;

//LV_ITEM lvi;

//LV_COLUMN lvc;



int iStatus;

char szStr[ 32 ];

int i;



InitCommonControls();





if( ! hList )

MessageBox(NULL,
"hList is Null","",MB_OK);

// Create lists, for 32x32 icons



himlLarge = ImageList_Create( GetSystemMetrics( SM_CXICON ),

GetSystemMetrics( SM_CYICON ),

TRUE, 1, 1 );





for( i = 0; i < color="#008000">// Add icons to each image list

{





hiconItem = (HICON)LoadImage( g_hModule, MAKEINTRESOURCE( 1000+i ),IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR );

if(!hiconItem)

MessageBox(NULL,
"hiconItem is Null","",MB_OK);





ImageList_AddIcon( himlLarge, hiconItem );

DeleteObject( hiconItem );

}





// Make the listview use the images lists and exit

//ListView_SetImageList( hList, himlLarge, LVSIL_NORMAL);

SendMessage(hList, LVM_SETIMAGELIST, (WPARAM)(LVSIL_NORMAL), (LPARAM)(HIMAGELIST)(himlLarge));



GetClientRect( hList, &Rect );
// Get the listview RECT size





lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

lvc.fmt = LVCFMT_LEFT;

lvc.cx = Rect.right - Rect.left;
// Make bar width of window

lvc.pszText =
"Item Status";



lvc.iSubItem = 0;
// Add display column (for Report View)

//iStatus = ListView_InsertColumn( hList, 0, &lvc );

iStatus = SendMessage((hList), LVM_INSERTCOLUMN, (WPARAM)(
int)(0), (LPARAM)(const LV_COLUMN *)(&lvc));







lvi.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT | LVIF_STATE;

lvi.state = 0;

lvi.stateMask = LVIS_FOCUSED;
// Set generic structure values

lvi.pszText = szStr;



for( i = 0; i < color="#008000">// Add 20 items to the control



{

wsprintf( szStr,
"%s #%d",strName.c_str(), i + 1 ); // Name depends on number

lvi.iImage = i ;
//% 5; // 5 images



lvi.iItem = i;

lvi.iSubItem = 0;

lvi.lParam = 1;

//iStatus = ListView_InsertItem( hList, &lvi ); // Add item to control



iStatus = ::SendMessage((hList), LVM_INSERTITEM, 0, (LPARAM)(
const LV_ITEM *)( &lvi));

}





return true;



}



int SListControl::GetCurSel()

{

//return ListView_GetNextItem(handle,-1,LVNI_FOCUSED);;

return SendMessage(handle,LVM_GETNEXTITEM,-1,LVNI_FOCUSED); // return item selected



}



cppcoder.blogspot.com/

No comments:

Post a Comment