简体   繁体   English

调整窗口大小后,direct2d工程图中的尺寸更改

[英]in direct2d drawing dimensions change after window resizing

I am a beginner, and I have made a program in direct2d using c++ in visual studio 2015, just a very basic program that draws a white square on a black background , and it does show the square in a small window. 我是一个初学者,我已经在Visual Studio 2015中使用c ++在Direct2d中编写了一个程序,这只是一个非常基本的程序,它在黑色背景上绘制了一个白色正方形,并且确实在一个小窗口中显示了正方形。 But the problem is that if the user maximizes that window the vertical dimension of the drawing becomes elongated and the square becomes rectangle. 但是问题是,如果用户最大化该窗口,则图形的垂直尺寸会变长,而正方形会变成矩形。

This is the screenshot of the window before maximizing it: enter image description here 这是最大化窗口之前的屏幕截图: 在此处输入图像描述

And this is the screenshot of the window after maximizing it: enter image description here 这是最大化后的窗口截图: 在此处输入图像描述

What am I doing wrong ? 我究竟做错了什么 ? here is my code and thanks in advance: 这是我的代码,在此先感谢:

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();
 }
}

It seems like you don't resize the render target when your window resizes. 调整窗口大小时,似乎没有调整渲染目标的大小。 I expect you run into similar problems not just from maximizing, but when you resize your window too? 我希望您不仅会因为最大化而遇到类似的问题,而且还会在调整窗口大小时遇到​​类似的问题? See https://docs.microsoft.com/en-us/windows/desktop/LearnWin32/dpi-and-device-independent-pixels#resizing-the-render-target . 请参阅https://docs.microsoft.com/zh-cn/windows/desktop/LearnWin32/dpi-and-device-independent-pixels#resizing-the-render-target

Code snippet from the linked document: 链接文档中的代码段:

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);
    }
}

You'll want to call this by catching WM_SIZE. 您需要通过捕获WM_SIZE来调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM