I have had many, MANY questions asking how to use my MFC grid control in a view instead of in a dialog, so hopefully this will help.
The easiest way as I see it is as follows:
- Add a member variable of type
CGridCtrl*
to your view class:
CGridCtrl* m_pGrid;
- Initialise this to NULL in your view class' constructor:
CMyView::CMyView
{
m_pGrid = NULL;
}
- In the
CView
function OnInitialUpdate
, create a new CGridCtrl
object if the m_pGrid
is not NULL, and then create the CGridCtrl
window:
CMyView::OnInitialUpdate
{
CView::OnInitialUpdate();
if (m_pGrid == NULL)
{
m_pGrid = new CGridCtrl;
if (!m_pGrid ) return;
CRect rect;
GetClientRect(rect);
m_pGrid->Create(rect, this, 100);
m_pGrid->SetRowCount(50);
m_pGrid->SetColumnCount(10);
}
}
This allows the view to be reused (eg SDI situations).
- We want the grid to take up the whole of the view's client space, so add a handler to the
WM_SIZE
message for the view and edit the OnSize
function thus:
CMyView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_pGrid->GetSafeHwnd())
{
CRect rect;
GetClientRect(rect);
m_pGrid->MoveWindow(rect);
}
}
- Remember to delete the object when you are done:
CMyView::~CMyView
{
delete m_pGrid;
}
- You may want to also add an
OnCmdMsg
overide to your view class and let the grid control have first go at the messages (this will allow commands such as ID_EDIT_COPY
to be wired in automatically:
BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
if (m_pGrid && IsWindow(m_pGrid->m_hWnd))
if (m_pGrid->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
If you want print preview, then check out Koay Kah Hoe's article Print Previewing without the Document/View Framework.
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, 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. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.