|
Hi AnthonyJ,
very very nice. fantastic tip!
Furthermore I saw you can also use the following macro and don't need to extend your list class:
#define ListView_SortItemsEx(hwndLV, _pfnCompare, _lPrm) \
(BOOL)SNDMSG((hwndLV), LVM_SORTITEMSEX, (WPARAM)(LPARAM)(_lPrm), (LPARAM)(PFNLVCOMPARE)(_pfnCompare))
makes it even more easy to use...
regards,
mykel
If they give you lined paper, write the other way!
|
|
|
|
|
How to get the current choise using ur class? Like when the user doubleclick or click on the row, there is GetCurSel() in the CListBox class to get the choice or selection made by the user. How can i do that with ur class?
Thanks
Desvario
And is a nice class, by the way...
|
|
|
|
|
Like in other CListCtrl, you just have to call to "GetNextItem":
int hIndex = GetNextItem(-1, LVNI_SELECTED);
You will get an index to the position where the user cursor is. You must call to "GetItemText" to get the text content.
----------------------------
Ivan Gadea Saez
Senior Software developer
|
|
|
|
|
great code, thanks alot for it.
When i refresh my listitems, by deleting all and adding them again, how can i stay in the previous "sort" mode?
|
|
|
|
|
ItemData* pid = new ItemData;
in _AssignNewItemData(...)
every sort, "new" so large!!!
|
|
|
|
|
Hi,
In View, I am unable to successfully create an object of CSortListCtrl.
The statement CSortListCtrl m_ListCtrl; or m_ListCtrl = new CSortListCtrl; returns a NULL for m_ListCtrl. So am not able to reference any of its functions in View. I wish someone could show me how!!
Pete
|
|
|
|
|
Try to select the list control (make the focus on the list) and Minimize and maximize the dialog.
The list control disappeared !!
A. D
|
|
|
|
|
hello,
I am trying to select a row in a SortListCtrl to no avail...
<br />
mySortListCtrl.SetItemState(indexToSelect, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);<br />
does SortListCtrl behave the same way as the ClistCtrl with dynamic selecting of its items?
thanks!
|
|
|
|
|
you can do that like this
m_List1.SetItemState(idx, LVIS_SELECTED, LVIS_SELECTED);
m_List1.SetFocus();
|
|
|
|
|
|
I'm sure I'm totally missing the forest for the trees here (it's 1am), but here's what I'm trying to do. I've added the check box style to this list control. I'm trying to iterrate through the list seeing which ones have been checked and getting the text.
Thanks in advance,
Mike Ellertson
|
|
|
|
|
hi,
that code does not change the DrawItem-function. just added some code to the OnPaint-handler ;o)
first you have to remove the owner-draw style from the header-ctrl in the SetSortArrow-function:
<br />
<br />
(just comented it out)
than ad an OnPaint-Handler in your SortHeaderCtrl.x
SortHeaderCtrl.h:
<br />
afx_msg void OnPaint();<br />
<br />
DECLARE_MESSAGE_MAP()<br />
SortHeaderCtrl.cpp:
<br />
void CSortHeaderCtrl::OnPaint() {<br />
CHeaderCtrl::OnPaint();<br />
<br />
if(m_iSortColumn < 0)<br />
return;<br />
<br />
CRect pRect;<br />
<br />
TCHAR szText[ 256 ];<br />
HD_ITEM hditem;<br />
<br />
hditem.mask = HDI_TEXT | HDI_FORMAT;<br />
hditem.pszText = szText;<br />
hditem.cchTextMax = 255;<br />
<br />
GetItem(m_iSortColumn, &hditem);<br />
<br />
CString ItemText = hditem.pszText;<br />
<br />
GetItemRect(m_iSortColumn, &pRect);<br />
<br />
CDC *pDC = GetDC();<br />
<br />
CPoint pPoints[3];<br />
<br />
CSize sz = pDC->GetTextExtent(ItemText);<br />
int x = pRect.left + sz.cx + 5;<br />
int y = (pRect.bottom / 2) - 3;<br />
<br />
pRect.right -= 2;<br />
CRgn pRgn;<br />
pRgn.CreateRectRgnIndirect(pRect);<br />
pDC->SelectClipRgn(&pRgn);<br />
<br />
if( m_bSortAscending )<br />
{<br />
pPoints[0] = CPoint(x + 5, y);<br />
pPoints[1] = CPoint(x + 10, y + 5);<br />
pPoints[2] = CPoint(x, y + 5);<br />
} else {<br />
pPoints[0] = CPoint(x, y);<br />
pPoints[1] = CPoint(x + 10, y);<br />
pPoints[2] = CPoint(x + 5, y + 5);<br />
}<br />
pDC->Polygon(pPoints, 3);<br />
}<br />
the important thing is of course to call CHeaderCtrl::OnPaint(); bevore you do your own painting stuff.
play around wit the Pen and Brush-colors for the polygon (the arrow) - i left this out, so the arrow is white and the border of it is black. looks good i think.
have fun!
vertex
|
|
|
|
|
It seems that you've forgotten to call ReleaseDC(pDC) at the end of OnPaint.
Anyway, thanks for your great code!
|
|
|
|
|
To insert a variable icon before each row, i must use the function CListCtrl::InsertItem(),just like this:
<br />
<br />
m_images.Create(IDB_CHECK_BOX,13,1,RGB(255,255,255));<br />
m_listctrl.SetImageList(&m_images,LVSIL_STATE);<br />
<br />
LVITEMA lvi;<br />
lvi.iSubItem=0;<br />
lvi.mask=LVIF_TEXT|LVIF_STATE;<br />
lvi.stateMask=LVIS_STATEIMAGEMASK;<br />
lvi.iItem=i;<br />
lvi.pszText="some words";<br />
lvi.state=INDEXTOSTATEIMAGEMASK(UNCHECKED_INDEX);<br />
m_listctrl.InsertItem(&lvi); <br />
then i use your function SetItemText(),but it generates a bug,so i add a member funtion AddItem2(LPCTSTR pszText, ... ) to match the function CListCtrl::InsertItem(),as follows:
<br />
int CSortListCtrl::AddItem2( LPCTSTR pszText, ... )<br />
{<br />
const int iIndex = GetItemCount() - 1;<br />
<br />
LPTSTR* arrpsz = new LPTSTR[ m_iNumColumns ];<br />
arrpsz[ 0 ] = new TCHAR[ lstrlen( pszText ) + 1 ];<br />
(void)lstrcpy( arrpsz[ 0 ], pszText );<br />
<br />
va_list list;<br />
va_start( list, pszText );<br />
<br />
for( int iColumn = 1; iColumn < m_iNumColumns; iColumn++ )<br />
{<br />
pszText = va_arg( list, LPCTSTR );<br />
ASSERT_VALID_STRING( pszText );<br />
VERIFY( CListCtrl::SetItem( iIndex, iColumn, LVIF_TEXT, pszText, 0, 0, 0, 0 ) );<br />
<br />
arrpsz[ iColumn ] = new TCHAR[ lstrlen( pszText ) + 1 ];<br />
(void)lstrcpy( arrpsz[ iColumn ], pszText );<br />
}<br />
<br />
va_end( list );<br />
<br />
VERIFY( SetTextArray( iIndex, arrpsz ) );<br />
<br />
return iIndex;<br />
}<br />
In addition, your code can not sort float, so i add two member function , as follows:
<br />
bool IsFloat( LPCTSTR pszText )<br />
{<br />
ASSERT_VALID_STRING( pszText );<br />
<br />
for( int i = 0; i < lstrlen( pszText ); i++ )<br />
if( !_istdigit( pszText[ i ] ) && pszText[i] != '.')<br />
return false;<br />
<br />
return true;<br />
}<br />
<br />
<br />
int FloatCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )<br />
{<br />
ASSERT_VALID_STRING( pszNumber1 );<br />
ASSERT_VALID_STRING( pszNumber2 );<br />
<br />
const float iNumber1 = atof( pszNumber1 );<br />
const float iNumber2 = atof( pszNumber2 );<br />
<br />
if( iNumber1 < iNumber2 )<br />
return -1;<br />
<br />
if( iNumber1 > iNumber2 )<br />
return 1;<br />
<br />
return 0;<br />
}<br />
<br />
i just make a little modification based on your codes.
your codes is very good, thank you for sharing these codes.
sea_soul
|
|
|
|
|
while creating the control dynamically like this :
m_pList = new CSortListCtrl;
m_pList->Create(LVS_REPORT|WS_CHILD|WS_VISIBLE,Rect,this, 0x1000 );
it crashes while running the debug version. The Create() calls CSortListCtrl::PreSubclassWindow() and the problematic line is:
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
What can be done ? Why is that happenning?
|
|
|
|
|
I've got the same result in creating the control dynamically.
The GetHeaderCtrl() call in PreSubclassWindow fails because it returns NULL pointer.
So, I subclassed the CHeaderCtrl in the parent window.
I think that would work.
....
m_pListCtrl = new CSortListCtrl;
m_pListCtrl->Create(LVS_REPORT|WS_CHILD|WS_VISIBLE|LVS_SHOWSELALWAYS|LVS_EDITLABELS,
CRect(0, 0, 0, 0), this, 0);
VERIFY( m_pListCtrl->m_ctlHeader.SubclassWindow( m_pListCtrl->GetHeaderCtrl()->GetSafeHwnd() ) );
....
The code above part of the create function in the parent window.
|
|
|
|
|
Thanks a lot, works fine, also in views
MS
|
|
|
|
|
It cann't work in view of SDI, how did you implement?
Benny fun
|
|
|
|
|
In the original code the subclassing of the header control class is done in the function PreSubclassWindow of the list control class:
void CSortListCtrl::PreSubclassWindow()
{
// the list control must have the report style.
ASSERT( GetStyle() & LVS_REPORT );
CListCtrl::PreSubclassWindow();
//subclassing code
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd()));
}
When working with a view instead of a dialog you have to do the subclassing of the header control in the OnInitialUpdate function of your view class. In other words: move the line
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd()));
into the OnInitialUpdate handler of your view class after creating the CSortListCtrl:
//my view class
void CWiz_ESView::OnInitialUpdate()
{
CView::OnInitialUpdate();
RECT rect;
GetClientRect(&rect);
//m_listCtrl is the CSortListCtrl member of my view class
m_listCtrl.Create(LVS_REPORT|WS_CHILD|WS_VISIBLE|LVS_SHOWSELALWAYS|LVS_EDITLABELS, rect, this, 0);
VERIFY( m_listCtrl.m_ctlHeader.SubclassWindow(m_listCtrl.GetHeaderCtrl()->GetSafeHwnd() ) );
//adding the columns to the list
m_listCtrl.SetHeadings("#,50;Hierarchy,100;Plan,100;Baustein,100;Bausteinkommentar,120;Anschluss,100;\
Anschlusskommentar,120;Messtellenanschluss,5;Wert,100;Signal,100;Einheit,50;\
Datentyp,100;I/O,50;Bausteintyp,100;Messtellentyp,5;Instanz DB,50");
m_listCtrl.LoadColumnInfo();
...
...
}
Hope this helps
MS
|
|
|
|
|
It cann't work completely.
Benny fun
|
|
|
|
|
|
It works fine now,
thanks you all.
|
|
|
|
|
Hi...
The control is nice, but there are a few bugs when using it in a view:
1) The control does not show scrollbars (vertical and horizontal)
2) The header control is not correctly updated when the user resizes the view window (ie, in a SDI view).
Thanks.
MaxPayne.
|
|
|
|
|
Hi,
In View, I am unable to successfully create an instance of CSortListCtrl in View.
The statement CSortListCtrl m_ListCtrl; returns a NULL for m_ListCtrl.
-Pete
Pete
|
|
|
|
|
Although this does work fine, I didn't want the container to know about the HeaderCtrl inside the ListCtrl, so what I did was:
a) safegaurd the PreSubclassWindow call with a check to make sure that the GetHeaderCtrl() returned a valid pointer (althought this may not be needed at all any more).
b) override both the create and createex in the listctrl class and add the subclassing there.
So the three functions should look like:
void FCListCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CListCtrl::PreSubclassWindow();
if(GetHeaderCtrl()) // Works for placed controls (non-dynamic creation)
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
}
BOOL FCListCtrl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bReturn = CListCtrl::Create(dwStyle, rect, pParentWnd, nID);
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
return bReturn;
}
BOOL FCListCtrl::CreateEx(DWORD dwExStyle, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bReturn = CListCtrl::CreateEx(dwExStyle, dwStyle, rect, pParentWnd, nID);
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
return bReturn;
}
Now anyone else can use the list control in any manner they like, and all they have to do is include the 4 files (two headerctrl and two listctrl files).
Works for me anyhow, hopefuly you'll find it usefull.
Thanks very much for the submission, I was just looking for the header control, this saved me time! The header ctrl is used as is but I noticed the web site listed in the header no longer exists, let me know if you want this updated.
Regards,
Adam
Adam Bialowas
Systems Engineer
Fugro Chance Inc.
|
|
|
|
|