Home arrow C++ arrow Page 2 - A Custom Drawn List Control in C++
C++

A Custom Drawn List Control in C++


In this article Vitali takes the standard C++ list control and modifies it to extend its functionality. The result is a useful, flexible list control.

Author Info:
By: Vitali Halershtein
Rating: 4 stars4 stars4 stars4 stars4 stars / 42
September 13, 2003
TABLE OF CONTENTS:
  1. · A Custom Drawn List Control in C++
  2. · The Article
  3. · Conclusion

print this article
SEARCH DEVARTICLES

A Custom Drawn List Control in C++ - The Article
(Page 2 of 3 )

Features

My new controls class name is ClistCtrlEx. My control contains the following features:

Multicolumn sorting
Multicolumn filtering
Column data type auto detection
Column coloring and dragging
Storing/loading control state

Hereís a quick screenshot of my list control in action:

  1. The first column is sorted in ascending order
  2. The second column is sorted in descending order and is filtered
  3. The third column is simply filtered

Other features are accessible via the context menu. The context menu can be displayed by right clicking on the columns, as shown below:

  1. Show/hide columns

  1. Automatic find
  2. Automatic filter
  3. Changing sorted columns background color
  4. Reset all settings to default

The following settings are implemented using the lists control style:

  1. Show/hide gridlines
  2. One click row activation
  3. Column dragging

The control also allows saving/loading of all of the features above.

How To Us the Control

To integrate my list control into your C++ project, hereís what you need to do:

  1. Insert a project into the workspace and setup dependencies from the project -> dependencies menu.
  2. Include the header file ListCtrlEx.h into the necessary files
  3. Add the following definition into your stdafx.h file:

    #define LISTCTRLEXDLL   __declspec( dllimport )

  4. Replace the CListCtrl variable with CListCtrlEx. All methods of the new control have names identical to the same method names of the standard CListCtrl.

The only exception is the following:

    void BeginTrans();
    void CommitTrans();
    void ApplySettings();
    void LoadSettings();
    void SetIniFileName(CString& sFileName);
    void SetDifferences(int aiCol, BOOL bSearch = TRUE);

If you want to insert a large number of rows into the control use BeginnTrans() and CommitTrans() before and after the insertion.

Use SetIniFileName(CString& sFileName) and LoadSettings() to set the full path to the file where the control will store and load its settings from. Call these methods after the controls creation. I.e. If you use a CDialog derived class you can call it from OnInitDialog. For the CFormView derived classes, make this call from OnInitialUpdate.

Tips and Tricks

In rare cases you may need to call ApplySettings() to force the control to store its current settings. Now that Iíve described everything you need to know to make my list control work, hereís some sample code:

CString ini_file = "test.ini";

m_List.SetIniFileName(ini_file);
m_List.InsertColumn( 0, "Column 1", LVCFMT_LEFT, 100, 0 );
m_List.InsertColumn( 1, "Column Numbers", LVCFMT_LEFT, 100, 1 );
m_List.InsertColumn( 2, "Column Dates", LVCFMT_LEFT, 100, 2 );
m_List.BeginTrans();

for(int i=0; i<1000; i++)
{
    CString txt;
    txt.Format("This is item %d", i);
    int item = m_List.InsertItem( 0, txt );
    txt.Format("%d", i);
    m_List.SetItemText(item, 1, txt);
   
    CTime tm = CTime::GetCurrentTime();
    m_List.SetItemText(item, 2, tm.Format("%d/%m/%Y"));
}

m_List.CommitTrans();

Usage
This software is released into the public domain. You are free to use it in any way you like. You should keep my author text in the header files. If you modify or extend my control, please consider posting the new code here in the DevArticles forum for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials