基於VisualStudio11開發Windows8的Direct2D-Metro應用程式範例(2) 國際化文字展示

來源:互聯網
上載者:User

Direct2D是美國微軟公司用來取代 DirectDraw 以及GDI, GDI+等新技術[1],主要提供2D 動畫的硬體加速,目前只支援 Windows Vista SP2以及Windows 7以上[2]。
 
Direct2D技術架構於Direct3D 10.1 API 之上,能夠透過硬體加速功能來建立2D圖形,而且完全支援透明和Alpha混合。Direct2D 亦支援軟體實現(Software rasterizer),亦即在顯卡不支援硬體加速情況下,Direct2D 仍可以使用軟體方式描繪,且效果仍優於GDI。
 
Direct2D 可以使用DXGI(DirectX Graphics Infrastructure) 與互動操作,Direct2D還能很好的支援DirectWrite。
 
Direct2D的支援高品質的渲染,具有以下特點:
 支援ClearType 文字的呈現方式(DirectWrite 提供)
 消除原圖鋸齒狀(Per primitive antialiasing)
 幾何形狀(直線,曲線)和位元影像繪製和填寫。
 純色(Solid color)、線性。
 描繪中介層。
 多元的幾何操作(如unions, intersections, widening, outlining等)

在微軟VS11提供了Direct模板

 

我們建立一個應用程式以後,

插入下列代碼

請見程式碼分析

 

 

 

 

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,運行看效果

 

趕緊下載VS11體驗吧

http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.