2014-01-19 2 views
0

저는 방금 Direct2D 및 Windows를 배우려고 시작했으며 MSDN에있는 자습서를 진행했습니다. 필자가 작성한 대부분의 코드는 사이트에서 손으로 직접 복사 한 것입니다. 나는 사용자 입력을 소개하는 자습서 모듈을 사용하고 있으며 코드는 이전 모듈과 거의 동일하지만 프로그램이 시작되고 창이 완전히 렌더링되지 않습니다. 비 클라이언트 영역이 렌더링되어야하는 영역 (상단 바, 측면 및 하단)을 포함하는 단지 흰색 영역. 아래는 main.cpp 파일의 코드입니다.Direct2D 창이 완전히 렌더링되지 않습니다.

#ifndef UNICODE 
#define UNICODE 
#endif // !UNICODE 

#include <windowsx.h> 
#include <Windows.h> 
#include <d2d1.h> 
#pragma comment (lib, "d2d1") 

#include "BaseWindow.h" 

template <class T> void SafeRelease(T **ppT) 
{ 
if(*ppT) 
{ 
    (*ppT)->Release(); 
    *ppT = NULL; 
} 
} 

class DPIScale 
{ 
static float scaleX; 
static float scaleY; 

public: 
static void Intitialize(ID2D1Factory *pFactory) 
{ 
    FLOAT dpiX, dpiY; 
    pFactory->GetDesktopDpi(&dpiX, &dpiY); 
    scaleX = dpiX/96.0f; 
    scaleY = dpiY/96.0f; 
} 

template <typename T> 
static D2D1_POINT_2F Pixels_To_DIPs(T x, T y) 
{ 
    return D2D1::Point2F(static_cast<float>(x)/scaleX, static_cast<float>(y)  
/scaleY); 
} 
}; 

float DPIScale::scaleX = 1.0F; 
float DPIScale::scaleY = 1.0F; 

class MainWindow : public BaseWindow<MainWindow> 
{ 
ID2D1Factory   *pFactory; 
ID2D1HwndRenderTarget *pRenderTarget; 
ID2D1SolidColorBrush *pBrush; 
D2D1_ELLIPSE   ellipse; 
D2D1_POINT_2F   ptMouse; 

HRESULT CreateGraphicsResources(); 
void DiscardGraphicsResources(); 
void Resize(); 
void OnPaint(); 
void OnLButtonDown(int pixelX, int pixelY, DWORD flags); 
void OnLButtonUp(); 
void OnMouseMove(int pixelX, int pixelY, DWORD flags); 

public: 

MainWindow() : pFactory(NULL), pRenderTarget(NULL), pBrush(NULL), 
ellipse(D2D1::Ellipse(D2D1::Point2F(), 0, 0)), 
    ptMouse(D2D1::Point2F()) {} 

PCWSTR ClassName() const { return L"D2D Drawing Program Class"; } 
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); 
}; 

HRESULT MainWindow::CreateGraphicsResources() 
{ 
HRESULT hr = S_OK; 
if(pRenderTarget == NULL) 
{ 
    RECT rc; 
    GetClientRect(m_hwnd, &rc); 

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

    hr = pFactory->CreateHwndRenderTarget(
     D2D1::RenderTargetProperties(), 
     D2D1::HwndRenderTargetProperties(m_hwnd, size), 
     &pRenderTarget 
     ); 

    if(SUCCEEDED(hr)) 
    { 
     const D2D1_COLOR_F color = D2D1::ColorF(0.0f, 0.0f, 0.0f, 1.0f); 
     hr = pRenderTarget->CreateSolidColorBrush(color, &pBrush); 
    } 
} 

return hr; 
} 

void MainWindow::DiscardGraphicsResources() 
{ 
SafeRelease(&pRenderTarget); 
SafeRelease(&pBrush); 
} 

void MainWindow::OnPaint() 
{ 
HRESULT hr = CreateGraphicsResources(); 
if(SUCCEEDED(hr)) 
{ 
    PAINTSTRUCT ps; 
    BeginPaint(m_hwnd, &ps); 

    pRenderTarget->BeginDraw(); 

    pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); 
    pRenderTarget->FillEllipse(ellipse, pBrush); 

    hr = pRenderTarget->EndDraw(); 
    if(FAILED(hr) || hr == D2DERR_RECREATE_TARGET) 
    { 
     DiscardGraphicsResources(); 
    } 
    EndPaint(m_hwnd, &ps); 
} 
} 

void MainWindow::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); 
    } 
} 

void MainWindow::OnLButtonDown(int pixelX, int pixelY, DWORD flags) 
{ 
SetCapture(m_hwnd); 
ellipse.point = ptMouse = DPIScale::Pixels_To_DIPs(pixelX, pixelY); 
ellipse.radiusX = ellipse.radiusY = 1.0f; 
InvalidateRect(m_hwnd, NULL, FALSE); 
} 

void MainWindow::OnLButtonUp() 
{ 
ReleaseCapture(); 
} 

void MainWindow::OnMouseMove(int pixelX, int pixelY, DWORD flags) 
{ 
const D2D1_POINT_2F DIPs = DPIScale::Pixels_To_DIPs(pixelX, pixelY); 

const float width = (DIPs.x - ptMouse.x)/2; 
const float height = (DIPs.y - ptMouse.y)/2; 
const float x1 = ptMouse.x + width; 
const float y1 = ptMouse.y + height; 

ellipse = D2D1::Ellipse(D2D1::Point2F(x1, y1), width, height); 

InvalidateRect(m_hwnd, NULL, FALSE); 
} 

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) 
{ 
MainWindow win; 

if(!win.Create(L"D2D Drawing Program", WS_OVERLAPPEDWINDOW)) 
{ 
    return 0; 
} 

ShowWindow(win.Window(), nCmdShow); 

MSG msg = {}; 

while(GetMessage(&msg, NULL, 0, 0)) 
{ 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
} 

return 0; 
} 

LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
switch(uMsg) 
{ 
    case WM_CREATE: 
     if(FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, 
         &pFactory))) 
     { 
      return -1; 
     } 

     // DPIScale::Intitialize(pFactory); 
     return 0; 

    case WM_DESTROY: 
     DiscardGraphicsResources(); 
     SafeRelease(&pFactory); 
     PostQuitMessage(0); 
     return 0; 

    case WM_PAINT: 
     OnPaint(); 
     return 0; 

    case WM_SIZE: 
     Resize(); 
     return 0; 

    case WM_LBUTTONDOWN: 
     OnLButtonDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 
          (DWORD)wParam); 
     return 0; 

    case WM_LBUTTONUP: 
     OnLButtonUp(); 
     return 0; 

    case WM_MOUSEMOVE: 
     OnMouseMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam 
          (DWORD)wParam); 
     return 0; 
} 
} 

가능한 경우 도움주세요! 나는 지금 1 시간 반 동안 그것을 끝냈고 어떤 불일치도 찾을 수 없었다.

이 코드는 창 클래스 및 그 모두를 만들고 등록하는 데 사용하는 헤더 파일에 포함 된 BaseWindow 템플릿 클래스이지만 헤더 파일은 잘 작동하는 다른 프로그램의 직접 복사 파일이므로 그것이 포함될 필요가 있다고 생각하십시오.

답변

0

나 자신을 알아 냈어. HandleMessage 메서드가 끝나면 기본 윈도우 프로 시저를 반환하는 것을 잊어 버렸습니다.

관련 문제