Windows interface: clistctrol inheritance class clistctrolex with "delete element" and "sort by column by character comparison"

Source: Internet
Author: User

1. header file implementation:

# If! Defined (afx_listctrlex_h1_fd8138ba_775e_449?acca_b5265ec70dbc=included _)
# Define afx_listctrlex_h1_fd8138ba_775e_449?acca_b5265ec70dbc=included _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000

//
// Listctrlex. h: header file
//

//////////////////////////////////////// /////////////////////////////////////
// Clistctrlex window
//
// 1. Supported sorting: strings are case-insensitive. The unique lparam parameter must be set in advance.
// 2. Support for deletion: The shortcut key "delete" is used to delete selected entries (Multiple choices ).
//

Class clistctrlex: Public clistctrl
{
Public:
Bool enablefunc (DWORD dwmask); // enable the function (enable it to take effect. No function is enabled by default)

Public:
Static const DWORD funcall; // functional mask: All functions 0 xffffffff
Static const DWORD funcsort; // functional mask: Sorting supports 0x00000001
Static const DWORD funcdelete; // function mask: delete 0x00000002
 
Protected:
// Comparison function of sorting
Static int callback comparebyparam (lparam lparam1, lparam lparam2, lparam lparamsort );
 
// Member variable: feature mask (no function is enabled by default)
DWORD m_dwfuncmask;
// Member variable: The ascending or descending order of records
Bool m_bascending;
// Member variable: column for record sorting
Int m_isortcol;

// Construction
Public:
Clistctrlex ();

// Attributes
Public:

// Operations
Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (clistctrlex)
//} Afx_virtual

// Implementation
Public:
Virtual ~ Clistctrlex ();

// Generated message map Functions
Protected:
// {Afx_msg (clistctrlex)
Afx_msg void oncolumnclick (nmhdr * pnmhdr, lresult * presult );
Afx_msg void onkeydown (uint nchar, uint nrepcnt, uint nflags );
//} Afx_msg

Declare_message_map ()
};

//////////////////////////////////////// /////////////////////////////////////

// {Afx_insert_location }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.

# Endif //! Defined (afx_listctrlex_h1_fd8138ba_775e_449?acca_b5265ec70dbc=included _)

2. Class implementation file:

// Listctrlex. cpp: implementation file
//

# Include "stdafx. H"
# Include "listctrlex. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// /////////////////////////////////////
// Clistctrlex

Const DWORD clistctrlex: funcall = 0 xffffffff;
Const DWORD clistctrlex: funcsort = 0x00000001;
Const DWORD clistctrlex: funcdelete = 0x00000002;
 

Clistctrlex: clistctrlex (): m_dwfuncmask (0), m_bascending (true), m_isortcol (-2)
{
}

Clistctrlex ::~ Clistctrlex ()
{
}

Begin_message_map (clistctrlex, clistctrl)
// {Afx_msg_map (clistctrlex)
On_policy_reflect (lvn_columnclick, oncolumnclick)
On_wm_keydown ()
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Clistctrlex message handlers

Bool clistctrlex: enablefunc (DWORD dwmask)
{
M_dwfuncmask = m_dwfuncmask | dwmask;
Return true;
}

Int callback clistctrlex: comparebyparam (lparam lparam1, lparam lparam2, lparam lparamsort)
{
Clistctrlex * plistview = (clistctrlex *) lparamsort;
 
Int icol = plistview-> m_isortcol;
Int ifactor = (plistview-> m_bascending )? 1: (-1 );

If (-1) = icol)
{
Return ifactor * (lparam1-lparam2 );
}
Else
{
Lvfindinfo Info;
Info. Flags = lvfi_param;
Info. lparam = lparam1;
Int iindex1 = plistview-> finditem (& info );
Info. lparam = lparam2;
Int iindex2 = plistview-> finditem (& info );


Cstring stritem1 = plistview-> getitemtext (iindex1, icol );
Cstring stritem2 = plistview-> getitemtext (iindex2, icol );

Return ifactor * _ tcsicmp (stritem1, stritem2 );
}
}

Void clistctrlex: oncolumnclick (nmhdr * pnmhdr, lresult * presult)
{
Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
// Todo: add your control notification handler code here
If (m_dwfuncmask & clistctrlex: funcsort)
{
// Obtain the sorted Column
Int icolumn = pnmlistview-> isubitem;

// Set the ascending or descending order
If (m_isortcol = icolumn)
{
M_bascending =! M_bascending;
}
Else
{
M_bascending = true;
M_isortcol = icolumn;
}

Sortitems (comparebyparam, (lparam) This );
}

* Presult = 0;
}

Void clistctrlex: onkeydown (uint nchar, uint nrepcnt, uint nflags)
{
// Todo: add your message handler code here and/or call default
If (nchar = vk_delete)
& (M_dwfuncmask & clistctrlex: funcdelete)
)
{
Cdwordarray arrdel;
Position Pos = getfirstselecteditemposition ();
// Process multiple choices cyclically
While (POS)
{
Int nitem = getnextselecteditem (POS );
// Record to delete the selected
// We can not delete them here
Arrdel. Add (nitem );
}

For (INT I = 0; I <arrdel. getsize (); I ++)
{
Deleteitem (arrdel [arrdel. getsize ()-1-I]);
}
}
 
Clistctrl: onkeydown (nchar, nrepcnt, nflags );
}

3. Use this class in a single document: (you can use this class directly or use classwizard to create a clistctrlex derived class)

//////////////////////////////////////// /////////////////////////////////////
// Csd_filescannerview message handlers

Int csd_filescannerview: oncreate (maid)
{
If (cview: oncreate (lpcreatestruct) =-1)
Return-1;
 
// Create listctrl
Rect;
Getclientrect (& rect );
M_listctrl.create (ws_child | ws_visible | lvs_report | lvs_autoarrange | lvs_showselalways
, Rect
, This
, 1
);
M_listctrl.setextendedstyle (lvs_ex_fullrowselect );
M_listctrl.enablefunc (clistctrlex: funcsort );

// Set Columns
Int iwidth = rect. Right-rect. Left;
M_listctrl.insertcolumn (0, _ T ("# No."), lvcfmt_right, 60 );
M_listctrl.insertcolumn (1, _ T ("file"), lvcfmt_left, iwidth * 6/10 );
M_listctrl.insertcolumn (2, _ T ("status"), lvcfmt_left, 80 );
M_listctrl.insertcolumn (3, _ T ("Last modify time"), lvcfmt_left, 130 );
M_listctrl.insertcolumn (4, _ T ("size (bytes)"), lvcfmt_right, iwidth * 1/10 );

Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.