本文介绍了MFC位图复制或获取设备上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经编写了一些代码来在设备上下文和ondraw类中的位图中绘制一些行等。

我可以使用cimage保存该位图,但我想从其他源文件(.cpp)或类中访问该位图。

无法从ondraw类以外的任何位置访问位图。我也尝试将位图作为全局变量但没有用。 

任何正文都可以显示一些方法来复制位图或如何在其他类或文件中使用它。


这是代码,


CClientDC dcScreen(this);

    

         CBitmap位图;

bitmap.CreateCompatibleBitmap(& dcScreen,500,500);



CDC dcMem;

dcMem.CreateCompatibleDC(& dcScreen);
$




CBrush画笔(RGB(255,255,255));

CBitmap * pOldBitmap = dcMem.SelectObject(& bitmap);

dcMem.FillRect(CRect(0,0,500,500),& brush);

dcMem.MoveTo(50,50);        //设置当前位置

   dcMem.LineTo(50200);       //画一条垂直线向下150个单位

   dcMem.LineTo(150,200);      //画一条水平线100个单位

   dcMem.LineTo(150,50);       //画一条垂直线150个单位

   dcMem.LineTo(50,50);



   dcScreen.BitBlt(0,0,500,500和& dcMem,0,0,SRCCOPY);
$


dcMem.SelectObject(pOldBitmap);

 









解决方案

Hi,

I have written some code to draw some lines etc in a device context and bitmap in ondraw class.
I can save that bitmap using cimage but I want to access that bitmap from other source file(.cpp) or class.
The bitmap is not accessible from anywhere other than the ondraw class. I have also tried taking the bitmap as global variable but no use. 
Can any body please show some method to copy the bitmap or how to use it in other class or file.

Here is the code,

CClientDC dcScreen (this);
    
        CBitmap bitmap;
bitmap.CreateCompatibleBitmap (&dcScreen, 500, 500);

CDC dcMem;
dcMem.CreateCompatibleDC (&dcScreen);


CBrush brush (RGB (255, 255, 255));
CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
dcMem.FillRect (CRect (0, 0, 500, 500), &brush);
dcMem.MoveTo(50,50);        // Set the current position
   dcMem.LineTo(50,200);       // Draw a vertical line down 150 units
   dcMem.LineTo(150,200);      // Draw a horizontal line right 100 units
   dcMem.LineTo(150,50);       // Draw a vertical line up 150 units
   dcMem.LineTo(50,50);

   dcScreen.BitBlt (0, 0, 500, 500, &dcMem, 0, 0, SRCCOPY);

dcMem.SelectObject (pOldBitmap);
 




解决方案


这篇关于MFC位图复制或获取设备上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:24