Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Checkboxes in CListCtrl via StateImages

I'm looking for a solution:
I intend to implement a CListCtrl (Reportstyle) with Checkboxes at the head of each line (like CCheckBox).
I've created two state icons (checked, unchecked). But the problem that occured was that if I try to click on the checkbox nothing changed respectively the state of another line which was selected changed. Furthermore I don't only want to change thy state but to open an dialog at a double click to get more information from the user.
Thus my accurate questions:
- How can I include the checkbox range into the selection?
- How can I change the state of the checkbox by a single click on the checkbox field (like the CListBox behaviour)?
- How can I effectively open an dialog by a double click (Override WM_...)?
- How can I simply implement a functionality for exchanging two lines? I've tried this but every time the selection of the line I wanted to go down or up was gone.

I would be very grateful if if you could could show me the source code for a 3 row Control like discribed above.
Thank in advance.
1 Solution
Take a look at samples\mfc\general\rowlist. To open dialog overide handle of WM_LBUTTONDBLCLK in your CListCtrl derived class and create and open dialog inside this function.
I have code that does all this for a list control

Probably a bit much to put up in detail here.

To add checkboxes...

void CMyListCtrl::AddExStyle(DWORD dwNewStyle) {
      DWORD dwStyle = GetExStyle();
      dwStyle |= dwNewStyle;

void CMyListCtrl::RemoveExStyle(DWORD dwDelStyle) {
      DWORD dwStyle = GetExStyle();
      dwStyle &= ~dwDelStyle;

void CMyListCtrl::EnableCheckboxes(bool enable) {
      if (enable) {
      } else {

To detect a checkbox change...

void CMyListCtrl::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) {
      UINT changes = (pNMListView->uNewState)^(pNMListView->uOldState);
      if (changes & LVIS_STATEIMAGEMASK) {
            UINT checkstate = ((pNMListView->uNewState&LVIS_STATEIMAGEMASK) >> 12)-1;
      *pResult = true;

where CheckStateChanged is a member function that gets called whenever a check state is changed

for double click...


int CMyListCtrl::HitTestMessage() const {
      const MSG * pMsg = GetCurrentMessage();
      CPoint pt = pMsg->pt;
      return HitTest(pt);

void CMyListCtrl::OnDblclk(NMHDR* /*pNMHDR*/, LRESULT* pResult) {
      m_clickitem = -1;
      int item = HitTestMessage(&col,NULL,&flag);
      if (item >= 0) {
            // handle double click (eg call dialog)
      *pResult = 0;

exchanging two lines looses selection...

void CMyListCtrl::PreSubclassWindow() {
      // .. other stuff

Hope this helps.

trouvainAuthor Commented:
Thsnx but you could be more specific

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now