本文介绍了当我在类范围中声明cpen变量并在ondraw中使用它时MFC应用程序崩溃(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试在类范围内声明一个CPen对象并在OnDraw(...)中使用它。但是当我运行它时我的应用程序崩溃。如果我在OnDraw中声明对象(... )它工作正常。任何人都可以让我知道如果在类范围内声明CPen对象,我还需要任何额外的东西吗?在此先感谢。



我尝试过:



Hi All, I'm trying to declare a CPen object in class scope and using it in OnDraw(...).But my application is crashing when I run it.If I declare the object in OnDraw(...) it is working fine. Can anyone please let me know do I need to any extra thing if declare CPen object in class scope? Thanks in advance.

What I have tried:

xxxxx.h
private:
  CPen cPen;


xxxxx.cpp

void CMFCApplication2View::OnDraw(CDC* pDC)
{
        //CPen cPen;  If I do it here its working fine.
	CPen* pOldPen = NULL;

	BOOL bCPen = cPen.CreatePen(PS_SOLID, 10, RGB(255, 0, 0));

	
	if (bCPen)
	{
		pOldPen = pDC->SelectObject(&cPen);

		pDC->MoveTo(95, 125);
		pDC->LineTo(230, 125);

		pDC->SelectObject(pOldPen);
		//cPen.DeleteObject();
	}
}

推荐答案

CMFCApplication2View::CMFCApplication2View()
{
    // Other initialisation goes here

    cPen.CreatePen(PS_SOLID, 10, RGB(255, 0, 0));
}

void CMFCApplication2View::OnDraw(CDC* pDC)
{
    CPen* pOldPen = pDC->SelectObject(&cPen);

    // Perform drawing here

    pDC->SelectObject(pOldPen);
}


BOOL CPen = cPen.CreatePen(PS_SOLID, 10, RGB(255, 0, 0));



这个陈述到底是什么意思?您正在将类名称( CPen )重新声明为 BOOL ean变量。


What on earth is that statement supposed to be? You are redeclaring a class name (CPen) as a BOOLean variable.


这篇关于当我在类范围中声明cpen变量并在ondraw中使用它时MFC应用程序崩溃(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:53