基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(2) 国际化文字展示

Direct2D是美国微软公司用来取代 DirectDraw 以及GDI, GDI+等新技术[1],主要提供2D 动画的硬件加速,目前只支援 Windows Vistamicrosoft远程桌面 SP2以及Windows 7以上[2]。

Direct2D技术架构于Dire方舟primitivect3D 10.1 API 之上,能够透过硬件加速功能来建立windows11有必要升级吗2D图形,而且完全支持透明和Alpha混合。Direct2D 亦支援microsoft远程桌面软件实现primitive翻译(Softwarmicrosoft是什么意思e raprimitivelysterizer),亦即在显卡不支持硬件加系统运维工程师速情况下,Direct2D 仍可以使用软件方式描绘,且效果仍优于GDI。

Direct2D 可系统/运维以使用DXGI(DirectX Graphics Infrastructure) 与交windows11有必要升级吗互操作,Direct2D还能很好的支持DirectWrite。

Direct2D的支持高品质的渲染,具有以下特点:

支援ClearType 文字的呈现方式(DirectWriwindows10te 提供)

消除primitive翻译原图锯齿状(Per primitive antialiasin系统运维主要做什么g)

几何形状(直线,曲线)和位图绘制和填写。

纯色系统运维主要做什么(Solid color)、线性。

描绘中间层。

多元的几何操作(如unions, intersections, widening, outlinimicrosoft账户注册ng等)

在微软VS11提供了primitive和primary的区别Direct模板

我们创建一个应用程序以后,

插入下列代码

direct3d代码分析

using namespace Microsoft::WRL;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::System;
using namespace Windows::Foundation;
using namespace Windows::Graphics::Display;

DWriteVerticalText::DWriteVerticalText()
{
}

void DWriteVerticalText::CreateDeviceIndependentResources()
{
DirectXBase::CreateDeviceIndependentResources();

m_readingDirection = ReadingDirectionLeftToRightTopToBottom;
m_text =
L"『世界人権宣言』\r\n"
L"(1948.12.10 第3回国連総会採択)〈前文〉\r\n"
L"\r\n"
L"人類社会のすべての構成員の固有の尊厳と平等で譲ることのできない権利とを承認することは、世界における自由、正義及び平和の基礎であるので、\r\n"
L"\r\n"
L"人権の無視及び軽侮が、人類の良心を踏みにじった野蛮行為をもたらし、言論及び信仰の自由が受けられ、恐怖及び欠乏のない世界の到来が、一般の人々の最高の願望として宣言されたので、\r\n"
L"\r\n"
L"人間が専制と圧迫とに対する最後の手段として反逆に訴えることがないようにするためには、法の支配によって人権を保護することが肝要であるので、\r\n"
L"\r\n"
L"諸国間の友好関係の発展を促進することが肝要であるので、\r\n"
L"\r\n"
L"国際連合の諸国民は、国連憲章において、基本的人権、人間の尊厳及び価値並びに男女の同権についての信念を再確認し、かつ、一層大きな自由のうちで社会的進歩と生活水準の向上とを促進することを決意したので、\r\n"
L"\r\n"
L"加盟国は、国際連合と協力して、人権及び基本的自由の普遍的な尊重及び遵守の促進を達成することを誓約したので、\r\n"
L"\r\n"
L"これらの権利及び自由に対する共通の理解は、この誓約を完全にするためにもっとも重要であるので、\r\n"
L"\r\n"
L"よって、ここに、国連総会は、\r\n"
L"\r\n"
L"\r\n"
L"社会の各個人及び各機関が、この世界人権宣言を常に念頭に置きながら、加盟国自身の人民の間にも、また、加盟国の管轄下にある地域の人民の間にも、これらの権利と自由との尊重を指導及び教育によって促進すること並びにそれらの普遍的措置によって確保することに努力するように、すべての人民とすべての国とが達成すべき共通の基準として、この人権宣言を公布する。\r\n"
L"\r\n"
L"第1条\r\n"
L"すべての人間は、生まれながらにして自由であり、かつ、尊厳と権利と について平等である。人間は、理性と良心とを授けられており、互いに同 胞の精神をもって行動しなければならない。\r\n"
L"\r\n"
L"第2条"
L"すべて人は、人種、皮膚の色、性、言語、宗教、政治上その他の意見、\r\n"
L"\r\n"
L"国民的もしくは社会的出身、財産、門地その他の地位又はこれに類するい\r\n"
L"\r\n"
L"かなる自由による差別をも受けることなく、この宣言に掲げるすべての権\r\n"
L"\r\n"
L"利と自由とを享有することができる。\r\n"
L"\r\n"
L"さらに、個人の属する国又は地域が独立国であると、信託統治地域で\r\n"
L"\r\n"
L"あると、非自治地域であると、又は他のなんらかの主権制限の下にあると\r\n"
L"\r\n"
L"を問わず、その国又は地域の政治上、管轄上又は国際上の地位に基ずくい\r\n"
L"\r\n"
L"かなる差別もしてはならない。\r\n"
L"\r\n"
L"第3条\r\n"
L"すべての人は、生命、自由及び身体の安全に対する権利を有する。\r\n"
L"\r\n"
L"第4条\r\n"
L"何人も、奴隷にされ、又は苦役に服することはない。奴隷制度及び奴隷\r\n"
L"\r\n"
L"売買は、いかなる形においても禁止する。\r\n"
L"\r\n"
L"第5条\r\n"
L"何人も、拷問又は残虐な、非人道的なもしくは屈辱的な取扱もしくは刑\r\n"
L"\r\n"
L"罰を受けることはない。\r\n"
;

m_fontName = L"";
m_localeName = L"";
m_textLength = 0;
m_fontSize = 14;

m_fontName = L"Meiryo UI";
m_localeName = L"ja-jp";
m_readingDirection = ReadingDirectionTopToBottomRightToLeft;

DX::ThrowIfFailed(
m_dwriteFactory->CreateTextFormat(
m_fontName,
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
m_fontSize,
m_localeName,
&m_textFormat
)
);
}

void DWriteVerticalText::CreateDeviceResources()
{
DirectXBase::CreateDeviceResources();

m_sampleOverlay = ref new SampleOverlay();

m_sampleOverlay->Initialize(
m_d2dDevice.Get(),
m_d2dContext.Get(),
m_wicFactory.Get(),
m_dwriteFactory.Get(),
"DirectWrite vertical text sample"
);

DX::ThrowIfFailed(
m_d2dContext->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&m_blackBrush
)
);
}

void DWriteVerticalText::CreateWindowSizeDependentResources()
{
DirectXBase::CreateWindowSizeDependentResources();

D2D1_SIZE_F size = m_d2dContext->GetSize();

m_flowLayoutSource = new FlowLayoutSource;
m_flowLayoutSink = new FlowLayoutSink(m_dwriteFactory.Get());
m_flowLayout = new FlowLayout(m_dwriteFactory.Get());
m_flowLayout->SetTextFormat(m_textFormat.Get());
m_flowLayout->SetReadingDirection(m_readingDirection);
m_flowLayout->SetReadingDirection(m_readingDirection);

if (m_textLength == 0)
{
m_textLength = static_cast<UINT32>(wcsnlen(m_text, UINT32_MAX));
}

m_flowLayout->SetText(m_text, m_textLength);
m_flowLayout->AnalyzeText();
m_flowLayoutSource->SetSize(size.width, size.height);
m_flowLayoutSource->Reset();
m_flowLayoutSink->Reset();
m_flowLayout->FlowText(m_flowLayoutSource.Get(), m_flowLayoutSink.Get());
}

void DWriteVerticalText::Render()
{
m_d2dContext->BeginDraw();
m_d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::CornflowerBlue));
m_d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());

m_flowLayoutSink->DrawGlyphRuns(m_d2dContext.Get(), m_renderingParams.Get(), m_blackBrush.Get());

HRESULT hr = m_d2dContext->EndDraw();

if (hr == D2DERR_RECREATE_TARGET)
{
m_d2dContext->SetTarget(nullptr);
m_d2dTargetBitmap = nullptr;
CreateWindowSizeDependentResources();
}
else
{
DX::ThrowIfFailed(hr);
}

m_sampleOverlay->Render();
}

void DWriteVerticalText::Initialize(
_In_ CoreApplicationView applicationView
)
{
applicationView->Activated +=
ref new TypedEventHandler<CoreApplicationView, IActivatedEventArgs>(this, &DWriteVerticalText::OnActivated);
}

void DWriteVerticalText::SetWindow(
_In_ CoreWindow window
)
{
window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);

window->SizeChanged +=
ref new TypedEventHandler<CoreWindow, WindowSizeChangedEventArgs>(this, &DWriteVerticalText::OnWindowSizeChanged);

DisplayProperties::LogicalDpiChanged +=
ref new DisplayPropertiesEventHandler(this, &DWriteVerticalText::OnLogicalDpiChanged);

DirectXBase::Initialize(window, DisplayProperties::LogicalDpi);
}

void DWriteVerticalText::Load(
Platform::String entryPoint
)
{
}

void DWriteVerticalText::Run()
{
m_window->Activate();

Render();
Present();

m_window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
}

void DWriteVerticalText::Uninitialize()
{
}

void DWriteVerticalText::OnWindowSizeChanged(
_In_ CoreWindow sender,
_In_ WindowSizeChangedEventArgs args
)
{
UpdateForWindowSizeChange();
Render();
Present();
}

void DWriteVerticalText::OnLogicalDpiChanged(
_In_ Platform::Object sender
)
{
SetDpi(DisplayProperties::LogicalDpi);
Render();
Present();
}

void DWriteVerticalText::OnActivated(
_In_ CoreApplicationView applicationView,
_In_ IActivatedEventArgs args
)
{
m_window->Activate();
}

IFrameworkView DirectXAppSource::CreateView()
{
return ref new DWriteVerticalText();
}

[Platform::MTAThread]
int main(Platform::Array<Platform::String>)
{
auto directXAppSource = ref new DirectXAppSource();
CoreApplication::Run(directXAppSource);
return 0;
}

按下F5,运行看效果