Originally posted by: Mohammed Liaqat.
It's actually a question rather than a comment. I am trying to achieve the same thing with a Tree Control (instead of a
List Control), but applying the same technique doesn't seem to work ? I have derived 'MyCTreeCtrl' from CTreeCtrl which is created in a CDialog::OnInitDialog(), but the MyCTreeCtrl::OnPaint()doesn't seem to draw the vertical or Horizontal Lines !!
Could someone please explain.
thanks.
unixos@yahoo.com
Originally posted by: HN
Can anyone tell me where I could find the complete source code and sample.
thanks
ReplyOriginally posted by: Darren DeFalco
Here is a simple fix for the problem that Zafir described:
Here is a snippet of the code that Zafir posted, with the fix that I created:
>>> snip >>>
// Draw the horizontal grid lines
// if height was never set, 'synthesize' a height for items in the empty list
for( i = 1; i <= itemsper; i++ )
This easy fix will cause horizontal lines to be drawn in the listview, even when the list is empty.
Regarding "Drawing horizontal and vertical gridlines" - Zafir Anjum (1998/08/06)
"One implication of using GetItemRect() is that it fails when the list does not have any items in it
and no horizontal line is drawn."
// note: "rect" already has a value from a previous call to
// GetClientRect( &rect ) in the section which draws the column lines
int width = rect.right;
int itemsper = GetCountPerPage();
// remember this between paints
static int height = 0;
// only do this once, in case listview is resized
// this value will be very close to the real height of an item
if(!height)
height = (rect.bottom - top) / itemsper;
// height will not get set here if the list is empty
if( GetItemRect( 0, &rect, LVIR_BOUNDS ))
height = rect.bottom - rect.top;
{
dc.MoveTo( 0, top + height*i);
dc.LineTo( width, top + height*i );
}
>>> snip >>>
Originally posted by: Jerome Delamarche
Here is the proposed code, it is an adaptation of the code from the article "Drawing horizontal and
vertical gridlines" from Z.Anjum:
void CMyListCtrl::OnPaint()
if ((GetStyle() & LVS_TYPEMASK) == LVS_REPORT) {
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
GetClientRect( &rect );
// adjust rect.bottom to avoid drawing column for empty rows:
// adjustment for incomplete rows:
// The border of the column is offset by the horz scroll
// Draw the horizontal grid lines
int width = rect.right;
dc.SelectObject(oldpen);
// Do not call CListCtrl::OnPaint() for painting messages
Instead of handling notification why not avoid drawing horizontal lines for empty rows. In that way, vertical
lines will not suffer from the resizing problem...and the control will look better.
{
// TODO: Add your message handler code here
const MSG *msg = GetCurrentMessage();
DefWindowProc(msg->message,msg->wParam,msg->lParam);
// Get the number of columns
CClientDC dc(this );
CPen graypen(PS_SOLID,0,RGB(192,192,192));
CPen *oldpen = dc.SelectObject(&graypen);
int nColumnCount = pHeader->GetItemCount();
// The bottom of the header corresponds to the top of the line
RECT rect;
pHeader->GetClientRect( &rect );
int top = rect.bottom;
int headerheight = rect.bottom - rect.top;
// Now get the client rect so we know the line length and
// when to stop
int firstrow = GetTopIndex();
int perpage = GetCountPerPage();
int realrows = min(perpage,GetItemCount()-firstrow);
int rowheight = (rect.bottom - rect.top - headerheight) / perpage;
if (realrows * rowheight < rect.bottom - rect.top - headerheight &&
GetItemCount() > GetTopIndex() + realrows) {
//rect.bottom = realrows * rowheight + headerheight;
;
}
else {
rect.bottom = realrows * rowheight + headerheight;
}
int borderx = 0 - GetScrollPos( SB_HORZ );
for( int i = 0; i < nColumnCount; i++ ) {
// Get the next border
borderx += GetColumnWidth( i );
// if next border is outside client area, break out
if( borderx >= rect.right )
break;
// Draw the line.
dc.MoveTo( borderx-1, top);
dc.LineTo( borderx-1, rect.bottom );
}
// First get the height
if( !GetItemRect( 0, &rect, LVIR_BOUNDS ))
return;
int height = rect.bottom - rect.top;
GetClientRect( &rect );
for( i = 1; i <= realrows; i++ ) {
dc.MoveTo( 0, top + height*i);
dc.LineTo( width, top + height*i );
}
}
}
Originally posted by: Ganesh Kumar B V
/*I have tried this in VC++ 5.0. I have not tried it out in VC++ 4.2.*/
DWORD dwStyle = SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
if(m_bGridLines)
dwStyle |= LVS_EX_HEADERDRAGDROP| LVS_EX_GRIDLINES ;
else
dwStyle &= ~(LVS_EX_HEADERDRAGDROP| LVS_EX_GRIDLINES) ;
SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
/*Put the above in a function and just call it. That's it.*/
/* Need not struggle with codes of ondraw etc.. */