One straightforward way to integrate MFC-based projects with .NET libraries is to set the /clr compiler option and use C++/CLI. Stingray Objective Grid samples are provided with a /clr build configuration to demonstrate compatibility with this approach.
This section discusses two possible ways for integrating Objective Grid into a WinForm:
Use C++/CLI projects to employ Windows Forms and apply CGXGridWnd-based classes directly.
Create .NET controls wrapping classes from Objective Grid libraries and use these controls with any .NET language, particularly C#, VB, C++/CLI.
Simple samples that demonstrate these approaches are shipped with Stingray Studio:
<install_dir>\Samples\Grid\Integration with .NET\GridForm
<install_dir>\Samples\Grid\Integration with .NET\GridControl
More advanced samples are available in the Rogue Wave Knowledge Base kb.roguewave.com/kb/). Specifically, see Using Objective Grid with C++/CLI at kb.roguewave.com/kb/index.php?View=entry&EntryID=1400.
The procedures below reference Section 4.1.1, "Using the Objective Grid Static Libraries," in this chapter for more detailed information on how to perform particular steps.
To embed a grid in a WinForm:
Use the Visual Studio App Wizard to create a Visual C++\CLR\Windows Form Application project GridForm.
Add grid resources, as described in Step 2 of Section 4.1.1.
Add #include to the stdafx.h file, as described in Step 4 of Section 4.1.1.
Add GXInit() to the InitInstance() method, as described in Step 5 of Section 4.1.1.
Add the following immediately after class CmyApp (to initialize grid libraries):
CMyApp theApp; |
Add a member to class Form1:
private: CGXGridWnd *m_pGX; |
Initialize the grid, using this code:
private: System::Void Form1_Load(System::Objectˆ sender, System::EventArgsˆ e) { m_pGX = new CGXGridWnd(); if (!m_pGX) return; DWORD dwFlags = WS_TABSTOP | WS_BORDER | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL; CRect rect(panel1->ClientRectangle.Left, panel1->ClientRectangle.Top, panel1->ClientRectangle.Right, panel1->ClientRectangle.Bottom); BOOL bOk = m_pGX->Create(dwFlags, rect, CWnd::FromHandle((HWND)panel1->Handle.ToInt32()), 32000); m_pGX->Initialize(); m_pGX->GetParam()->SetSortRowsOnDblClk(TRUE); m_pGX->GetParam()->EnableMoveCols(TRUE); m_pGX->GetParam()->EnableMoveRows(TRUE); m_pGX->GetParam()->EnableSelection(TRUE); m_pGX->LockUpdate(TRUE); m_pGX->SetRowCount(15); m_pGX->SetColCount(5); m_pGX->SetStyleRange(CGXRange(1,1), CGXStyle().SetControl(GX_IDS_CTRL_PUSHBTN)); m_pGX->LockUpdate(FALSE); } |
Clean up memory in the destructor:
~Form1() { if(m_pGX) delete m_pGX; if (components) { delete components; } } |
Optionally, add _GXDLL to the Preprocessor definitions, as described in Step 4 of Section 4.1.3.
Build and run the project.
To embed a grid in a WinForm:
Use the Visual Studio App Wizard to create a Visual C++\CLR\Windows Form Control Library project Grid.
Add grid resources, as described in Step 2 of Section 4.1.1.
Add #include to the stdafx.h file, as described in Step 4 of Section 4.1.1.
Add the following immediately after namespace Grid {…}:
CMyApp theApp; |
Add this member to class GridControl:
private: CGXGridWnd *m_pGX; |
Iinitialize the grid, using this code:
private: System::Void GridControl_Load(System::Objectˆ sender, System::EventArgsˆ e) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); GXInit(); m_pGX = new CGXGridWnd(); if (!m_pGX) return; DWORD dwFlags = WS_TABSTOP | WS_BORDER | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL; CRect rect(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Right,ClientRectangle.Bottom); BOOL bOk = m_pGX->Create(dwFlags,rect, CWnd::FromHandle((HWND)this->Handle.ToInt32()),32000); m_pGX->Initialize(); m_pGX->GetParam()->SetSortRowsOnDblClk(TRUE); m_pGX->GetParam()->EnableMoveCols(TRUE); m_pGX->GetParam()->EnableMoveRows(TRUE); m_pGX->GetParam()->EnableSelection(TRUE); m_pGX->LockUpdate(TRUE); m_pGX->SetRowCount(15); m_pGX->SetColCount(5); m_pGX->LockUpdate(FALSE); } |
Expose the APIs to call from the parent program:
public: System::Void SetValueRange(int t, int l, int b, int r, Stringˆ value) { m_pGX->SetValueRange(CGXRange(t, l, b, r), value); } public: System::Void SetControl(int t, int l, int b, int r, WORD nControl) { m_pGX->SetStyleRange(CGXRange(t, l, b, r), CGXStyle().SetControl(nControl)); } |
Clean up memory, as described in Step 8 of Section 4.4.1.
Build the control.
Add a new project to the solution in any .NET language program; here, we'll use Other Languages |Visual C# |Windows Forms Application.
Open Form1.
Select Tools | Choose Toolbox Items, then click Browse. Navigate to and select Grid.dll built in Step 9.
For x64 Build Configurations:
For x64 build configurations (or "AnyCPU" on an x64 machine), any control built with /clr (or even /clr:pure) throws an error when you try to add it to the Toolbox; controls built only with /clr:safe are valid for the Toolbox. This is a current Visual Studio limitation. As a workaround, you can use a control built with x64 and /clr by adding code to WinForms manually, rather then using the Toolbox.
To do so, set the configuration to Win32, use the Toolbox to add the control built with Win32, and then set the x64 build configuration and build it. The reference should be set to the GridControl project, rather then to a particular assembly.
Alternatively, save the Windows Forms Designer generated code and use it for initialization of an x64 control in a project with an x64 build configuration, like so:
// Add a member to class Form1 private Grid.GridControl gridControl1; // Use following code to initialize the control this.gridControl1 = new Grid.GridControl(); this.gridControl1.Location = new System.Drawing.Point(77, 57); this.gridControl1.Name = "gridControl1"; this.gridControl1.Size = new System.Drawing.Size(350, 235); this.gridControl1.TabIndex = 0; |
Add a reference to the Grid project in the Windows Forms Application.
Initialize GridControl with the following code:
private void Form1_Load(object sender, EventArgs e) { this.gridControl1.SetValueRange(1, 1, 1, 1, "Value"); this.gridControl1.SetControl(2, 1, 2, 1, GX_IDS_CTRL_PUSHBTN); } |
To use an embedded pushbutton, you can add the following to class Form1:
private ushort GX_IDS_CTRL_PUSHBTN = 52514; |
Build and run the solution.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.