2015-04-24 48 views
-1

如何在C++中實現MFC應用程序中的2D圖形?如果有任何類或庫,請給我參考MSDN中適當的文章。我可以在MFC應用程序中使用Visual C++中的GDI +嗎?我可以在MFC應用程序中使用Visual C++中的Direct2D嗎?我在MS VS 2013 Ultimate工作。任何幫助將非常感激。如何在Visual C++中的MFC應用程序中實現2D圖形?

回答

1

是,的Direct2D通過MFC框架,CHwndRenderTarget類原生支持:

下面是關於如何使用它的一個例子:

class CDemoView : public CScrollView 
{ 
    CHwndRenderTarget m_renderTarget; 
    std::shared_ptr<CD2DBitmap> m_spBitmap; 
    // ... 
}; 

int CDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
    if (CScrollView::OnCreate(lpCreateStruct) == -1) 
     return -1; 

    HRESULT hr = m_renderTarget.Create(m_hWnd); 
    return SUCCEEDED(hr) ? 0 : -1;  
} 

void CDemoView::OnSize(UINT nType, int cx, int cy) 
{ 
    CScrollView::OnSize(nType, cx, cy); 

    if(m_renderTarget.IsValid()) 
    { 
     m_renderTarget.Resize(CD2DSizeU(cx, cy)); 
    } 
} 

void CDemoView::OnDraw(CDC* pDC) 
{ 
    if(m_renderTarget.IsValid()) 
    { 
     // initiate drawing on render target 
     m_renderTarget.BeginDraw(); 
     // clear background using white color 
     D2D1_COLOR_F color = {1.f, 1.f, 1.f, 1.f}; // r, g, b, a 
     m_renderTarget.Clear(color); 
     if((nullptr != m_spBitmap) && m_spBitmap->IsValid()) 
     { 
      // apply translation transform according to view's scroll position 
      CPoint point = GetScrollPosition(); 
      D2D1_MATRIX_3X2_F matrix = D2D1::Matrix3x2F::Translation((float)-point.x, (float)-point.y); 
      m_renderTarget.SetTransform(matrix); 
      // draw the bitmap 
      CD2DSizeF size = m_spBitmap->GetSize(); 
      m_renderTarget.DrawBitmap(m_spBitmap.get(), CD2DRectF(0, 0, size.width, size.height)); 
     } 
     // ends drawing operations 
     HRESULT hr = m_renderTarget.EndDraw(); 
     // if the render target has been lost, then recreate it 
     if(D2DERR_RECREATE_TARGET == hr) 
     { 
      m_renderTarget.ReCreate(m_hWnd); 
     } 
    } 
} 

GDI +也支持:

// init GDI+ 
Gdiplus::GdiplusStartupInput gdiplusStartupInput; 
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 

這是與GDI設備上下文中使用它的方式:

CPaintDC dc(this); 
Graphics graphics(dc.hdc); 
Pen MyPen(Color(255, 0, 255,)); // A green pen, with full alpha 
graphics.DrawLine(&pen, 0, 0, 200, 100); 
0

除了什麼安德魯已經建議:

MFC更是把內置支持的Direct2D庫(也爲相關DirectWrite的 )。

呼叫的CWnd :: EnableD2DSupport,例如在WM_CREATE消息處理程序,那不更需要創造渲染目標自己,調整其大小,調用

  1. BeginDraw
  2. EndDraw

如果渲染目標已丟失,則重新創建渲染目標等。

查看此文章的詳細信息:MFC Support for Direct2D – Part 2

相關問題