Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC

Using comboboxes in the MFC Grid Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (47 votes)
8 Jan 2013CPOL 455.6K   8.2K   122  
Explains how to use comboboxes to edit cells in the MFC Grid Control
This patch to Chris Maunder's CGridCtrl class includes the following
changes.  Search for EFW in the code to locate them.  Most changes
contain notes or are pretty self-explanatory.

MemDC.h
-------
Added code to clear the bitmap background when created so that if used
by other classes that don't completely draw over their client area, we
don't see garbage showing through.

InPlaceEdit.cpp
---------------
Fixed EndEdit() so that it does not cause assertion errors and crashes
if called multiple times.  This can occur if a derived grid control is
used that validates its input and you click on a grid control scrollbar.
In the above situation, the edit control can receive a WM_KILLFOCUS
event after EndEdit() has been called but hasn't finished yet (i.e. it
hasn't returned from the pOwner->SendMessage() call yet).  This patch
will probably need applying to any other inplace controls that use the
same code in EndEdit().

GridCell.h
----------
Added new function prototype for PrintCell() to handle the printing of
cells.

GridCell.cpp
------------
Various changes to CGridCell::Draw() to make it draw cells better when
using alternate color schemes and to keep the cell focus highlight in
any mode.

Removed printing references as that's now done by
CGridCell::PrintCell().

Fixed the sort marker so that it doesn't draw out of bounds in cells
that are as tall or shorter than the font height.

Added DT_NOPREFIX when drawing text so that we can see '&' characters.

Changed CGridCell::GetTextExtent() so that it uses its current text to
figure the extent if NULL is passed in the 'str' parameter.  This was
done to support the new CGridCtrl::GetCellTextExtent() function.

GridCtrl.h
----------
Added new CGridCtrl::GetCellTextExtent() function to get the text extent
of the text currently in the specified cell.  This saves having to
retrieve the cell text and then call CGridCtrl::GetTextExtent().

Added CGridCtrl::SetItemTextFmt() and CGridCtrl::SetItemTextFmtID() to
allow the use of printf-style formatting codes when setting text and also
the use of resource string IDs.

Added various new data members to support shaded/unshaded printout and
user-definable page margins.  Also added the relevant support functions
to get and set the new data members.

Added a message handler for OnRButtonUp() so that it can forward the
message to the parent to invoke a right click context menu.

GridCtrl.cpp
------------
Added code where relevant to support the new data members and functions
noted above.

Set m_bAllowDragAndDrop to FALSE in the constructor.  This is how it was
in the last version and is how I prefer it.  Change it back if you
prefer it the other way.

Added some bug fixes reported by others from the prior release that
didn't look like they made it into the new version (flush OLE data
source clipboard, ignore Escape in OnChar() for instant cell editing).

Added code in several locations to fix problems with cell focus and
selection highlighting.

Added code and adjusted existing code where necessary to support the new
printing features noted above.

End of changes

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions