Re: CGridCtrl in CCtrlView
Code:
class CTestGridView : public CCtrlView
{
// ...
CGridCtrl& GetGridCtrl() const {return *(CGridCtrl*)this;} // <--bad
// ...
}
It crashes because neither CCtrlView nor CTestGridView has a SetRowCount method.
You tried to apply a "trick" used in another CCtrlView-derived classes. It works there because the control classes (CListCtrl, CTreeCtrl, etc.) has methods that only send messages.
Example:
Code:
_AFXCMN_INLINE void CListCtrl::SetItemCount(_In_ int nItems)
{ ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, LVM_SETITEMCOUNT, nItems, 0); }
In case of CGridCtrl do the following:
- add a member of type CGridCtrl to view class (not necessary to be derived from CCtrlView);
- call CGridCtrl::Create, in WM_CREATE handler of view class;
- handle WM_SIZE message in view class and resize the grid control.
Re: CGridCtrl in CCtrlView
Here is the code:
Code:
class CTestGridView : public CView // not CCtrlView
{
CGridCtrl m_gridCtrl;
// ...
};
Code:
#define GRID_CTRL_ID 1000
// ...
BEGIN_MESSAGE_MAP(CTestGridView, CView)
// ...
ON_WM_CREATE()
ON_WM_SIZE()
END_MESSAGE_MAP()
// ...
int CTestGridView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
VERIFY(m_gridCtrl.Create(CRect(), this, GRID_CTRL_ID, WS_CHILD | WS_VISIBLE));
return 0;
}
void CTestGridView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_gridCtrl.GetSafeHwnd())
{
m_gridCtrl.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOACTIVATE);
}
}
void CTestGridView::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_gridCtrl.SetRowCount(10);
m_gridCtrl.SetColumnCount(4);
// ... and so on.
}
Re: CGridCtrl in CCtrlView
So, I should understand that I cannot use CGridCtrl inside of CCtrlView in this way ...
I am using right now this technique, to resize an CGridCtrl object over a CView, but I have met a little bug in this case: when I have CDockablePane on an CChildFrame, and CGridCtrl over CView, because CChildFrame are somehow restored under CGridCtrl ... , yes, this is weird, and this happen even if all child frames are maximized, I could switch between open child frames, as I said before, because some CChildFrame are restored ... I know, it is strange, but this is sort of difficult to reproduce this bug ... I will try to made a test app that illustrate this behavior ...
I would like to use the solution from the first post, but I guess it is impossible ...
I will come back with another test app. Thank you.