我想将CListCtrl
的大小调整为Report
,以便仅显示n行带有文本或为空的行。我需要一行的高度(它们都具有相同的大小和常数)。
CRect rect;
myList.GetWindowRect(&rect);
ScreenToClient(&rect);
myList.MoveWindow(rect.left, rect.top, rect.Width(), 16*8-5 /* TODO */);
最佳答案
CListCtrl具有可变的行高
/*
1a. Setup a typical CListCtrl with owner draw
1b. Fill the CListCtrl with the text you want, as you would normally
2. Setup a CListBox with OwnerDrawVariable and NO border
3. Make the ListBox a child of the ListCtrl
4. Use OnSize to position and OnDrawItem to display the text
Note the OnDrawItem is using the text and some parameters from the CListCtrl
so be mindful of m_lbTest vs. m_lcTest.
*/
void CTestDlg::FillListCtrl()
{
// fill CListCtrl with some text
int x,y;
int ColCount;
CString csStr;
m_lbTest.SetParent(&m_lcTest);
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (y=0; y<10; y++) {
m_lcTest.InsertItem(y,"");
for (x=0; x<ColCount; x++) {
csStr.Format("y=%d x=%d",y,x);
m_lcTest.SetItemText(y,x,csStr);
}
m_lbTest.AddString("");
m_lbTest.SetItemHeight(y,20 + y*10);
}
}
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
int x,y;
CRect rb;
if (m_lcTest.m_hWnd != NULL) {
y=0;
x=0;
m_lcTest.SetWindowPos(&wndTop,x,y,cx,cy-y,SWP_NOZORDER);
m_lcTest.GetHeaderCtrl()->GetClientRect(&rb);
x=0;
y+=rb.Height();
m_lbTest.SetWindowPos(&wndTop,x,rb.Height(),cx-x-4,cy-y-4,SWP_NOZORDER);
}
}
void CTestDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpD)
{
CString csStr;
CRect rc;
CRect rb;
UINT fmt;
LVCOLUMN lvc;
int x,y;
CDC dc;
int ColCount;
CPen cPen;
CopyRect(&rc,&lpD->rcItem);
dc.Attach(lpD->hDC);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
cPen.CreatecPen.CreatePen(PS_SOLID,1,RGB(192,192,192));
dc.SelectObject(cPen);
dc.SetTextColor(RGB(0,0,0));
if (m_lbTest.m_hWnd!=NULL && nIDCtl==IDC_LB_TEST) {
y=lpD->itemID;
if (y >= 0) {
rc.left+=5;
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (x=0; x<ColCount; x++){
rc.right=rc.left + m_lcTest.GetColumnWidth(x) - 10;
rb.top =rc.top+1;
rb.bottom=rc.bottom;
rb.left =rc.left - 5;
rb.right =rc.right + 4;
if ((lpD->itemState&ODS_SELECTED) != 0) dc.FillSolidRect(&rb,RGB(255,255,192)); // light yellow
else dc.FillSolidRect(&rb,RGB(255,255,255));
rb.top--;
dc.MoveTo(rb.left-1,rb.bottom);
dc.LineTo(rb.right, rb.bottom);
dc.LineTo(rb.right, rb.top);
dc.LineTo(rb.left-1,rb.top);
lvc.mask=LVCF_FMT;
m_lcTest.GetColumn(x,&lvc);
if ((lvc.fmt&LVCFMT_RIGHT) != 0) fmt=DT_RIGHT;
else if ((lvc.fmt&LVCFMT_CENTER) != 0) fmt=DT_CENTER;
else fmt=DT_LEFT;
fmt|=DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX;
csStr=m_lcTest.GetItemText(y,x);
dc.DrawText(csStr,&rc,fmt);
rc.left=rc.right + 10;
}
}
}
cPen.DeleteObject();
dc.Detach();
}