Objective Grid : PART II Programmer’s Guide : Chapter 27 GridEx Feature Pack Extension : Excel-Like Selection
Excel-Like Selection
Excel-Like selection functionality is improved in Objective GridEx with the use of the CDrawingManager class from MFC's Feature Pack. One of these improvements takes advantage of the feature that draws transparent or semitransparent pixels, called alpha-blending.
In addition to alpha-blend drawing, other Excel-like implementations have been updated, including drawing for selection frame, drag and drop, Excel look and feel, and copy/cut and paste.
Please refer to Microsoft's MSDN class reference at http://msdn.microsoft.com/en-us/library/bb983843.aspx for more details about the CDrawingManager class
Implementation
All new features are implemented in an override of the CGXGridCore class in the file GXGridCoreEx.h. To make these new features applicable both for CGXGridWnd and CGXGridView-derived grids, the implementation uses a template class:
 
template<class T>
class CGXGridCoreEx : public T
The following instantiations are provided:
 
typedef CGXGridCoreEx<CGXGridView> CGXGridViewEx;
typedef CGXGridCoreEx<CGXGridWnd> CGXGridWndEx;
GridEx uses the CDrawingManager class to take advantage of its complex drawing algorithms available in all versions of the MFC Feature Pack. The example below shows how to implement alpha-blending in your GridEx application:
 
CDrawingManager dm(*pDC);
dm.HighlightRect(rectItem,
m_AlphaBlend.nPercentage,
m_AlphaBlend.clrTransparent,
m_AlphaBlend.nTolerance,
m_AlphaBlend.clrBlend);
Please refer to the CDrawingManager class description in Microsoft's MSDN Library for further details.
Setting an Excel-Like Selection in a Grid (for CGXGridView-Derived grid)
1. Add #include "GXGridCoreEx.h” in a View class derived from CGXGridViewEx, for example:
 
class CExcelGridView : public CGXGridViewEx
Or, to use tool tips, derive from the following template class:
 
class CExcelGridView : public CGXGridToolTipsEx<CGXGridViewEx>
2. In the function OnInitialUpdate(), call InitializeExcel().
Optionally, for more similarity with Excel, call EnableFormulaEngine() before calling CGXGridView::OnInitialUpdate().
3. To customize alpha-blending parameters:
Set all values using the function AlphaBlendParams():
 
virtual void AlphaBlendParams(int nPercentage = 50,
COLORREF clrTransparent = (COLORREF)-1,
int nTolerance = 0,
COLORREF clrBlend = RGB(236,234,245) );
For example, AlphaBlendParams(50,(COLORREF)-1,0,RGB(0,255,0));.
Set individual values using the specific functions:
 
void SetSelectionColor(COLORREF clrBlend)
{m_AlphaBlend.clrBlend = clrBlend;}
 
void SetSelectionColorTransparent(COLORREF clrTransparent)
{m_AlphaBlend.clrTransparent = clrTransparent;}
 
void SetSelectionPercentage(int nPercentage)
{m_AlphaBlend.nPercentage = nPercentage;}
 
void SetSelectionTolerance(int nTolerance)
{m_AlphaBlend.nTolerance = nTolerance;}
You can also change the frame color for a copy-cut selection, for instance, SetCopyCutSelectionColor(RGB(0,255,0));
Excel-Like Selection Drawing Behaviors
You can perform the following keyboard or mouse exercises with the selections in a grid application to view each drawing behavior.
Using the mouse: Selects the alpha-blend drawing over the selected range except for the current cell, and frames the selection with a black solid frame featuring a marker on the right bottom corner. The selection is expanded automatically to include the entire covered area if any cell from this area is in the selection.
Clicking a column header: Selects the alpha-blend drawing over the selected column, except for the header and the current cell that is set on row 1, and frames the selection with a black solid frame featuring a marker on the top right corner.
Clicking a row header: Selects the alpha-blend drawing over the selected row except for the header and the current cell that is set on column 1, and frames the selection with a black solid frame featuring a marker on the bottom left corner.
Using the Ctrl key: Selects a multi-range alpha-blend drawing over the selected range except for the current cell, and frames only the current cell with a thin solid rectangle.
Holding down the Shift key: Expands the alpha-blend drawing selection over the selected ranges, and frames the expanded range if there is no multi-range selection. The position of the current cell is not changed.
Selecting or unselecting the entire grid: Clicking on cell (0,0); for the selected table sets the current cell to (1,1) and frames all cells except headers.
Dragging and dropping: Drag and drop one selected range to either copy the selection or to move it if pressing Ctrl. This method moves all covered cells, similar to the funcionality in Excel. Note that you cannot drop onto a covered area, i.e. a range of cells that span across multiple cells.
Autofilling: Expand the selection by dragging the selection’s marker to copy data. This works for both individual cells as well as for column or row selections.
Selecting a range to copy, cut, or paste: When using the Copy or Cut functions via either keyboard shortcuts (Ctrl+C or Ctrl+X) or the Edit or Context menus, if implemented, the selected range is designated by a dashed rectangle frame. If the operation is Cut, the frame is removed. If the operation is Paste (using either Ctrl+V or the menu option) or Copy — and multiple paste is available — the frame is removed with the use of the Esc key. For both the Copy and Cut operations, the frame is removed and the data is pasted with the use of the Enter key; the frame is also removed when typing a character in any cell. Pasting onto a covered area is not allowed if more than one cell is selected for Copy or Cut.
Copying or cutting a multi-range selection: This feature goes beyond Excel functionality. Use the Ctrl key as described above to capture a multi-range selection in which each range is framed with a dashed rectangle. When the Paste operation is executed, the cells with data are shifted to exclude the empty cells between them, and the union range is selected.
Related Samples
Alpha-blending selection is demonstrated in the sample ExcelGrid.