📄 gridctrl.shtml
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Chris Maunder">
<TITLE>Controls - MFC Grid control (derived from CWnd)</TITLE>
</HEAD>
<!-- document properties -->
<body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000">
<!-- advertisment -->
<table WIDTH="100%"><tr WIDTH="100%"><td align=center><!--#exec cgi="/cgi/ads.cgi"--><td></tr></table>
<!-- Title -->
<CENTER><H3><FONT COLOR="#AOAO99">MFC Grid control (derived from CWnd)</FONT></H3></CENTER>
<HR>
This article was contributed by <A HREF="mailto:Chris.Maunder@cbr.clw.csiro.au">Chris Maunder</A>.
<P>Copyright © 1998 Chris Maunder</A>.
<!-- Image and download files -->
<P><IMG SRC="gridctrl.gif" ALT="gridctrl example image" BORDER=0 ALIGN=BOTTOM>
Download <A HREF="gridctrl.zip">source files</A> (40 Kb) or
<A HREF="gridctrl_demo.zip">sample project</A> (113 Kb).
<h4>Check out latest changes (version 1.06) <a href="gridctrl.shtml#New">here</a>.</h4>
<p>Environment: VC++ 5.0 (SP3) NT 4.0 (SP3), Win95 (OSR 2.1)<br>
<!-- TOC -->
<p>
<TABLE CELLSPACING=5 WIDTH="90%">
<tr>
<td><a href="gridctrl.shtml#Introduction">Introduction</a></td>
<td><a href="gridctrl.shtml#Appearance">General appearance and features</a></td>
<td><a href="gridctrl.shtml#Structures">Structures, defines and Messages</a></td>
</tr>
<tr>
<td><a href="gridctrl.shtml#Files">Files</a></td>
<td><a href="gridctrl.shtml#Colours">Colours</a></td>
<td><a href="gridctrl.shtml#Overrides">Protected overridable functions</a></td>
</tr>
<tr>
<td><a href="gridctrl.shtml#Constuction">Constuction</a></td>
<td><a href="gridctrl.shtml#CellInfo">General cell information</a></td>
<td><a href="gridctrl.shtml#Clipboard">Clipboard</a></td>
</tr>
<tr>
<td><a href="gridctrl.shtml#NumRowsCols">Number of rows and columns</a></td>
<td><a href="gridctrl.shtml#Operations">Operations</a></td>
<td><a href="gridctrl.shtml#Acknowledgements">Acknowledgements</a></td>
</tr>
<tr>
<td><a href="gridctrl.shtml#Sizing">Sizing and position functions</a></td>
<td><a href="gridctrl.shtml#Printing">Printing</a></td>
<td><a href="gridctrl.shtml#History">History</a></td>
</tr>
</table>
<br><br>
<!-- the article... -->
<h3><a name="Introduction">Introduction</a></h3>
<P>After pushing the CListCtrl to its limits in trying to display and edit
tabulated data I decided what was really needed was a dedicated grid control.
I started writing my own grid control from scratch but decided to save
time by modifying <A HREF="mailto:chinajoe@aol.com">Joe Willcoxson's</A>
free WorldCom grid control at <A HREF="http://users.aol.com/chinajoe/wcmfclib.html">
http://users.aol.com/chinajoe/wcmfclib.html</A>. I tore apert his code and
rebuilt it from the ground up in order to get it to do all the things I needed.
<P>The project started out as a simple clean up but very quickly ballooned
into a bit of a nightmare as I kept finding new features that I felt just
<I>had</I> to go into it. It has not been tested exhaustively - but I'm
fairly confident that it won't fall over too badly :). Joe was kind enough
to allow me to release the source with no strings attached (since it is
based on his code) but since the project was such a marathon I am placing
a couple of very minor conditions on the use of this code:
<br><br>
<CENTER><TABLE WIDTH="80%" border=1>
<TR>
<TD><I>This file may be redistributed unmodified by any means PROVIDING
it is not sold for profit without the authors written consent, and providing
that this notice and the authors name and all copyright notices remains
intact. This code may be used in compiled form in any way you desire with
the following conditions. If the source code in this file is used in any
commercial application then a statement along the lines of "Portions copyright
(c) Chris Maunder, 1998" must be included in the startup banner, "About"
box or printed documentation. The source code may not be compiled into
a standalone library and sold for profit. Besides - it's free to whoever
wants it anyway! <br><br><b>This software is provided "as is" without
express or implied warranty. Use it at your own risk!</b> <br>
Whilst I have made every effort to remove any undesirable "features",
I cannot be held responsible if it causes any damage or loss of time or
data.</I></TD>
</TR>
</TABLE></CENTER>
<br><br>
Hopefully that isn't too much to ask considering the amount of work that
went into this. If you <I>do</I> use it in a commercial application then
<B>please</B> send me an <A HREF="mailto:Chris.Maunder@cbr.clw.csiro.au">email</A>
letting me know. There's no point in me releasing and maintaining/upgrading
this thing if no one is gonna use it.
<p>The control features:
<ul>
<li>Cell selection using the mouse, with optional Control and Shift key
combinations. Selection can be disabled.
<li>Row and Column resizing. Sizing can be diabled for row, columns or both.
<li>Auto row or column sizing when dividers are double-clicked.
<li>Any number of fixed rows and columns.
<li>Individual cells can have separate text and background colours.
<li>Individaul cells can be marked "Read-Only"
<li>OLE Drag and drop.
<li>Ctrl-C, Ctrl-X and Ctrl-V perform clipboard copy, cut and paste, and Ctrl-A for
"Select All"
<li>In place editing of cell contents. If a character key is pressed while
a cell has focus, editing will start on that cell, and the arrow keys
will allow navigation to other keys. If the current focus cell is clicked
on, editing will start and the arrow keys will move the carot inside the
edit control. Editing can be disabled.
<li>Support for Microsoft intellimouse.
<li>Optional grid lines.
<li>Images in any cell
<li>Full printing support, for either a Doc/View environment (inc Print
preview) or a standalone dialog based app (no print preview).
<li>Optional "List mode", including full row selection and sort on column
header click.
<li>Numerous virtual functions to allow this control to be extended very easily.
<li>UNICODE support.
</ul>
The <A HREF="gridctrl_demo.zip">sample project</A> demonstrates most of the
features of the grid control.
<h3><a name="Files">Files</a></h3>
<p>To use the Grid control in your project you will need to add a number of
files to your project:
<br><br>
<TABLE CELLSPACING=0 WIDTH="90%">
<TR VALIGN=TOP>
<TD WIDTH="5%"> </td>
<TD WIDTH="45%"><tt><FONT COLOR="#990000">gridctrl.cpp, gridctrl.h</FONT></tt></TD>
<TD WIDTH="50%">Main grid control source and header files.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="5%"> </td>
<TD WIDTH="45%"><tt><FONT COLOR="#990000">CellRange.h</FONT></tt></TD>
<TD WIDTH="50%">Definition of CCellID and CCellRange helper classes.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="5%"> </td>
<TD WIDTH="45%"><tt><FONT COLOR="#990000">MemDC.h</FONT></tt></TD>
<TD WIDTH="50%"><a href="keithr@europa.com">Keith Rule</a>'s memory DC helper class.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="5%"> </td>
<TD WIDTH="45%"><tt><FONT COLOR="#990000">InPlaceEdit.cpp, InPlaceEdit.h</FONT></tt></TD>
<TD WIDTH="50%">In-place edit windows source and header files.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="5%"> </td>
<TD WIDTH="45%"><tt><FONT COLOR="#990000">GridDropTarget.cpp, GridDropTarget.h</FONT></tt></TD>
<TD WIDTH="50%">Grid control OLE drag and drop target.</TD>
</TR>
</table>
<h3><a name="Constuction">Constuction</a></h3>
OK - so how do you use it?
<P>The underlying class of the grid control is <B>CGridCtrl</B> which is
derived from CWnd. To use it, either use the MS Visual C++ dialog editor
to place a custom control on a dialog, and enter "MFCGridCtrl" (no quotes)
as the Class name, or use CGridCtrl::Create:
<PRE><FONT COLOR="#990000">
CGridCtrl(int nRows = 0, int nCols = 0, int nFixedRows = 0, int nFixedCols = 0);
BOOL Create(const RECT& rect, CWnd* parent, UINT nID,
DWORD dwStyle = WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VISIBLE);
void AFXAPI DDX_GridControl(CDataExchange* pDX, int nIDC, CGridCtrl&rControl);
</FONT></PRE>
<p>DDX_GridControl is used where a DDX_Control call is needed. It is necessary
to use DDX_GridControl instead of DDX_Control when creating the control via a
dialog template in order to ensure that the grid is correctly registered as a
drag and drop target. This is to avoid a strange COleDropTarget::Register
error that occurs in win95.
<h3><a name="NumRowsCols">Number of rows and columns</a></h3>
<TABLE CELLSPACING=0 WIDTH="90%" >
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetRowCount() const</FONT></PRE>
</TD>
<TD WIDTH="50%">Returns the number of rows (including fixed
rows)</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetColumnCount() const</FONT></PRE>
</TD>
<TD WIDTH="50%">Returns the number of columns (including fixed
columns)</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetFixedRowCount() const</FONT></PRE>
</TD>
<TD VALIGN=TOP WIDTH="50%">Returns the number of fixed rows</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetFixedColumnCount() const</FONT></PRE>
</TD>
<TD WIDTH="50%">Returns the number of fixed columns</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetRowCount(int nRows)</FONT></PRE>
</TD>
<TD WIDTH="50%">Sets the number of rows (including fixed rows),
Returning TRUE on success.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetColumnCount(int nCols)</FONT></PRE>
</TD>
<TD WIDTH="50%">Sets the number of columns (including fixed
columns), Returning TRUE on success.</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetFixedRowCount(int nFixedRows = 1)</FONT></PRE>
</TD>
<TD WIDTH="50%">Sets the number of fixed rows, returning TRUE on success.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetFixedColumnCount(int nFixedCols = 1)</FONT></PRE>
</TD><TD WIDTH="50%">Sets the number of columns, returning TRUE on success.</TD>
</TR>
</TABLE>
<h3><a name="Sizing">Sizing and position functions</a></h3>
<TABLE CELLSPACING=0 WIDTH="90%" >
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetRowHeight(int nRow) const</FONT></PRE>
</TD><TD WIDTH="50%">Gets the height of row nRow.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetRowHeight(int row, int height)</FONT></PRE>
</TD><TD WIDTH="50%">Sets the height of row nRow.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetColumnWidth(int nCol) const</FONT></PRE>
</TD><TD WIDTH="50%">Gets the width of column nCol</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">BOOL SetColumnWidth(int col, int height)</FONT></PRE>
</TD><TD WIDTH="50%">Sets the width of column nCol.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH="50%">
<PRE><FONT COLOR="#990000">int GetFixedRowHeight() const</FONT></PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -