void CMy004View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar=='s')
{
CDC *pDC;
CBitmap bitmap5;
bitmap5.LoadBitmap(IDB_BITMAP1); BITMAP bmp;
bitmap5.GetBitmap(&bmp); CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC); dcCompatible.SelectObject(&bitmap5);
pDC->StretchBlt(280,20,20,20,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
}
我这段代码为什么会在dcCompatible.CreateCompatibleDC(pDC);内存报错啊?
{
// TODO: Add your message handler code here and/or call default
if(nChar=='s')
{
CDC *pDC;
CBitmap bitmap5;
bitmap5.LoadBitmap(IDB_BITMAP1); BITMAP bmp;
bitmap5.GetBitmap(&bmp); CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC); dcCompatible.SelectObject(&bitmap5);
pDC->StretchBlt(280,20,20,20,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
}
我这段代码为什么会在dcCompatible.CreateCompatibleDC(pDC);内存报错啊?
=========
你前面定义的CDC *pDC;这个pDC是一个野指针,当然不行了。
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
//pDC未初始化,请将pDC指向具体的DC,如果CreateCompatibleDC(NULL),那么使用的是桌面DC
CDC *pDC=GetDC(NULL);
因为你是在视图类中调用的,就用视图类的DC。