⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grid_list_control.shtml.htm

📁 mfc资料集合5
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>CListCtrl - Grid List Control</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=catalyst"><IMG SRC="../banners/catalyst.jpg" tppabs="http://www.codeguru.com/banners/catalyst.jpg" HEIGHT=60 WIDTH=468 ALT="Catalyst Development" BORDER=2></A><BR><SMALL><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=catalyst">Click here for Free ActiveX Control</A></SMALL><td>
</tr>
</table>


<CENTER>
<H3>
<FONT COLOR="#AOAO99">Grid List Control</FONT></H3></CENTER>
<HR>


This article was contributed by <A HREF="mailto:rex@surfutah.com">Rex Myer</A>.



<p>This article illustrates how to use the new (v4.72) common control 
feature of custom drawn controls in connection with the list control to create
a grid list control.  This is part of the whole implementation for the grid list control.
This article covers the custom drawing to indicate the cell with focus and 
keyboard navigation to cursor that focused cell.
The other related article, "<A HREF="multiline_edit_subitems.shtml.htm" tppabs="http://www.codeguru.com/listview/multiline_edit_subitems.shtml">Multiline Editable Subitems</A>", 
covers the editing of the subitems.

<P><IMG SRC="gridlist.gif" tppabs="http://www.codeguru.com/listview/gridlist.gif" BORDER=1>
<P>
You can download the full grid list control implementation at the bottom of
the page. Since it uses the newest common control features, you will need
to download the <A HREF="http://www.microsoft.com/msdn/downloads/files/40Comupd.htm">common
control redistributable</A> in order to run the sample. In order to build 
the sample, you will also
need the <A HREF="http://www.microsoft.com/msdn/sdk/bldenv.htm">Platform
SDK build environment</A>.&nbsp; There is also <A HREF="http://www.microsoft.com/msdn/sdk/inetsdk/help/itt/CommCtls/CommCtls.htm#book_cc">documentation</A>
on the new common control features there as well.&nbsp; All you need from
the build environment are the CommCtrl.h and the ComCtl32.lib files. Make
these available to
your VC++ build environment.&nbsp; I did this by backing up my old files
(just in case) and copying these two files into my include and lib directories 
respectively. 
<BR>In this explanation, I call out certain functions from the grid 
list control sample which pertain to this article.
<H4>
Step 1: Derive a class from CListCtrl</H4>
I derived CGridListCtrl from the CListCtrl.
I added a member variable to keep track of the current column
which the user is interacting with. This variable is the column number
of the item also called the column order.&nbsp; Keep in mind that this
may not be the same as the
subitem when dragging the columns and changing their order.
<PRE><TT><FONT COLOR="#990000">// Attributes
public:
&nbsp;&nbsp;&nbsp; // The current subitem or column number which is order based.&nbsp;
&nbsp;&nbsp;&nbsp; int m_CurSubItem;</FONT></TT></PRE>

<H4>
Step 2: Prepare the control for grid usage</H4>
We must be sure that the list control is in report view and that the
edit sublabel is turned off (this is for the editing subitem implementation).  
We also want full row select and to drag and drop columns.
This function should be called by the parent of the grid list control in its
initialize function (i.e. OnInitDialog).  The reason this block of code is not 
in PreCreate is that the exended list view styles must be set after a control 
is created.  
<PRE><TT><FONT COLOR="#990000">BOOL CGridListCtrl::PrepareControl()
    {
    ASSERT( m_hWnd );
    DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE); 
    dwStyle &= ~(LVS_TYPEMASK);
    dwStyle &= ~(LVS_EDITLABELS);
 
    // Make sure we have report view.
    SetWindowLong( m_hWnd, GWL_STYLE, dwStyle | LVS_REPORT );

    // Enable the full row selection and the drag drop of headers.
    DWORD styles = LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP ;
    // Use macro since this is new and not in MFC.
    ListView_SetExtendedListViewStyleEx(m_hWnd, styles, styles );
    return TRUE;
    }
</PRE></TT></FONT>

<H4>
Step 2: Enable user navigation</H4>
The user can navigate the focused cell through the
mouse or keyboard.&nbsp; As previously mentioned we keep track of the 
current column number or column order.  The user navigation simply updates
this member variable and invalidates the item.  
<p>For more information on the cell navigation with the mouse see the article, 
"Grid List Control." For keyboard navigation, the user can use the right and 
left arrow to move the cell cursor.  We can do this in the PreTranslateMessage 
override method.  We only handle the messages if the grid list control has the 
focus.  The pretranslated messages may also be for the subitem while it is being 
edited, and we don't want to interfere with its operation.
<p>
MakeColumnVisible is called to make sure that the focused cell is fully exposed
by scrolling as necessary. Its implementation follows.  The Header_OrderToIndex macro
is called to translate the order based m_CurSubItem to an index.
The up and down arrows are already handled sufficiently by the list control itself.
This also includes the nondestructive edit activation via F2.
<p>The value of -1 for m_CurSubItem indicates that there is no current subitem 
focus.

<PRE><TT><FONT COLOR="#990000">BOOL CGridListCtrl::PreTranslateMessage(MSG* pMsg) 
    {
    if(pMsg->message == WM_KEYDOWN)
        {
        // Handle the keystrokes for the left and right keys
        // to move the cell selection left and right.
        // Handle F2 to commence edit mode from the keyboard.
        // Only handle these if the grid control has the focus.
        // (Messages also come through here for the edit control
        // and we don't want them.
        if( this == GetFocus() )
            {
            switch( pMsg->wParam )
                {
                case VK_LEFT:
                    {
                    // Decrement the order number.
                    m_CurSubItem--;
                    if( m_CurSubItem < -1 ) 
                        {
                        // This indicates that the whole row is selected and F2 means nothing.
                        m_CurSubItem = -1;
                        }
                    else
                        {
                        CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
                        // Make the column visible.
                        // We have to take into account that the header
                        // may be reordered.
                        MakeColumnVisible( Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem ) );
                        // Invalidate the item.
                        int iItem = GetNextItem( -1, LVNI_FOCUSED );
                        if( iItem != -1 )
                            {
                            CRect rcBounds;
                            GetItemRect(iItem, rcBounds, LVIR_BOUNDS);
                            InvalidateRect( &rcBounds );
                            }
                        }
                    }
                    return TRUE;
                case VK_RIGHT:
                    {
                    // Increment the order number.
                    m_CurSubItem++;
                    CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
                    int nColumnCount = pHeader->GetItemCount();
                    // Don't go beyond the last column.
                    if( m_CurSubItem > nColumnCount -1 ) 
                        {
                        m_CurSubItem = nColumnCount-1;
                        }
                    else
                        {
                        // We have to take into account that the header
                        // may be reordered.
                        MakeColumnVisible( Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem ) );
                        int iItem = GetNextItem( -1, LVNI_FOCUSED );
                        // Invalidate the item.
                        if( iItem != -1 )
                            {
                            CRect rcBounds;
                            GetItemRect(iItem, rcBounds, LVIR_BOUNDS);
                            InvalidateRect( &rcBounds );
                            }
                        }
                    }
                    return TRUE;
                case VK_F2: // Enter nondestructive edit mode.
                    {
                    int iItem = GetNextItem( -1, LVNI_FOCUSED );
                    if( m_CurSubItem != -1 && iItem != -1 )
                        {
                        // Send Notification to parent of ListView ctrl
                        CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
                        CString str;
                        // We have to take into account that the header
                        // may be reordered.
                        str = GetItemText( iItem, Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem ) );
                        LV_DISPINFO dispinfo;
                        dispinfo.hdr.hwndFrom = m_hWnd;
                        dispinfo.hdr.idFrom = GetDlgCtrlID();
                        dispinfo.hdr.code = LVN_BEGINLABELEDIT;
                        
                        dispinfo.item.mask = LVIF_TEXT;
                        dispinfo.item.iItem = iItem;
                        // We have to take into account that the header
                        // may be reordered.
                        dispinfo.item.iSubItem = Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem );
                        dispinfo.item.pszText = (LPTSTR)((LPCTSTR)str);
                        dispinfo.item.cchTextMax = str.GetLength();
                        // Send message to the parent that we are ready to edit.
                        GetParent()->SendMessage( WM_NOTIFY, GetDlgCtrlID(), 
                            (LPARAM)&dispinfo );
                        }
                    }
                    break;
                default:
                    break;
                }
            }
        }
    
    return CListCtrl::PreTranslateMessage(pMsg);
    }

void CGridListCtrl::MakeColumnVisible(int nCol)
    {
    if( nCol < 0 )
        return;
    // Get the order array to total the column offset.
    CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
    int colcount = pHeader->GetItemCount();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -