Resizing behavior of using IDXGISwapChain1::ResizeBuffer() is weird

thebluetropics 1,046 Reputation points
2022-09-08T11:35:49.38+00:00

I have building a Direct2D 1.1 application, and now I currently learn how to handle the WM_SIZE, but something weird occurs:

Image1

Resize window code:

   case WM_SIZE: {  
       UINT w = LOWORD(lparam);  
       UINT h = HIWORD(lparam);  
       win->Resize(w, h);  
       return 0;  
   }  



   void MainWindow::Resize(UINT nwidth, UINT nheight) {  
       if (state.dxgiSwapChain) {  
           HRESULT hresult;  
     
           state.d2dDeviceContext->SetTarget(nullptr);  
           SafeRelease(&state.dxgiBackBuffer);  
     
           hresult = state.dxgiSwapChain->ResizeBuffers(  
               2,  
               nwidth,  
               nheight,  
               DXGI_FORMAT_B8G8R8A8_UNORM,  
               0  
           );  
     
           if (FAILED(hresult)) {  
               if (hresult == DXGI_ERROR_DEVICE_REMOVED) {  
                   OutputDebugStringW(L"DXGI Device Removed\n");  
               } else if (hresult == DXGI_ERROR_DEVICE_RESET) {  
                   OutputDebugStringW(L"DXGI Device Reset\n");  
               } else {  
                   OutputDebugStringW(L"Any else\n");  
               }  
           }  
     
           IDXGISurface* s_dxgiBackBuffer;  
     
           FLOAT dpi = GetDpiForWindow(state.hwnd);  
     
           D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1(  
               D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,  
               D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),  
               dpi,  
               dpi,  
               NULL  
           );  
     
           if (FAILED(state.dxgiSwapChain->GetBuffer(  
               0,  
               IID_PPV_ARGS(&s_dxgiBackBuffer)  
           ))) throw std::runtime_error("Failed to get buffer\n");  
     
           if (FAILED(state.d2dDeviceContext->CreateBitmapFromDxgiSurface(  
               s_dxgiBackBuffer,  
               bitmapProperties,  
               &state.dxgiBackBuffer  
           ))) throw std::runtime_error("Failed create bitmap\n");  
     
           state.d2dDeviceContext->SetTarget(state.dxgiBackBuffer);  
       }  
   }  
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,405 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,503 questions
{count} votes