【发布时间】:2018-03-01 16:05:27
【问题描述】:
我是一个初学者,我在 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();
}
}
【问题讨论】:
-
一两张屏幕截图可能有助于人们理解您的问题
-
我添加了两张截图
-
我认为没有任何东西被拉长,很可能你只是看到旧的渲染结果,在它们上面有新的渲染结果。因此,当您调整大小时,您会反复绘制现有窗口内容。我建议正确处理窗口消息,特别是 WM_PAINT。