Jump to content
Tuts 4 You

Change the color of grid lines for ListCtrl


CodeExplorer

Recommended Posts

Posted (edited)

Change the color of grid lines for ListCtrl
so I've set this:
    m_List.SetExtendedStyle(LVS_EX_GRIDLINES |
LVS_EX_FULLROWSELECT |
LVS_EX_SUBITEMIMAGES |
LVS_OWNERDRAWFIXED
);

I've created message like this
void MyListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
...
    if (ShowCustomGridLines)
    DrawGridLines();

Now problematic code:
// https://aucd29.tistory.com/2078
 

void MyListCtrl::DrawGridLines()
{

    // TODO: Add your message handler code here
    // Do not call CListCtrl::OnPaint() for painting messages
    // First let the control do its default drawing.
    const MSG *msg = GetCurrentMessage();
    DefWindowProc(msg->message, msg->wParam, msg->lParam);

    // Draw the lines only for LVS_REPORT mode
    if((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
    {
        int i;
        CClientDC dc(this);
        CPen myPen, *oldPen;
        
        myPen.CreatePen(PS_SOLID, 1, RGB(105, 105, 105));
        oldPen = dc.SelectObject(&myPen);

         // Get the number of columns:
        CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
        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;

        // Now get the client rect so we know the line length and
        // when to stop
        GetClientRect(&rect);

        // The border of the column is offset by the horz scroll
        int borderx = 0;//-GetScrollPos(SB_HORZ);

        int minpos, maxpos, pos;
        pos = GetScrollPos(SB_HORZ);
        GetScrollRange(SB_HORZ, &minpos, &maxpos);

        int size1 = 0;
        for(i = 0; i < nColumnCount; i++)
        {
            // Get the next border
            size1 += GetColumnWidth(0);  // i

        }

        //borderx -= 1;  // debatable if this should be enabled

        //for(i = 0; i < nColumnCount; i++)
        //{
            // Get the next border
            borderx += GetColumnWidth(0);  // i

            // if next border is outside client area, break out
//            if( borderx >= rect.right ) break;

            // Draw the line.
            dc.MoveTo( borderx, top);
            dc.LineTo( borderx, rect.bottom );
        //}

Original code:
int borderx = 0-GetScrollPos(SB_HORZ);
borderx += GetColumnWidth(0);
The problem with the above is that if I scroll the first horizontal grid line (now I've stick to that) will be placed after the right place,
pos = GetScrollPos(SB_HORZ); doesn't return proper value,
I've also noticed that pos when I scroll to maxim is a lot smaller than maxpos.
And all this crap is just for changing a stupid color.
 

 

Edited by CodeExplorer
Posted

Just for info,
how to draw a rectangle with line, rectangle with coordinates in rcColumn
so instead of FillRect(lpLVCustomDraw->nmcd.hdc,rcColumn, black);
do this instead:

        MoveToEx(lpLVCustomDraw->nmcd.hdc, rcColumn.left , rcColumn.top , NULL);
        // we are are in left-top corner, draw line to right top:
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.right, rcColumn.top );
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.right, rcColumn.bottom );
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.left , rcColumn.bottom );
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.left , rcColumn.top);

and in order to draw just two line in bottom and in right of rectangle:

        MoveToEx(lpLVCustomDraw->nmcd.hdc, rcColumn.left , rcColumn.bottom , NULL);
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.right, rcColumn.bottom );
        LineTo  (lpLVCustomDraw->nmcd.hdc, rcColumn.right, rcColumn.top );


Finally I found this: - seems to work ok
https://forums.codeguru.com/showthread.php?395167-CListCtrl-and-onPaint

Posted

Comments are funny

image.png.55ec2bef9e47b37b085e59e7fcaabf70.png

Posted (edited)

I don't think are funny, OnPain is just another way of custom drawing controls; nothing too special.

https://www.codeproject.com/Articles/8/MFC-Grid-control-2-27
I am unable to compile and run this project

https://www-user.tu-chemnitz.de/~heha/petzold/ch05i.htm
nice read about rectangles types

There is no problem with GetScrollPos(SB_HORZ), the value returned is good.
There is some space (2 pixels) between header and first item where are drawn with craps,
I just chosen to draw over it with white color:

        RECT rect;
        pHeader->GetClientRect(&rect);

		RECT firstitem;
        int nrItem = GetItemRect(0, &firstitem, LVIR_BOUNDS);
        if (nrItem)
        {
        CBrush white = RGB(255, 255, 255);
        RECT crap = rect;
        crap.bottom = rect.bottom+2;
        crap.top = rect.bottom;
        dc.FillRect(&crap, &white);
        }

Now the problem is that when I scroll down GetItemRect(0, &firstitem, LVIR_BOUNDS);
the firstitem.top value has negative, I already know that in my system has 2 pixels so I just did crap.bottom = rect.bottom+2;
Now just wandering if there is a better way to get the size?
 

Edited by CodeExplorer
Posted

Silly me, solution:
int nrItem = GetItemRect(GetTopIndex(), &itemrect, LVIR_BOUNDS);

crap.bottom = itemrect.top;

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...