#ifndef UNICODE
#define UNICODE
#endif // !UNICODE
#include <Windows.h>
#include <d2d1.h>
#include <cstdlib>
#include <time.h>
LRESULT CALLBACK WProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
ID2D1Factory *pD2D1Factory = NULL;
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2D1Factory);
template<class T>
void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
void DrawSquars(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"Window Class";
WNDCLASS wc = {};
wc.hInstance = hInstance;
wc.lpfnWndProc = WProc;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"Wind Name", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
SafeRelease(&pD2D1Factory);
PostQuitMessage(0);
return 0;
case WM_PAINT:
DrawSquars(hwnd, uMsg, wParam, lParam);
return 0;
}
DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void DrawSquars(HWND hwnd,UINT uMsg,WPARAM wParam, LPARAM lParam)
{
if (SUCCEEDED(hr))
{
RECT rc;
GetClientRect(hwnd, &rc);
ID2D1HwndRenderTarget *pRT;
hr = pD2D1Factory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)), &pRT);
if (SUCCEEDED(hr))
{
pRT->BeginDraw();
pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));
//pRT->EndDraw();
srand(time(NULL));
ID2D1SolidColorBrush *pSCB;
int length_square = rc.bottom / 123;
if (length_square == 0)
length_square = 1;
for (int i = 0; i <= rc.bottom; i += length_square)
{
for (int j = 0; j <= rc.right; j += length_square)
{
float r = rand() % 100;
r = r / 100;
float g = (rand() % 100);
g = g / 100;
float b = rand() % 100;
b = b / 100;
pRT->CreateSolidColorBrush(D2D1::ColorF(r, g, b), &pSCB);
//pRT->BeginDraw();
pRT->FillRectangle(D2D1::RectF(j, i, j + length_square, i + length_square), pSCB);
//pRT->EndDraw();
}
}
pRT->EndDraw();
SafeRelease(&pSCB);
}
SafeRelease(&pRT);
}
}
Картинка почему то постоянно обновляется, и происходит постоянное выделение памяти без освобождения.