基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(1)hello world

 

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模板

基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(1)hello world_第1张图片

 

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

插入下列代码

请见代码分析

#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;
}


 

基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(1)hello world_第2张图片

 

赶紧下载VS11体验吧

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

 

你可能感兴趣的:(windows,object,Microsoft,代码分析,alignment,Primitive)