15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Compatible DCs

Christopher Hudson -- christopher.hudson@magnet.mt
Wednesday, December 04, 1996


Environment: NT 4.0 sp1, VC4.2b

I am having difficulties with creating a memory DC, then copying it to the paint 
DC. If create the OnPaint funtion below, then everything works fine.

// Working OnPaint not using Memory DCs
void CLrView::OnPaint() 
{
	TRACE("Painting\n");

	CPaintDC MemDC(this); // device context for painting
		
	CLrDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// Get pointer to image
	CImage* l_pImage = pDoc->GetImage();
	if (l_pImage)
	{
		// Display Image
		int XPos = GetScrollPos(SB_HORZ);
		int YPos = GetScrollPos(SB_VERT);
		l_pImage->DisplayImage(this,&MemDC,XPos,YPos);

		// Display Regions
		if (!pDoc->GetRegionList()->IsEmpty())
		{
			MemDC.SetBkMode(TRANSPARENT);

			// Iterate through Regions
			POSITION pos;
			pos = pDoc->GetRegionList()->GetHeadPosition();
			while (pos)
			{
				CBrush NewBrush(HS_BDIAGONAL,RGB(255,0,0));
				MemDC.SelectObject(NewBrush);
				MemDC.SetBrushOrg(CPoint(0,0));
				TRACE("Painting Region\n");
			        MemDC.PaintRgn((CRgn*)pDoc->GetRegionList()
                                                ->GetAt(pos));
				pDoc->GetRegionList()->GetNext(pos);
				NewBrush.DeleteObject();
			}
		}
	}
}


But if I try to paint everything to a compatible memory DC then copy this to the 
actual dc it does not paint anything, but just leaves it blank  

// Not working OnPaint using CompatibleDCs
void CLrView::OnPaint() 
{
	TRACE("Painting\n");

	CPaintDC dc(this); // device context for painting
	
	// Create memory DC for creating the image on.
	CDC MemDC;
	MemDC.CreateCompatibleDC(&dc);
	OnPrepareDC(&MemDC);
	
	int ImageWidth = 0;
	int ImageHeight = 0;
	CLrDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// Get pointer to image
	CImage* l_pImage = pDoc->GetImage();
	if (l_pImage)
	{
		// Display Image
		int XPos = GetScrollPos(SB_HORZ);
		int YPos = GetScrollPos(SB_VERT);
		l_pImage->DisplayImage(this,&MemDC,XPos,YPos);

		// Display Regions
		if (!pDoc->GetRegionList()->IsEmpty())
		{
			MemDC.SetBkMode(TRANSPARENT);

			// Iterate through Regions
			POSITION pos;
			pos = pDoc->GetRegionList()->GetHeadPosition();
			while (pos)
			{
				CBrush NewBrush(HS_BDIAGONAL,RGB(255,0,0));
				MemDC.SelectObject(NewBrush);
				MemDC.SetBrushOrg(CPoint(0,0));
				TRACE("Painting Region\n");
			        MemDC.PaintRgn((CRgn*)pDoc->GetRegionList()
                                                ->GetAt(pos));
				pDoc->GetRegionList()->GetNext(pos);
				NewBrush.DeleteObject();
			}
		}
		// Copy MemDC to dc
		/*
		*/
		ImageWidth = l_pImage->GetImageWidth();
		ImageHeight = l_pImage->GetImageHeight();
	
	}
	dc.StretchBlt(0,0,ImageWidth,ImageHeight,
                      &MemDC,0,0,ImageWidth,ImageHeight, SRCPAINT);
}


I am getting desperate to find out what is wrong with this.  I have searched 
through MSDN, FAQs, and various books, but cannot understand what is wrong with 
this. 

TIA 

C
-------------------------------------------
Chris Hudson - christopher.hudson@magnet.mt




Brad Wilson/Crucial Software -- crucial@pobox.com
Saturday, December 07, 1996

[Mini-digest: 3 responses]

> But if I try to paint everything to a compatible memory DC then copy this
to the 
> actual dc it does not paint anything, but just leaves it blank  

When you create a compatible DC, it has a 1x1x1bit bitmap selected into it.
 You also need to create a compatible bitmap to paint on to.

> 	CDC MemDC;
	CBitmap MemBitmap, *pOldBitmap;
	CRect MemRect;
> 	MemDC.CreateCompatibleDC(&dc);
	PaintDC.GetClientRect(&MemRect)
	MemBitmap.CreateCompatibleBitmap(&dc,MemRect.Width(),MemRect.Height());
	pOldBitmap = MemDC.SelectObject(&MemBitmap);
> 	OnPrepareDC(&MemDC);

Then clean up at the end:

> 	dc.StretchBlt(0,0,ImageWidth,ImageHeight,
>                       &MemDC,0,0,ImageWidth,ImageHeight, SRCPAINT);
	MemDC.SelectObject(pOldBitmap);

Good luck!

--
Brad Wilson                       Custom software development & solutions
crucial@pobox.com               Internet and intranet consulting services
The Brads' Consulting          Windows NT/Windows 95 software development
http://www.thebrads.com    Web site planning, development and maintenance

-----From: "P.J.  Tezza" 

Chris,

Unless you create a bitmap and select it into your memory DC there is no =
point in painting anything on it. See MSDN:Books and =
Periodicals:Programming Windows 3.1:Part 4 The Graphics Device =
Interface:Chapter 13 Bits, Blts, and Metafiles:THE GDI BITMAP OBJECT. =
This may be discussed elsewhere, but this is were I read about working =
with memory DCs first. I am not sure what CLrView::OnPrepareDC does, but =
CView::OnPrepareDC does nothing when called without the pInfo parameter. =
OnPrepareDC is usually called before OnDraw is called.

PJ
pj@exemplarsoftware.com
-----From: Paul.B.Folbrecht@JCI.Com

     Chris,
       When a memory DC is created, it has a 1x1 monochrome bitmap selected 
     into it.  That's all the room you have to work with- 1 pixel.  What 
     you need to do is select a bitmap into the DC the size (dimensions and 
     colors) of what you're going to work with, before you do anything 
     else.  See CreateCompatibleBitmap().
     
     -Paul Folbrecht
     Compuware Corp.
     
     
     Environment: NT 4.0 sp1, VC4.2b
     
     I am having difficulties with creating a memory DC, then copying it to 
     the paint 
     DC. If create the OnPaint funtion below, then everything works fine.
     
     // Working OnPaint not using Memory DCs
     void CLrView::OnPaint() 
     {
        TRACE("Painting\n");
     
        CPaintDC MemDC(this); // device context for painting
                
        CLrDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        
        // Get pointer to image
        CImage* l_pImage = pDoc->GetImage();
        if (l_pImage)
        {
                // Display Image
                int XPos = GetScrollPos(SB_HORZ);
                int YPos = GetScrollPos(SB_VERT);
                l_pImage->DisplayImage(this,&MemDC,XPos,YPos);
     
                // Display Regions
                if (!pDoc->GetRegionList()->IsEmpty())
                {
                        MemDC.SetBkMode(TRANSPARENT);
     
                        // Iterate through Regions
                        POSITION pos;
                        pos = pDoc->GetRegionList()->GetHeadPosition();
                        while (pos)
                        {
                                CBrush NewBrush(HS_BDIAGONAL,RGB(255,0,0));
                                MemDC.SelectObject(NewBrush);
                                MemDC.SetBrushOrg(CPoint(0,0));
                                TRACE("Painting Region\n");
                                MemDC.PaintRgn((CRgn*)pDoc->GetRegionList()
                                                     ->GetAt(pos));
                                pDoc->GetRegionList()->GetNext(pos);
                                NewBrush.DeleteObject();
                        }
                }
        }
     }
     
     
     But if I try to paint everything to a compatible memory DC then copy 
     this to the 
     actual dc it does not paint anything, but just leaves it blank  
     
     // Not working OnPaint using CompatibleDCs
     void CLrView::OnPaint() 
     {
        TRACE("Painting\n");
     
        CPaintDC dc(this); // device context for painting
        
        // Create memory DC for creating the image on.
        CDC MemDC;
        MemDC.CreateCompatibleDC(&dc);
        OnPrepareDC(&MemDC);
        
        int ImageWidth = 0;
        int ImageHeight = 0;
        CLrDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        
        // Get pointer to image
        CImage* l_pImage = pDoc->GetImage();
        if (l_pImage)
        {
                // Display Image
                int XPos = GetScrollPos(SB_HORZ);
                int YPos = GetScrollPos(SB_VERT);
                l_pImage->DisplayImage(this,&MemDC,XPos,YPos);
     
                // Display Regions
                if (!pDoc->GetRegionList()->IsEmpty())
                {
                        MemDC.SetBkMode(TRANSPARENT);
     
                        // Iterate through Regions
                        POSITION pos;
                        pos = pDoc->GetRegionList()->GetHeadPosition();
                        while (pos)
                        {
                                CBrush NewBrush(HS_BDIAGONAL,RGB(255,0,0));
                                MemDC.SelectObject(NewBrush);
                                MemDC.SetBrushOrg(CPoint(0,0));
                                TRACE("Painting Region\n");
                                MemDC.PaintRgn((CRgn*)pDoc->GetRegionList()
                                                     ->GetAt(pos));
                                pDoc->GetRegionList()->GetNext(pos);
                                NewBrush.DeleteObject();
                        }
                }
                // Copy MemDC to dc
                /*
                */
                ImageWidth = l_pImage->GetImageWidth();
                ImageHeight = l_pImage->GetImageHeight();
        
        }
        dc.StretchBlt(0,0,ImageWidth,ImageHeight,
                           &MemDC,0,0,ImageWidth,ImageHeight, SRCPAINT);
     }
     
     
     I am getting desperate to find out what is wrong with this.  I have 
     searched 
     through MSDN, FAQs, and various books, but cannot understand what is 
     wrong with 
     this. 
     
     TIA 
     
     C
     -------------------------------------------
     Chris Hudson - christopher.hudson@magnet.mt





| Вернуться в корень Архива |