 |
|
 |
Simple and clear
very help to me
|
|
|
|
 |
|
 |
Hi,
Thanks for this kind of good article. I tried your code, but for a List control having no items it doesn't show the alternate colors correctly. When there are items only the rows having the items gets the background color. When i checked the code, i found that GetItemPosition fails. This happens in VS2008. When i run your code using VS2005 it works. The problem is with the GetItemPosition. Any idea's
Thanks
Nitheesh.
Jose Jo Martin
http://www.simpletools.co.in
|
|
|
|
 |
|
 |
If you activate LVS_EX_DOUBLEBUFFER for the CListCtrl you will have the following redraw issues:
* the non column data area is not painted initially
* when you scroll the non column area modified is not painted
* when you select a record it will clean (paint white) an additional area after the selection. This will break backgrounds.
Double buffer is an important feature of CListCtrl that helps makes resizing and scrolling very smooth.
Best regards,
Sorin Sbarnea
|
|
|
|
 |
|
 |
I m also having the same painting problems.please tell me how can i resolve this?
|
|
|
|
 |
|
|
 |
|
 |
hello
I have a question.
Is it possible to change the color of an entire row via your control class?
Also: if I want to select a row from my code, and change the color to indicate that it has been selected via the code, not the result of the user clicking the row, is this possible?
Thank you.
A.T
|
|
|
|
 |
|
 |
change the m_colRow1 and m_colRow2 variables or create a new method for this purpose.
sdancer75
|
|
|
|
 |
|
 |
Hi,
its such a nice code.
here if list is empty the alternate row color not displayed.
is this possible is the list is empty,still alternate row colors are visible.
please help me for this.
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
 |
|
 |
Hey,
Your article was really very helpful. But my problem is how i can change Color of lines separating items in CListCtrl in report view(grid style) .
Thanks
|
|
|
|
 |
|
 |
Go to the source code and change the variables m_colRow1 & m_colRow2 you will found in the contructor with the prefered colors like this
m_colRow1 = RGB(255,0,0) --> make Row1 Red
Regards,
sdancer75
|
|
|
|
 |
|
 |
Hey ! Plz read the problem carefully. I am not interested in changing color of the alternate rows instead of that i am looking for the changing color of the grid lines.
|
|
|
|
 |
|
 |
Hmm,
I dont think that I have a solution for you. But an idea is to disable the grid line option and then try to design them by your own in the ErasaBackground virtual method.
Hope that it will help,
Regards,
sdancer75
|
|
|
|
 |
|
 |
I'd like to use this in my commercial product, do you require licensing to be followed regarding this component?
|
|
|
|
 |
|
 |
Nope its free to use and to modify. Just refer my name as the original author.
Thanks for asking !
sdancer75
|
|
|
|
 |
|
 |
Hi I tried to use this functionality in dialog based application.
But Dialog loosing its image, a transparent dialog is showing.
How to over come this in Dialog Based Apps.
|
|
|
|
 |
|
 |
I use your code in my project ,but when add data,color back is not appear in data area else is appear,Why?
|
|
|
|
 |
|
 |
Did you adjusted the m_colRow1 & m_colRow2 variables ? Did you changed the textbackground color for your data? Check once again the example ! Below i am giving a snip of my code.
It just creates the CListCtrl object, adjust the font, define the columns and after that it fills it from a database. There is nothing tricky here !!
----- creates the object ----------
m_ListCtrl.Create( LVS_REPORT | LVS_SHOWSELALWAYS | LBS_NOTIFY, CRect(0,0,400,200), this, IDC_LISTCTRL );
m_ListCtrl.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
------ sets the font ---------
CFont fArial;
fArial.CreatePointFont (100,"System");
m_ListCtrl.SetFont(&fArial);
----- define the columns --------
GetClientRect(r);
//m_ListCtrl.SetColumnWidth (0,r.right /3);
m_ListCtrl.SetColumnWidth (1,r.right /3);
m_ListCtrl.SetColumnWidth (2,r.right /3);
m_ListCtrl.SetColumnWidth (3,(r.right /3) /2 );
m_ListCtrl.SetColumnWidth (4,(r.right /3) /2 );
m_ListCtrl.SetWindowPos (&wndTop,0,0,r.right,r.bottom ,SWP_SHOWWINDOW);
m_ListCtrl.InsertColumn (0,_T("column1"),LVCFMT_CENTER);
m_ListCtrl.InsertColumn (1,_T("column2"),LVCFMT_LEFT);
m_ListCtrl.InsertColumn (2,_T("column3"),LVCFMT_LEFT);
m_ListCtrl.InsertColumn (3,_T("column4"),LVCFMT_CENTER);
m_ListCtrl.InsertColumn (4,_T("column5"),LVCFMT_CENTER);
-------- inserts data from a database ------
m_ListCtrl.InsertItem(counter,_T(strVal));
pRs->GetFieldValue("Title", strVal);
m_ListCtrl.SetItemText(counter,1,_T(strVal));
pRs->GetFieldValue("Author1", strVal);
m_ListCtrl.SetItemText(counter,2,_T(strVal));
pRs->GetFieldValue("PublishHouseDesc", strVal);
m_ListCtrl.SetItemText(counter,3, _T(strVal));
pRs->GetFieldValue("CategoryDesc", strVal);
m_ListCtrl.SetItemText(counter,4, _T(strVal));
Regards,
sdancer75
|
|
|
|
 |
|
 |
Thanks for you reply,i seek my code and find finger out,because i am not use Create method create this listctrl,i was Drag a listctrl Controls,and i use the create method ,it's ok~ Thank you very much!
|
|
|
|
 |
|
 |
Hi,
Here is my CustomDrawList class in the following. can you tell me how to implement your solution (cloredList)?
Thanks,
// CustomDrawList.cpp : implementation file
#include "stdafx.h"
#include "CustomDrawList.h"
// CCustomDrawList
IMPLEMENT_DYNAMIC(CCustomDrawList, CListCtrl)
CCustomDrawList::CCustomDrawList()
{
}
CCustomDrawList::~CCustomDrawList()
{
}
BEGIN_MESSAGE_MAP(CCustomDrawList, CListCtrl)
ON_WM_CTLCOLOR()
ON_WM_VSCROLL()
END_MESSAGE_MAP()
// CCustomDrawList message handlers
HBRUSH CCustomDrawList::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
void CCustomDrawList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
//CustomDrawList.h
#pragma once
// CCustomDrawList
class CCustomDrawList : public CListCtrl
{
DECLARE_DYNAMIC(CCustomDrawList)
public:
CCustomDrawList();
virtual ~CCustomDrawList();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
};
|
|
|
|
 |
|
 |
hi,
its farily simple. Just plug the onDraw to your custom control and do the appropriate changes to the color.
Regards,
sdancer75
|
|
|
|
 |
|
 |
.... to the OnEraseBkgnd i meant, sorry.
sdancer75
|
|
|
|
 |
|
 |
Can you implement your code into my above code?
Thanks,
|
|
|
|
 |
|
|
 |
|
 |
Does anyone know how to implement this in a CListView?
TIA
Tony
|
|
|
|
 |
|
 |
Hi, I made some code changes to support scrolling of the list control, clipping (when the column data does not go all the way to the right of the control), and when the last line/item is only partially displayed. I also fiddled with the colors so that it chooses colors that are compatible with the 3 basic windows default theme, Blue, Olive, and Silver. Feel free to include these in your next update of the control. Chuck ---------------------------MyListCtrl.H------------------------- #if !defined(AFX_MYLISTCTRL_H__ED7781FB_AB0B_49F4_B58E_3D011912BEB4__INCLUDED_) #define AFX_MYLISTCTRL_H__ED7781FB_AB0B_49F4_B58E_3D011912BEB4__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // MyListCtrl.h : header file // #include "stdafx.h" ///////////////////////////////////////////////////////////////////////////// // MyListCtrl window class MyListCtrl : public CListCtrl { // Construction public: MyListCtrl(); DetermineScheme(); // Attributes public: COLORREF m_colRow1; COLORREF m_colRow2; // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(MyListCtrl) //}}AFX_VIRTUAL // Implementation public: virtual ~MyListCtrl(); // Generated message map functions protected: //{{AFX_MSG(MyListCtrl) afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_MYLISTCTRL_H__ED7781FB_AB0B_49F4_B58E_3D011912BEB4__INCLUDED_) ------------------------MyListCtrl.CPP----------------------------------------------- // MyListCtrl.cpp : implementation file // #include "stdafx.h" #include "cmnctrl1.h" #include "MyListCtrl.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // MyListCtrl MyListCtrl::MyListCtrl() { } MyListCtrl::~MyListCtrl() { } MyListCtrl::DetermineScheme() { COLORREF current = GetSysColor(COLOR_HIGHLIGHT); // determines the XP windows theme colors if (current == RGB(178, 180, 191)) { // Theme Silver m_colRow1 = RGB(243,243,245); m_colRow2 = RGB(227,227,232); } else if (current == RGB(147,160,112)) { // Theme Olive m_colRow1 = RGB(244,253,244); m_colRow2 = RGB(223,247,223); } else // Theme Blue or anything else { m_colRow1 = RGB(243,249,253); m_colRow2 = RGB(208,232,251); } } BEGIN_MESSAGE_MAP(MyListCtrl, CListCtrl) //{{AFX_MSG_MAP(MyListCtrl) ON_WM_ERASEBKGND() ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // MyListCtrl message handlers void MyListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) { *pResult = 0; LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR; int iRow = lplvcd->nmcd.dwItemSpec; switch(lplvcd->nmcd.dwDrawStage) { case CDDS_PREPAINT : { *pResult = CDRF_NOTIFYITEMDRAW; return; } // Modify item text and or background case CDDS_ITEMPREPAINT: { lplvcd->clrText = RGB(0,0,0); // If you want the sub items the same as the item, // set *pResult to CDRF_NEWFONT *pResult = CDRF_NOTIFYSUBITEMDRAW; return; } // Modify sub item text and/or background case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM: { DetermineScheme(); if(iRow % 2) { lplvcd->clrTextBk = m_colRow2; } else { lplvcd->clrTextBk = m_colRow1; } } default: { *pResult = CDRF_DODEFAULT; return; } } } BOOL MyListCtrl::OnEraseBkgnd(CDC* PaintDC) { CRect ctrl; CRect rect; CDC *pDC = GetDC(); // non-clipped DC DetermineScheme(); CBrush brush0(m_colRow1); CBrush brush1(m_colRow2); GetClientRect(ctrl); PaintDC->FillRect(&ctrl, &brush0); int index = GetTopIndex(); int last_visible_index = index + GetCountPerPage() + 1; // since it's rare that an exact number fits, last visible may be a partial if (GetItemRect(index, &rect, LVIR_BOUNDS) != 0) { int itemHeight = rect.Height(); // Loop until the number of visible items has been reached. while (index <= last_visible_index) { PaintDC->FillRect(&rect, index % 2 ? &brush1 : &brush0); ctrl.left = rect.right; ctrl.top = rect.top; ctrl.bottom = rect.bottom; pDC->FillRect(&ctrl, index % 2 ? &brush1 : &brush0); // fill part of control without column data rect.top = rect.bottom; rect.bottom = rect.top + itemHeight; index++; } } brush0.DeleteObject(); brush1.DeleteObject(); ReleaseDC(pDC); return FALSE; } ------------------------------------------------------------------------
|
|
|
|
 |