1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| void FanyDrawText(HWND hwnd, std::wstring wText) {
g_pDWriteFactory->CreateTextFormat(L"微软雅黑", NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 16.0f, L"zh-cn", &g_pDWriteTextFormat); RECT rc; GetClientRect(hwnd, &rc); g_pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)), &g_pRenderTarget); g_pRenderTarget->BeginDraw(); g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::LightGreen)); g_pRenderTarget->SetTransform(D2D1::IdentityMatrix()); g_pRenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_DEFAULT); wchar_t *szText = const_cast<wchar_t *>(wText.c_str()); RECT rect; GetClientRect(hwnd, &rect); D2D1_RECT_F layoutRect = D2D1::RectF(static_cast<FLOAT>(rect.left), static_cast<FLOAT>(rect.top), static_cast<FLOAT>(rect.right), static_cast<FLOAT>(rect.bottom)); g_pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &g_pBrush); g_pRenderTarget->DrawText(szText, wcslen(szText), g_pDWriteTextFormat, layoutRect, g_pBrush); HRESULT hr = g_pRenderTarget->EndDraw(); if (FAILED(hr)) { MessageBox(NULL, TEXT("End Drawing Error!"), L"error", MB_ICONERROR); } SAFE_RELEASE(g_pDWriteTextFormat); SAFE_RELEASE(g_pBrush); SAFE_RELEASE(g_pRenderTarget); }
|