CDC之CreateCompatibleDC与BitBlt

来源:互联网 发布:玉田二中网络优盘 编辑:程序博客网 时间:2024/05/16 10:39

CreateCompatibleDC  

of the compatible device.

When a memory device context is created, GDI automatically selects a 1-by-1 monochrome stock bitmap for it. GDI output functions can be used with a memory device context only if a bitmap has been created and selected into that context.

This function can only be used to create compatible device contexts for devices that support raster operations. See theCDC::BitBlt member function for information regarding bit-block transfers between device contexts. To determine whether a device context supports raster operations, see theRC_BITBLT raster capability in the member function CDC::GetDeviceCaps.

兼容设备。
建立一个内存设备上下文时,GDI自动选择为它装载一个黑白位图。GDI输出函数可以使用一个内存设备上下文只有一个位图已经创建并选择为背景。
此功能只能用于支持光栅操作的设备创建兼容的设备环境。看美国:对于位块传输设备之间的上下文信息BitBlt函数成员。确定设备是否支持光栅操作的上下文,在成员函数中CDC看到rc_bitblt光栅能力::GetDeviceCaps。

// This OnDraw() handler loads a bitmap from system resources,// centers it in the view, and uses BitBlt() to paint the bitmap// bits.void CBlat2View::OnDraw(CDC* pDC){   CBlat2Doc* pDoc = GetDocument();   ASSERT_VALID(pDoc);   // load IDB_BITMAP1 from our resources   CBitmap bmp;   if (bmp.LoadBitmap(IDB_BITMAP1))   {      // Get the size of the bitmap      BITMAP bmpInfo;      bmp.GetBitmap(&bmpInfo);      // Create an in-memory DC compatible with the      // display DC we're using to paint      CDC dcMemory;      dcMemory.CreateCompatibleDC(pDC);      // Select the bitmap into the in-memory DC      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);      // Find a centerpoint for the bitmap in the client area      CRect rect;      GetClientRect(&rect);      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;      // Copy the bits from the in-memory DC into the on-      // screen DC to actually do the painting. Use the centerpoint      // we computed for the target offset.      pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,          0, 0, SRCCOPY);      dcMemory.SelectObject(pOldBitmap);   }   else      TRACE0("ERROR: Where's IDB_BITMAP1?\n");}
 

创建一个与指定设备一致的内存设备描述表。

HDC CreateCompatibleDC(HDC hdc //设备描述表句柄);

参数 hdc

现有的设备描述表的一个句柄,如果这个句柄为NULL,则函数创建一个和应用程序当前屏幕一致的内存设备描述表。

返回值 如果函数调用成功,则返回一个内存设备描述表句柄;否则返回NULL。

说明

创建一个与特定设备场景一致的内存设备场景

返回值

Long,新设备场景句柄,若出错则为零

参数表

参数

类型及说明

hdc

Long,设备场景句柄。新的设备场景将与它一致。也可能为0以创建一个与屏幕一致的设备场景

注解

在绘制之前,先要为该设备场景选定一个位图。不再需要时,该设备场景可用DeleteDC函数删除。删除前,其所有对象应回复初始状态

BitBlt( //图形拷贝
  HMDC0, //目标设备场景
  x0,y0 //目标左上角坐标
  w,h //目标图形宽、高
  HMDC1, //源设备场景
  x1,y1 //源左上角坐标
  SRCCOPY //拷贝方式,这里是直接拷贝);

HBITMAP hbmp = (HBITMAP)::LoadImage(

AfxGetInstanceHandle(),
nidresourcename,IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION);

1.如何获取应用程序的实例句柄CWinAppIm_hInstance中,用户可以调用AfxGetInstanceHandle获取句柄。

?/P>

2.如何在代码中获取应用程序主窗口的指针

主框窗口的指针保存在CwinThread:: m_pMaiWnd中,可以调用AfxGetMainWnd来获取。下面的例子使应用程序极小化:

AfxGetMainWnd()->ShowWindow(SW_SHOWWMINMIZED);

CreateCompatibleDC

CDC MemDC;//首先定义一个显示设备对象
CBitmap MemBitmap;//定义一个位图对象

//随后建立与屏幕显示兼容的内存显示设备
MemDC.CreateCompatibleDC(NULL);
//这时还不能绘图,因为没有地方画^_^
//下面建立一个与屏幕显示兼容的位图,至于位图的大小嘛,可以用窗口的大小
CRect rc;
GetClientRect(&rc);

MemBitmap.CreateCompatibleBitmap(pDC,rc.Width(),rc.Height());
//将位图选入到内存显示设备中
//只有选入了位图的

 

0 0
原创粉丝点击