Direct2D 窗口未完全呈现
Direct2D window doesn't fully render
我才刚刚开始尝试学习Direct2D和Windows,并且一直在学习MSDN上的教程。我编写的大部分代码几乎都是从他们的网站上手工复制的。我正在开发一个教程模块,该模块介绍了用户输入,代码与上一个模块基本相同,但程序启动并且窗口从未完全呈现;它只是一个白色区域,包括非工作区应该呈现的位置(顶部栏、侧面和底部)。以下是主文件的代码.cpp:
#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;
}
}
如果可以的话,请帮忙!我已经检查了一个半小时,找不到任何不一致的地方。
头文件包含一个 BaseWindow 模板类,此代码使用它来创建和注册窗口类等等,但头文件是从另一个工作正常的程序直接复制的,所以我认为它不需要包含。
自己想通了。原来我忘了在 HandleMessage 方法的末尾返回默认的窗口过程。
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- 如何在 Direct2D 窗口中正确初始化 Directwrite IDWriteTextLayout
- 如何在 direct2d 中使用随其窗口重新缩放的效果绘制图像
- 在 Direct2D 绘图中,调整窗口大小后尺寸会更改
- 在Windows中平滑调整窗口大小(使用Direct2D 1.1)
- Direct2D 窗口未完全呈现
- 如何使用Direct2D创建自定义窗口铬
- 基于 ATL 的 CWindowImpl 的 Direct2D 桌面窗口类模板
- Direct2D:透明父窗口之上的不透明子窗口
- Direct2D调整窗口大小时内存过度使用