简体   繁体   中英

How to enable/disable checkbox on CGridCtrl in MFC

I am using MFC Grid control 2.27 ( http://www.codeproject.com/Articles/8/MFC-Grid-control-2-27 ) in my project. I would like to enable/disable checkbox on CGridCtrl . I'm not able to find the direct method to disable the check box. In fact, when I write the following line, it disables the grid cell on which checkbox is shown, while checkbox remains enabled.

m_Grid2.GetCell(row,col)->SetState(GVIS_READONLY);

Please suggest any way to disable/enable both checkbox and grid cell at runtime.

My suggestion is to add a second image list to the control. This image list would have disabled (greyed) versions of your images.

In CGridCtrl header you add:

void SetDisabledImageList(CImageList* pList) { m_pDisabledImageList = pList; }
CImageList* GetDisabledImageList() const { return m_pDisabledImageList; }

And add the variable, just after the existing m_pImageList :

CImageList* m_pDisabledImageList;

Now you need to prepare yourself a disabled version of your images and populate a CImageList (beyond the scope of this answer - just need a greyscale version of your image resource). Then, you simply pass that image list in using the new SetDisabledImageList method.


Finally, you modify the CGridCellBase::Draw method and massage the code like this:

CImageList *pImageListToUse = nullptr;
if (!pGrid->IsWindowEnabled())
    pImageListToUse = pGrid->GetDisabledImageList();

if(pImageListToUse == nullptr)
    pImageListToUse = pGrid->GetImageList();

So, whenever you set the cell as read only , as in your original code, it will render the icon using the disabled image list icons.

The above works OK for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM