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

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    399

    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

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
  •  





Click Here to Expand Forum to Full Width

Featured