Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to insert checkboxes to the subitems of a listcontrol using MFC

$
0
0

Hi Everyone,

I am new to MFC programming .At present i am currently working with "ListControl "where I am supposed to add the checkboxes to the subitems of ListControl .As per my knowledge i just added  the checkbox to the ListControl first column by setting the style as" LVS_EX_CHECKBOXES ".But actually what I am supposed to do is I must add the checkbox to the subitems of the ListControl  and that too of for columns.I have been adding the checkbox to the subitems as follows.

1)Created a listcontrol(of Report Style).

2)Inserted columns and items to the listcontrol.

3)Set the style of the listcontrol to LVS_EX_CHECKBOXES.

4)To the above step it is adding checkboxes to the first column only.

5)Inorder to get checkboxes to the subitems I added this piece of code.

           

BOOL CCheckListcontrolDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

m_ImageList.Create(16,16,ILC_COLOR16|ILC_MASK,1,0);
int nStateImageIdx = AppendStateImages(m_CheckListCtrl,m_ImageList);
//Setting the columns for the listcontrol
LVCOLUMN lvcolumn;
        int nlistcol;
//Inserting first column in the list control

lvcolumn.mask    = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
lvcolumn.fmt     = LVCFMT_LEFT; 
lvcolumn.cx      = 100;
lvcolumn.pszText = _T("Product Name");
int col = m_sel_col;
if(col == 2)
{

AddImageIndex(0,_T(""),false);
AddImageIndex(1,_T(""),true);
SetToggleSelection(true);

}
nlistcol         = m_CheckListCtrl.InsertColumn(0,&lvcolumn);   //Inserting the column to the listcontrol

//Inserting second column in the list control

lvcolumn.mask    = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
lvcolumn.fmt     = LVCFMT_LEFT;
lvcolumn.cx      = 100;
lvcolumn.pszText = _T("Name or IP Address");

m_CheckListCtrl.InsertColumn(1,&lvcolumn);   //Inserting the column to the listcontrol

//Inserting Third column in the list control

lvcolumn.mask    = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
lvcolumn.fmt     = LVCFMT_LEFT;
lvcolumn.cx      = 80;
lvcolumn.pszText = _T("PCL6");

if(col == 2)
{

AddImageIndex(0,_T(""),false);
AddImageIndex(1,_T(""),true);
SetToggleSelection(true);

}
m_CheckListCtrl.InsertColumn(2,&lvcolumn);   //Inserting the column to the listcontrol

//Inserting fourth column in the list control

lvcolumn.mask    = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
lvcolumn.fmt     = LVCFMT_LEFT;
lvcolumn.cx      = 80;
lvcolumn.pszText = _T("PS");

if(col == 3)
{

AddImageIndex(0,_T(""),false);
AddImageIndex(1,_T(""),true);
SetToggleSelection(true);

}
m_CheckListCtrl.InsertColumn(3,&lvcolumn);  //Inserting the column to the listcontrol

//Inserting fifth column in the list control

lvcolumn.mask    = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
lvcolumn.fmt     = LVCFMT_LEFT;
lvcolumn.cx      = 80;
lvcolumn.pszText = _T("PPD");

if(col == 4)
{

AddImageIndex(0,_T(""),false);
AddImageIndex(1,_T(""),true);
SetToggleSelection(true);

}
m_CheckListCtrl.InsertColumn(4,&lvcolumn);   //Inserting the column to the listcontrol


   //Setting the checkbox  style for listcontrol style 

m_CheckListCtrl.SetExtendedStyle(m_CheckListCtrl.GetExtendedStyle()|LVS_EX_CHECKBOXES);


//Inserting items in the column

LVITEM lvitem;
       int nItem;
//Inserting first item in the column
lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 0;
lvitem.iSubItem   = 0;
lvitem.pszText    = _T("SHARP MX-4111FN");
lvitem.cchTextMax = sizeof(lvitem.pszText);
nItem             = m_CheckListCtrl.InsertItem(&lvitem);  //setting item to the listcontrol

//Inserting the subitems for the first item in the listcontrol
lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 0;
lvitem.iSubItem   = 1;
lvitem.pszText    = _T("192.242.32.11");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 0;
lvitem.iSubItem   = 2;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 0;
lvitem.iSubItem   = 3;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);  //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 0;
lvitem.iSubItem   = 4;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

//Inserting second row item in the list control

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 1;
lvitem.iSubItem   = 0;
lvitem.pszText    = _T("SHARP MX-4112FN");
lvitem.cchTextMax = sizeof(lvitem.pszText);
nItem = m_CheckListCtrl.InsertItem(&lvitem);   //setting item to the listcontrol

//Inserting the subitems for second row item in the list control

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 1;
lvitem.iSubItem   = 1;
lvitem.pszText    = _T("192.23.252.36");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 1;
lvitem.iSubItem   = 2;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol


lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 1;
lvitem.iSubItem   = 3;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 1;
lvitem.iSubItem   = 4;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

//Inserting third row item  to the list control

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 2;
lvitem.iSubItem   = 0;
lvitem.pszText    = _T("SHARP MX-4113FN");
lvitem.cchTextMax = sizeof(lvitem.pszText);
nItem = m_CheckListCtrl.InsertItem(&lvitem);  //setting item to the listcontrol

//Inserting subitems for the third row item in the list control

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 2;
lvitem.iSubItem   = 1;
lvitem.pszText    = _T("192.23.242.42");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 2;
lvitem.iSubItem   = 2;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);  //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 2;
lvitem.iSubItem   = 3;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);   //setting item to the listcontrol

lvitem.mask       = LVIF_TEXT;
lvitem.iItem      = 2;
lvitem.iSubItem   = 4;
lvitem.pszText    = _T("");
lvitem.cchTextMax = sizeof(lvitem.pszText);
m_CheckListCtrl.SetItem(&lvitem);  //setting item to the listcontrol


int num_item = m_CheckListCtrl.GetItemCount();
for(size_t rowId = 0; rowId < num_item ; ++rowId)
{

for(int col = 0; col < 5; ++col)
{
int nCellCol = col+1;


if (nCellCol==2||nCellCol == 3||nCellCol == 4)
{

  SetCellImage(rowId, nCellCol, nStateImageIdx);// unchecked
  SetCellImage(rowId, nCellCol, nStateImageIdx);// unchecked
  SetCellImage(rowId, nCellCol, nStateImageIdx);// unchecked


}
}

}



return TRUE;  // return TRUE  unless you set the focus to a control
}

   

BOOL CCheckListcontrolDlg::SetCellImage(int n

Row,int nCol,int nImageId)
{
LVITEM lvitem = {0};
lvitem.iItem = nRow;
lvitem.iSubItem = nCol;
lvitem.mask = LVIF_IMAGE;
lvitem.iImage = nImageId;
return  (m_CheckListCtrl.SetItem(&lvitem));

}

//This is place adding of checkboxes is done.

int CCheckListcontrolDlg::AppendStateImages(CListCtrl& owner, CImageList& imagelist)
{
if (!(owner.GetExtendedStyle() & LVS_EX_SUBITEMIMAGES))
owner.SetExtendedStyle(owner.GetExtendedStyle() | LVS_EX_SUBITEMIMAGES);

if (!imagelist)
imagelist.Create(16, 16, ILC_COLOR16 | ILC_MASK, 1, 0);

if (!owner.GetImageList(LVSIL_SMALL))
owner.SetImageList(&imagelist, LVSIL_SMALL);

VERIFY( owner.GetImageList(LVSIL_SMALL)==&imagelist );

bool createdStateImages = false;
CImageList* pStateList = owner.GetImageList(LVSIL_STATE);
if (pStateList==NULL)
{
if (!(owner.GetExtendedStyle() & LVS_EX_CHECKBOXES))
{
createdStateImages = true;

 owner.SetExtendedStyle(owner.GetExtendedStyle() | (LVS_EX_CHECKBOXES));
 pStateList = owner.GetImageList(LVSIL_STATE);

}
}
int imageCount = -1;
ASSERT(pStateList!=NULL);
if (pStateList!=NULL && pStateList->GetImageCount() >= 2)
{
imageCount = imagelist.GetImageCount();

// Get the icon size of current imagelist
CSize iconSize(16,16);
if (imageCount > 0)
{
IMAGEINFO iconSizeInfo = {0};
VERIFY( imagelist.GetImageInfo(0, &iconSizeInfo) );
iconSize = 
CSize(iconSizeInfo.rcImage.right-iconSizeInfo.rcImage.left, 
iconSizeInfo.rcImage.bottom-iconSizeInfo.rcImage.top);
}

// Scale the icon-position if necessary
CPoint iconPos(1,0); // +1 pixel to avoid overlap with left-grid-line
{
IMAGEINFO stateSizeInfo = {0};
VERIFY( pStateList->GetImageInfo(0, &stateSizeInfo) );
int stateIconHeight = stateSizeInfo.rcImage.bottom-stateSizeInfo.rcImage.top;
if (iconSize.cy > stateIconHeight)
iconPos.y = (iconSize.cy - stateIconHeight) / 2;
}

// Redraw the state-icon to match the icon size of the current imagelist (without scaling image)
CClientDC clienDC(&owner);
CDC memDC;
VERIFY(memDC.CreateCompatibleDC(&clienDC));
CBitmap dstBmp;
VERIFY(dstBmp.CreateCompatibleBitmap(&clienDC, iconSize.cx, iconSize.cy));

CBitmap* pBmpOld = memDC.SelectObject(&dstBmp);
COLORREF oldBkColor = pStateList->SetBkColor(imagelist.GetBkColor());
CBrush brush(imagelist.GetBkColor());
memDC.FillRect(CRect(0,0,iconSize.cx, iconSize.cy), &brush);
VERIFY( pStateList->Draw(&memDC, 0, iconPos, ILD_NORMAL) );
memDC.SelectObject(pBmpOld);
VERIFY( imagelist.Add(&dstBmp, oldBkColor) != -1 );//Adding of Checkboxes is being done here
pBmpOld = memDC.SelectObject(&dstBmp);
memDC.FillRect(CRect(0,0,iconSize.cx, iconSize.cy), &brush);
VERIFY( pStateList->Draw(&memDC, 1, iconPos, ILD_NORMAL) );
memDC.SelectObject(pBmpOld);
VERIFY( imagelist.Add(&dstBmp, oldBkColor) != -1 );
pStateList->SetBkColor(oldBkColor);
}
if (createdStateImages)
owner.SetExtendedStyle(owner.GetExtendedStyle() & ~LVS_EX_CHECKBOXES);

return imageCount;
}


after doing so this is final o/p i was getting checkbox for the subitems and first column too ,but i am unable to check/uncheck the checkboxes.And I don't want checkbox for the first column.Finally I must be able to check/uncheck the checkbox .Moreover ,I tried handling NM_CLICK event I was getting the selected row and subitem but check state is not getting changed.Can anyone please let me know how can i achieve the toggling the checkboxe's and let me know what is to be incorporate in my code. 

Any pointers can be appreciated :)

Thanks in advance.


Viewing all articles
Browse latest Browse all 15302

Trending Articles