Objective Grid: Using the grid as a registered dialog control

Article ID: 525
Last updated: 16 Apr, 2018
Article ID: 525
Last updated: 16 Apr, 2018
Revision: 3
Views: 1967
Posted: 11 Jan, 2001
by Meltreger B.
Updated: 16 Apr, 2018
by Meltreger B.

Problem


How do I use the grid as a registered dialog control in a dialog template?


Cause



Action


Follow these steps to embed a registered grid control in a dialog. Please note that there is also the possibility to use SubclassDlgItem() instead.

Create a new class with ClassWizard. Use "generic CWnd" as Class Type.

Next, using a text editor, change the derivation in your window class, in both the .h and .cpp files. All occurrences of CWnd should be replaced with CGXGridWnd.

Insert the line DECLARE_REGISTER( ) into your class definition.

class CDerivedGridWnd : public CGXGridWnd
{
DECLARE_REGISTER( );

...

Insert the line IMPLEMENT_REGISTER(CYourClassName, StyleBits, 0, 0, 0) into your class implementation file.

// dergrid.cpp : implementation file
//

#include "stdafx.h"
#include "sample1.h"
#include "dergrid.h"

#ifdef _DEBUG

#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_REGISTER(CDerivedGridWnd, CS_DBLCLKS, 0, 0, 0);

////////////////////////////////////////////////////////////////
// CderivedGridWnd
CDerivedGridWnd::CDerivedGridWnd( )
{

...

Create or open your dialog template. Choose the user control icon, drag it into the dialog and open its property page. Enter your class name into the class box and specify the style bits for the grid:

  • 0x50b10000 to display both scrollbars and a border
  • 0x50810000 to display no scrollbars but a border
  • See the definitions for windows style bits in your windows.h header file.

If you created a new dialog, you should now create your dialog class with ClassWizard.

Add a method to your dialog class to gain access to the grid control object.

Add a prototype to the header file.

// dergrdlg.h : header file
//
/////////////////////////////////////////////////////////////

// CDerGridDialog dialog
class CDerivedGridWnd;
class CDerGridDialog : public Cdialog
{
public:

// Construction
CDerGridDialog(CWnd* pParent = NULL);

// Dialog Data

//{{AFX_DATA(CDerGridDialog)

enum { IDD = IDD_GRIDDIALOG };

// NOTE: the ClassWizard will add data members here

//}}AFX_DATA

CDerivedGridWnd* GetDerivedGridWnd( );

...

Add the implementation to your dialog class implementation file:

CDerivedGridWnd* CDerGridDialog::GetDerivedGridWnd( )
{
return (CDerivedGridWnd*) GetDlgItem(IDC_GRIDSAMPLE);
}

Add the OnInitDialog() member to you dialog class (WM_INITDIALOG-message). In OnInitDialog(), you can initialize the grid:

////////////////////////////////////////////////////////////////
// CDerGridDialog message handlers
BOOL CDerGridDialog::OnInitDialog( )
{
CDialog::OnInitDialog( );

CDerivedGridWnd& grid = *GetDerivedGridWnd( );

grid.Initialize( );
grid.GetParam( )->EnableUndo(FALSE);
grid.SetRowCount(100);
grid.SetColCount(20);
grid.GetParam( )->EnableUndo(TRUE);

return TRUE;

}

Add DDV_GXGridWnd() in the DoDataExchange() method:

DDV_GXGridWnd(pDX, GetDerivedGridWmd());

You must register the grid class. This is best done in InitInstance():

BOOL CSample1App::InitInstance( )
{
...

GXInit( );
CDerivedGridWnd::RegisterClass( );
Enable3dControls( );

...

}

This article was:   Helpful | Not helpful
Report an issue
Article ID: 525
Last updated: 16 Apr, 2018
Revision: 3
Views: 1967
Posted: 11 Jan, 2001 by Meltreger B.
Updated: 16 Apr, 2018 by Meltreger B.

Others in this category