dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: CGridCtrl in CCtrlView

  1. #1
    Join Date
    Jan 2009
    Posts
    237

    CGridCtrl in CCtrlView

    I am trying to use CGridCtrl inside of CCtrlView, in an MDI app. And I wrote this:
    Code:
    class CTestGridView : public CCtrlView
    {
    protected: // create from serialization only
    	CTestGridView();
    	DECLARE_DYNCREATE(CTestGridView)
    
    // Attributes
    public:
    	CTestGridDoc* GetDocument() const;
    	CGridCtrl& GetGridCtrl() const {return *(CGridCtrl*)this;}
    ....
    }
    and in cpp:
    Code:
    CTestGridView::CTestGridView()
    	:CCtrlView(GRIDCTRL_CLASSNAME, AFX_WS_DEFAULT_VIEW)
    {
    	// TODO: add construction code here
    
    	WNDCLASS wndcls;
    	//HINSTANCE hInst = AfxGetInstanceHandle();
    	HINSTANCE hInst = AfxGetResourceHandle();
    	if(! (::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls)))
    	{
    		// otherwise we need to register a new class
    		wndcls.style			= CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    		wndcls.lpfnWndProc		= ::DefWindowProc;
    		wndcls.cbClsExtra		= wndcls.cbWndExtra = 0;
    		wndcls.hInstance		= hInst;
    		wndcls.hIcon			= NULL;
    #ifndef _WIN32_WCE_NO_CURSOR
    		wndcls.hCursor		  = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
    #else
    		wndcls.hCursor		  = 0;
    #endif
    		wndcls.hbrBackground	= (HBRUSH) (COLOR_3DFACE + 1);
    		wndcls.lpszMenuName		= NULL;
    		wndcls.lpszClassName	= GRIDCTRL_CLASSNAME;
    
    		if(! AfxRegisterClass(&wndcls))
    		{
    			AfxThrowResourceException();
    		}
    	}
    }
    Until here, it goes without problem ... but when I am trying to use CGridCtrl object inside, I get a crash:
    Code:
    void CTestGridView::OnInitialUpdate() 
    {
    	CCtrlView::OnInitialUpdate();
    	
    	CGridCtrl& GridCtrl = (CGridCtrl&)GetGridCtrl();
    	GridCtrl.SetRowCount(1); // <-- crash
    }
    and the error is:
    Code:
    Unhandled exception at 0x77ce0ebc (mfc90d.dll) in TestGrid.exe: 0xC0000005: Access violation reading location 0xfeeefeee.
    Code:
    	if (!AfxIsValidAddress(pOb, pOb->GetRuntimeClass()->m_nObjectSize, FALSE))
    	{
    		TRACE(traceAppMsg, 0, "ASSERT_VALID fails with illegal pointer.\n");
    		if (AfxAssertFailedLine(lpszFileName, nLine))
    			AfxDebugBreak();
    		return;     // quick escape
    	}
    This error has been triggered when grid is trying to call:
    Code:
    	TRY
    	{
    		m_arRowHeights.SetSize(nRows); // <-- here is firing the crash
    Is obvious that I done something wrong ... but what ? It is possible what I am trying to do ?

    Thank you for any hint/help in solving my task.


    P.S. I attach an test app that illustrate the problem.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,195

    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:
    1. add a member of type CGridCtrl to view class (not necessary to be derived from CCtrlView);
    2. call CGridCtrl::Create, in WM_CREATE handler of view class;
    3. handle WM_SIZE message in view class and resize the grid control.
    Last edited by ovidiucucu; February 20th, 2018 at 03:22 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,195

    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.
    }
    Last edited by ovidiucucu; February 20th, 2018 at 04:10 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  4. #4
    Join Date
    Jan 2009
    Posts
    237

    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.
    Last edited by mesajflaviu; February 21st, 2018 at 10:32 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)