Dwrite

//.h

 


HRESULT CreateDeviceResources();
HRESULT CreateDeviceIndependentResources();
HRESULT DrawDirectText();

ID2D1HwndRenderTarget* m_pRT;
ID2D1Factory* m_pFactory;
ID2D1SolidColorBrush* m_pBrush;

IDWriteFactory* m_pDwriteFactory;
IDWriteTextFormat* m_pFormat;
IDWriteTextLayout* m_pTextLayout;

CString m_strData;

-----------------------------------------------------------------------------------------------------------------------------

//cpp


#include <d2d1.h>
#include <dwrite.h>
#include <wincodec.h>

#pragma comment (lib,"d2d1.lib")
#pragma comment (lib, "dwrite.lib")

void CTestDirectDlg::OnBnClickedButton1()
{

CreateDeviceIndependentResources();
CreateDeviceResources();
m_pRT->BeginDraw();
m_pRT->SetTransform(D2D1::IdentityMatrix());
m_pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));
DrawDirectText();
m_pRT->EndDraw();


}

HRESULT CTestDirectDlg::CreateDeviceIndependentResources()
{
HRESULT hr;

// Create Direct2D factory.
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&m_pFactory
);

// Create a shared DirectWrite factory.
if (SUCCEEDED(hr))
{
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&m_pDwriteFactory)
);
}

// The string to display.
m_strData = L"Hello World My first DirectWrite!";
//int cTextLength_ = (UINT32) wcslen(strData);
int cTextLength = m_strData.GetLength();

// Create a text format using Gabriola with a font size of 72.
// This sets the default font, weight, stretch, style, and locale.
if (SUCCEEDED(hr))
{
hr = m_pDwriteFactory->CreateTextFormat(
L"Gabriola", // Font family name.
NULL, // Font collection (NULL sets it to use the system font collection).
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
72.0f,
L"en-us",
&m_pFormat
);
}
// Center align (horizontally) the text.
if (SUCCEEDED(hr))
{
hr = m_pFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
}

// Center align (vertically) the text.
if (SUCCEEDED(hr))
{
hr = m_pFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}


// Create a text layout using the text format.
if (SUCCEEDED(hr))
{
RECT rect;
::GetClientRect(m_hWnd, &rect);

float width = rect.right;
float height = rect.bottom;

hr = m_pDwriteFactory->CreateTextLayout(
m_strData, // The string to be laid out and formatted.
cTextLength, // The length of the string.
m_pFormat, // The text format to apply to the string (contains font information, etc).
width, // The width of the layout box.
height, // The height of the layout box.
&m_pTextLayout // The IDWriteTextLayout interface pointer.
);
}


// Format the "DirectWrite" substring to be of font size 100.
if (SUCCEEDED(hr))
{
DWRITE_TEXT_RANGE textRange = {23, // Start index where "DirectWrite" appears.
6 }; // Length of the substring "Direct" in "DirectWrite".
hr = m_pTextLayout->SetFontSize(100.0f, textRange);
}

// Format the word "DWrite" to be underlined.
if (SUCCEEDED(hr))
{

DWRITE_TEXT_RANGE textRange = {23, // Start index where "DirectWrite" appears.
11 }; // Length of the substring "DirectWrite".
hr = m_pTextLayout->SetUnderline(TRUE, textRange);
//m_pTextLayout->SetFontFamilyName(L"Segio UI", textRange);
}

if (SUCCEEDED(hr))
{
// Format the word "DWrite" to be bold.
DWRITE_TEXT_RANGE textRange = {20,
11 };
hr = m_pTextLayout->SetFontWeight(DWRITE_FONT_WEIGHT_BOLD, textRange);
}


// Declare a typography pointer.
IDWriteTypography* pTypography = NULL;

// Create a typography interface object.
if (SUCCEEDED(hr))
{
hr = m_pDwriteFactory->CreateTypography(&pTypography);
}

// Set the stylistic set.
DWRITE_FONT_FEATURE fontFeature = {DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7,
1};
if (SUCCEEDED(hr))
{
hr = pTypography->AddFontFeature(fontFeature);
}



if (SUCCEEDED(hr))
{
// Set the typography for the entire string.
DWRITE_TEXT_RANGE textRange = {0,
cTextLength};
hr = m_pTextLayout->SetTypography(pTypography, textRange);
}


SafeRelease(&pTypography);

return hr;
}


HRESULT CTestDirectDlg::CreateDeviceResources()
{
HRESULT hr = S_OK;

RECT rc;
::GetClientRect(
m_hWnd,
&rc
);

D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);
if (!m_pFactory)
{
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pFactory);
}

if (!m_pRT)
{
// Create a Direct2D render target.
hr = m_pFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
m_hWnd,
size
),
&m_pRT
);

// Create a black brush.
if (SUCCEEDED(hr))
{
hr = m_pRT->CreateSolidColorBrush(
D2D1::ColorF(
D2D1::ColorF::Black
),
&m_pBrush
);
}
}

return hr;
}

HRESULT CTestDirectDlg::DrawDirectText()
{
RECT rc;

::GetClientRect(
m_hWnd,
&rc
);


D2D1_POINT_2F origin = D2D1::Point2F(
static_cast<FLOAT>(rc.left),
static_cast<FLOAT>(rc.top)
);



m_pRT->DrawTextLayout(
origin,
m_pTextLayout,
m_pBrush
);

return S_OK;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值