CListCtrl Ownerdraw stop working

  • Thread starter Thread starter no_one_special
  • Start date Start date
N

no_one_special

Guest
Hi,

Here is an issue. I have created a frame window that uses a dialog as a child window. Within the dialog is a CListCtrl that is classified as OWNERDRAWFIXED. On startup, the dialog is loaded, ON_WM_MEASUREITEM is called properly (I use a larger font), OnInitDialog is called and the list control is populated, the control is drawn flawlessly! However, the nature of the app may call for a new list of data that does not use one of the columns, so I delete it, and that cause both the ownerdraw to stop working, and also mouse clicks (select a row). I have included the dialog code. Any ideas?

******************************** Header *********************************

#pragma once

class CTestDialog : public CDialogEx
{
// Construction
public:
CTestDialog(CWnd* pParent = nullptr); // standard constructor
virtual ~CTestDialog(void);

// Attributes
protected:
CListCtrl m_ListCtrl;
CFont m_Font;

// Overrides
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

public:
BOOL OnInitDialog(void);

// Generated Message Map Functions
public:
afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnButton_Test(void);
DECLARE_MESSAGE_MAP()
};


************************************** Code ************************************

#include "stdafx.h"
#include "MFC_Test.h"
#include "TestDialog.h"
#include "afxdialogex.h"
#include "resource.h"

BEGIN_MESSAGE_MAP(CTestDialog, CDialogEx)
ON_WM_MEASUREITEM()
ON_WM_DRAWITEM()
ON_BN_CLICKED(IDC_BUTTON1, OnButton_Test)
END_MESSAGE_MAP()

CTestDialog::CTestDialog(CWnd* pParent) : CDialogEx(IDD_DIALOG1, pParent)
{
}

CTestDialog::~CTestDialog(void)
{
}

void CTestDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LISTCTRL, this->m_ListCtrl);
}

BOOL CTestDialog::OnInitDialog(void)
{
CString str;
int index = 0;

CDialogEx::OnInitDialog();

for (index = 0; index < 8; index++)
{
str.Format("%d", index);
this->m_ListCtrl.InsertColumn(index, str, LVCFMT_LEFT, 50, index);
}

for (index = 0; index < 20; index++)
{
str.Format("%d", index);
this->m_ListCtrl.InsertItem(index, str);
}

return TRUE;
}

void CTestDialog::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
CDC *pDC = nullptr;
CFont *pOldFont = nullptr;
bool failed = false;

if (!(pDC = this->GetDC()))
{
CDialogEx::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
else
{
pOldFont = pDC->SelectObject(&this->m_Font);
lpMeasureItemStruct->itemHeight = pDC->GetTextExtent("QQQgggjkjj").cy;
pDC->SelectObject(pOldFont);
this->ReleaseDC(pDC);
}
}

void CTestDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = nullptr;
int index = 0;
CString text;
CBrush normalBkBrush(::GetSysColor(COLOR_WINDOW));
CBrush selectedBkBrush(::GetSysColor(COLOR_HIGHLIGHT));
int columnCount = this->m_ListCtrl.GetHeaderCtrl()->GetItemCount();
CRect itemRect = lpDrawItemStruct->rcItem;
int columnWidth = 0;
unsigned int format = 0;
int xPos = 0;

do
{
if (!(pDC = new CDC()))
{
break;
}

if (!pDC->Attach(lpDrawItemStruct->hDC))
{
break;
}

if (lpDrawItemStruct->itemState &= ODS_SELECTED)
{
pDC->FillRect(&itemRect, &selectedBkBrush);
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
}
else
{
pDC->FillRect(&itemRect, &normalBkBrush);
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}

pDC->SetBkMode(TRANSPARENT);

for (index = 0; index < columnCount; index++)
{
columnWidth = this->m_ListCtrl.GetColumnWidth(index);
((format = (index == 0 ? DT_CENTER : DT_RIGHT)));

text = this->m_ListCtrl.GetItemText(lpDrawItemStruct->itemID, index);

itemRect.left = xPos;
itemRect.right = xPos + columnWidth;

pDC->DrawText(text, itemRect, format);

xPos += columnWidth;
}
}
while (false);

if (pDC != nullptr)
{
pDC->Detach();
delete pDC;
}
}
void CTestDialog::OnButton_Test(void)
{
CString str;
int index = 0;

this->m_ListCtrl.DeleteAllItems();

while (this->m_ListCtrl.GetHeaderCtrl()->GetItemCount() > 0)
{
this->m_ListCtrl.GetHeaderCtrl()->DeleteItem(0);
}

for (index = 0; index < 8; index++)
{
str.Format("%d", index);
this->m_ListCtrl.InsertColumn(index, str, LVCFMT_LEFT, 50, index);
}

for (index = 0; index < 20; index++)
{
str.Format("%d", index);
this->m_ListCtrl.InsertItem(index, str);
}
}

Continue reading...
 
Back
Top