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模板
我們建立一個應用程式以後,
插入下列代碼
請見程式碼分析
#include "pch.h"#include "DWriteHelloWorld.h"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;DWriteHelloWorld::DWriteHelloWorld(){}void DWriteHelloWorld::CreateDeviceIndependentResources(){ DirectXBase::CreateDeviceIndependentResources(); DX::ThrowIfFailed( m_dwriteFactory->CreateTextFormat( L"Gabriola", nullptr, DWRITE_FONT_WEIGHT_REGULAR, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 64.0f, L"en-US", //locale &m_textFormat ) ); // Center the text horizontally and vertically. m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); m_textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);}void DWriteHelloWorld::CreateDeviceResources(){ DirectXBase::CreateDeviceResources(); m_sampleOverlay = ref new SampleOverlay(); m_sampleOverlay->Initialize( m_d2dDevice.Get(), m_d2dContext.Get(), m_wicFactory.Get(), m_dwriteFactory.Get(), "D2D Hello World sample by Microsoft MVP Yincheng" ); DX::ThrowIfFailed( m_d2dContext->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::Black), &m_blackBrush ) );}void DWriteHelloWorld::CreateWindowSizeDependentResources(){ DirectXBase::CreateWindowSizeDependentResources(); Platform::String^ text = "Hello World By Microsoft MVP -Yincheng!"; D2D1_SIZE_F size = m_d2dContext->GetSize(); //建立文本輸出裝置 DX::ThrowIfFailed( m_dwriteFactory->CreateTextLayout( text->Data(), text->Length(), m_textFormat.Get(), size.width, size.height, &m_textLayout ) ); DWRITE_TEXT_RANGE textRange = {21, 12}; //設定文字大小 m_textLayout->SetFontSize(100.0f, textRange); //建立圖形文字顯示裝置 DX::ThrowIfFailed( m_dwriteFactory->CreateTypography( &m_textTypography ) ); DWRITE_FONT_FEATURE fontFeature = {DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_6, 1}; m_textTypography->AddFontFeature(fontFeature); //移動文字的長度 textRange.length = text->Length(); textRange.startPosition = 0; //顯示文字 DX::ThrowIfFailed( m_textLayout->SetTypography( m_textTypography.Get(), textRange ) ); //設定文字的長度與開始位置 textRange.length = 12; textRange.startPosition = 21; //設定水平線 m_textLayout->SetUnderline(TRUE, textRange); m_textLayout->SetFontWeight(DWRITE_FONT_WEIGHT_BOLD, textRange);}void DWriteHelloWorld::Render(){ m_d2dContext->BeginDraw(); m_d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::CornflowerBlue)); m_d2dContext->SetTransform(D2D1::Matrix3x2F::Identity()); m_d2dContext->DrawTextLayout( D2D1::Point2F(0.0f, 0.0f), m_textLayout.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 DWriteHelloWorld::Initialize( _In_ CoreApplicationView^ applicationView ){ applicationView->Activated += ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &DWriteHelloWorld::OnActivated); CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs^>(this, &DWriteHelloWorld::OnSuspending); CoreApplication::Resuming += ref new EventHandler<Platform::Object^>(this, &DWriteHelloWorld::OnResuming);}void DWriteHelloWorld::SetWindow( _In_ CoreWindow^ window ){ window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); window->SizeChanged += ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &DWriteHelloWorld::OnWindowSizeChanged); DisplayProperties::LogicalDpiChanged += ref new DisplayPropertiesEventHandler(this, &DWriteHelloWorld::OnLogicalDpiChanged); DirectXBase::Initialize(window, DisplayProperties::LogicalDpi);}void DWriteHelloWorld::Load( Platform::String^ entryPoint ){}void DWriteHelloWorld::Run(){ Render(); Present(); m_window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);}void DWriteHelloWorld::Uninitialize(){}void DWriteHelloWorld::OnWindowSizeChanged( _In_ CoreWindow^ sender, _In_ WindowSizeChangedEventArgs^ args ){ UpdateForWindowSizeChange(); m_sampleOverlay->UpdateForWindowSizeChange(); Render(); Present();}void DWriteHelloWorld::OnLogicalDpiChanged( _In_ Platform::Object^ sender ){ SetDpi(DisplayProperties::LogicalDpi); Render(); Present();}void DWriteHelloWorld::OnActivated( _In_ CoreApplicationView^ applicationView, _In_ IActivatedEventArgs^ args ){ m_window->Activate();}void DWriteHelloWorld::OnSuspending( _In_ Platform::Object^ sender, _In_ SuspendingEventArgs^ args ){}void DWriteHelloWorld::OnResuming( _In_ Platform::Object^ sender, _In_ Platform::Object^ args ){}IFrameworkView^ DirectXAppSource::CreateView(){ return ref new DWriteHelloWorld();}[Platform::MTAThread]int main(Platform::Array<Platform::String^>^){ auto directXAppSource = ref new DirectXAppSource(); CoreApplication::Run(directXAppSource); return 0;}
趕緊下載VS11體驗吧
http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144