Win32 + Direct2D
-
I've never used GDI in any of my projects. But had some programming introduction to it. Since I've been using Windows 7, I'm trying to build a new project with Direct2D. It compiles and runs, but it doesn't display anything! Does anyone have any experience, what could be the reason?
#include
#includeLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hCurrInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS SetFileTime = {};
SetFileTime.hInstance = hCurrInstance;
SetFileTime.lpfnWndProc = WndProc;
SetFileTime.lpszClassName = L"D2D";RegisterClass(&SetFileTime); CreateWindow( L"D2D", L"Direct2D", WS\_OVERLAPPEDWINDOW|WS\_VISIBLE, 100, 100, 800, 600, NULL, NULL, hCurrInstance, NULL); MSG WindowMessages = { }; while (GetMessage(&WindowMessages, NULL, 0, 0)) { TranslateMessage(&WindowMessages); DispatchMessage(&WindowMessages); }
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
ID2D1Factory* pD2DFactory = NULL;
D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);RECT rc; GetClientRect(hwnd, &rc); ID2D1HwndRenderTarget\* pRT = NULL; pD2DFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU( rc.right - rc.left, rc.bottom - rc.top)), &pRT); ID2D1SolidColorBrush\* m\_pLightSlateGrayBrush = NULL; pRT->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::LightSlateGray), &m\_pLightSlateGrayBrush); D2D\_RECT\_F OriginalRect; OriginalRect.left = 10.f; OriginalRect.top = 10.f; OriginalRect.bottom = 100.f; OriginalRect.right = 100.f; D2D1\_ROUNDED\_RECT RoundedRect; RoundedRect.rect = OriginalRect; RoundedRect.radiusX = 1.0f; RoundedRect.radiusY = 1.0f; ID2D1RoundedRectangleGeometry\* Card = NULL; pD2DFactory->CreateRoundedRectangleGeometry(RoundedRect, &Card); pRT->BeginDraw(); pRT->FillRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush); pRT->DrawRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush); pRT->EndDraw(); return 0; } case WM\_CLOSE: { DestroyWindow(hwnd); return 0; } case WM\_DESTROY: { PostQuitMessage(0); return 0; } default: return DefWindowProc(hwnd, msg, wParam, lParam); }
}
-
I've never used GDI in any of my projects. But had some programming introduction to it. Since I've been using Windows 7, I'm trying to build a new project with Direct2D. It compiles and runs, but it doesn't display anything! Does anyone have any experience, what could be the reason?
#include
#includeLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hCurrInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS SetFileTime = {};
SetFileTime.hInstance = hCurrInstance;
SetFileTime.lpfnWndProc = WndProc;
SetFileTime.lpszClassName = L"D2D";RegisterClass(&SetFileTime); CreateWindow( L"D2D", L"Direct2D", WS\_OVERLAPPEDWINDOW|WS\_VISIBLE, 100, 100, 800, 600, NULL, NULL, hCurrInstance, NULL); MSG WindowMessages = { }; while (GetMessage(&WindowMessages, NULL, 0, 0)) { TranslateMessage(&WindowMessages); DispatchMessage(&WindowMessages); }
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
ID2D1Factory* pD2DFactory = NULL;
D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);RECT rc; GetClientRect(hwnd, &rc); ID2D1HwndRenderTarget\* pRT = NULL; pD2DFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU( rc.right - rc.left, rc.bottom - rc.top)), &pRT); ID2D1SolidColorBrush\* m\_pLightSlateGrayBrush = NULL; pRT->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::LightSlateGray), &m\_pLightSlateGrayBrush); D2D\_RECT\_F OriginalRect; OriginalRect.left = 10.f; OriginalRect.top = 10.f; OriginalRect.bottom = 100.f; OriginalRect.right = 100.f; D2D1\_ROUNDED\_RECT RoundedRect; RoundedRect.rect = OriginalRect; RoundedRect.radiusX = 1.0f; RoundedRect.radiusY = 1.0f; ID2D1RoundedRectangleGeometry\* Card = NULL; pD2DFactory->CreateRoundedRectangleGeometry(RoundedRect, &Card); pRT->BeginDraw(); pRT->FillRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush); pRT->DrawRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush); pRT->EndDraw(); return 0; } case WM\_CLOSE: { DestroyWindow(hwnd); return 0; } case WM\_DESTROY: { PostQuitMessage(0); return 0; } default: return DefWindowProc(hwnd, msg, wParam, lParam); }
}
You don't handle the WM_PAINT message, in which all the drawing code should be put. You have put everything in WM_CREATE, which is called when the window is being created. I suggest you read the following article[^] to get a basic understanding of how it works.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++