zoukankan      html  css  js  c++  java
  • 使用MFC中的CDC,CBitmap来实现贴图

    代码如下:

    void CProView::OnDraw(CDC* pDC)
    {
     CProDoc* pDoc = GetDocument();
     ASSERT_VALID(pDoc);
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////贴图
    CBitmap bitmap;
    bitmap.LoadBitmap(IDB_BITMAP1);
    
    CDC dcMemory;
    dcMemory.CreateCompatibleDC(pDC);
    
    // Select the bitmap into the in-memory DC
    CBitmap* pOldBitmap = dcMemory.SelectObject(&bitmap);
    
     // Find a centerpoint for the bitmap in the client area
     RECT rect;
     GetClientRect(&rect);
    
     // 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(0, 0, rect.right, rect.bottom, &dcMemory,0, 0, SRCCOPY);
     dcMemory.SelectObject(pOldBitmap);
    }

    cdc的方法CreateCompatibleDC:

    BOOL CreateCompatibleDC(
       CDC* pDC 
    );

    Creates a memory device context that is compatible with the device specified by pDC.

    根据一个dc创建一个相兼容的内存dc。

    cdc bitblt:Copies a bitmap from the source device context to this current device context.

    BOOL BitBlt( int x,    目标X坐标 int y,   目标Y坐标 int nWidth, 操作范围宽度 int nHeight,  操作范围高度

    CDC* pSrcDC,源内容 int xSrc,   源X坐标 int ySrc,   源Y坐标 DWORD dwRop     光栅操作方式);

    光栅操作经常是SRCCOPY:将源矩形区域直接拷贝到目标矩形区域。

    pDC->BitBlt(0, 0, rect.right, rect.bottom, &dcMemory,0, 0, SRCCOPY);

    上面的是以左上角为原点操作。可以找到当前视图中心点进行操作。

     CBitmap bmp;
      bmp.LoadBitmap(IDB_BITMAP1))
      
     Get the size of the bitmap
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);

     // 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);
    参考:http://msdn.microsoft.com/en-US/library/kwxzck32(v=vs.80).aspx
  • 相关阅读:
    [日常摸鱼]UVA393 The Doors 简单计算几何+最短路
    [日常摸鱼]bzoj3122 [Sdoi]2013 随机数生成器
    [日常摸鱼]积性函数求和——杜教筛
    [OI笔记]NOIP2017前(退役前)模拟赛的总结
    [日常摸鱼]poj2417 DiscreteLoggingBSGS算法
    [日常摸鱼]UVA11424&11426 GCD Extreme
    [日常摸鱼]JSOI2008最大数
    [日常摸鱼]HDU1724 Ellipse自适应Simpson法
    原码、补码、反码的作用和区别
    Fibonacci序列or兔子序列
  • 原文地址:https://www.cnblogs.com/youxin/p/2855541.html
Copyright © 2011-2022 走看看