在窗口大小调整后,direct2d图纸尺寸发生变化

时间:2018-03-01 16:05:28

标签: c++ direct2d

我是初学者,我在visual studio 2015中使用c ++在direct2d中创建了一个程序,这是一个在黑色背景上绘制白色方块的非常基本的程序,它确实在一个小窗口中显示正方形。但问题在于,如果用户最大化该窗口,则图形的垂直尺寸变长,方形变为矩形。

这是最大化之前窗口的屏幕截图: enter image description here

这是最大化后窗口的屏幕截图: enter image description here

我做错了什么?  这是我的代码,并提前感谢:

ID2D1HwndRenderTarget* pRT = NULL;
ID2D1Factory* pD2DFactory = NULL;
ID2D1SolidColorBrush* pBlackBrush = NULL;
ID2D1SolidColorBrush* pRedBrush = NULL;
HRESULT hr;
RECT rc;
POINT pt;

void draw(){
  pRT->BeginDraw();
  pRT->FillRectangle( D2D1::RectF((FLOAT)rc.left,
     (FLOAT)rc.top,(FLOAT)rc.right, (FLOAT)rc.bottom),pBlackBrush);
  pRT->FillRectangle( D2D1::RectF((FLOAT)pt.x,
     (FLOAT)pt.y,pt.x + 50.0f  ,pt.y + 50.0f),pRedBrush);
  hr = pRT->EndDraw();
}

int APIENTRY wWinMain(/* wWinMain parameters */){

 /*
 ...
 window making
 ...
 */

 hr = D2D1CreateFactory( 
   D2D1_FACTORY_TYPE_SINGLE_THREADED,&pD2DFactory );

 // Obtain the size of the drawing area.
 GetClientRect(hWnd, &rc);

 // Create a Direct2D render target         

 hr =pD2DFactory->CreateHwndRenderTarget(
    D2D1::RenderTargetProperties(),
    D2D1::HwndRenderTargetProperties(
       hWnd,D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)
    ),
    &pRT
 );
 if (SUCCEEDED(hr)){
   pRT->CreateSolidColorBrush(
      D2D1::ColorF(D2D1::ColorF::White),
      &pRedBrush
   );
   pRT->CreateSolidColorBrush(
      D2D1::ColorF(D2D1::ColorF::Black),
      &pBlackBrush
   );
 }
 pt.x = 0;
 pt.y = 0;

 while (1) {
   PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
   if (msg.message == WM_QUIT) break;
   if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
     TranslateMessage(&msg);
     DispatchMessage(&msg);
   }

   draw();
 }
}

1 个答案:

答案 0 :(得分:0)

当您调整窗口大小时,似乎没有调整渲染目标的大小。我希望您不仅会因为最大化而遇到类似的问题,而且还会在调整窗口大小时遇到​​类似的问题?参见https://docs.microsoft.com/en-us/windows/desktop/LearnWin32/dpi-and-device-independent-pixels#resizing-the-render-target

链接文档中的代码段:

void Resize()
{
    if (pRenderTarget != NULL)
    {
        RECT rc;
        GetClientRect(m_hwnd, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

        pRenderTarget->Resize(size);
        InvalidateRect(m_hwnd, NULL, FALSE);
    }
}

您将通过捕获WM_SIZE来调用它。