# 五、附录 B: `DirectXPage.xaml`类列表

处理背景颜色变化和 Direct2D (XAML)应用程序其他方面的事件和方法应该从这个类中移除，因为我们将从 SimpleTextRenderer 类中移除这些方法。这些类文件的顶部是对命名空间 PrinterApplication 的引用；这必须更改以匹配您的应用程序的名称空间(我已经用绿色突出显示了该行)。

DirectXPage.xaml.h

```cpp
//

// DirectXPage.xaml.h

// Declaration of the DirectXPage.xaml class.

//

#pragma once

#include "DirectXPage.g.h"

#include "SimpleTextRenderer.h"

#include "BasicTimer.h"

namespace PrinterApplication {

    [Windows::Foundation::Metadata::WebHostHidden]

     public ref class DirectXPage sealed  {

     public:

          DirectXPage();

     private:

          void OnPointerMoved(Platform::Object^ sender,

               Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args);

          void OnPointerReleased(Platform::Object^ sender,

                Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args);

          void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender,

                Windows::UI::Core::WindowSizeChangedEventArgs^ args);

          void OnLogicalDpiChanged(Platform::Object^ sender);

          void OnOrientationChanged(Platform::Object^ sender);

          void OnDisplayContentsInvalidated(Platform::Object^ sender);

          void OnRendering(Object^ sender, Object^ args);

          Windows::Foundation::EventRegistrationToken m_eventToken;

          SimpleTextRenderer^ m_renderer;

          bool m_renderNeeded;     

          BasicTimer^ m_timer;

          Windows::Graphics::Printing::PrintManager^ m_printManager;    

     internal:

          // Print task requested event handler method

          void SetPrintTask(_In_

     Windows::Graphics::Printing::PrintManager^ sender,

          _In_

     Windows::Graphics::Printing::PrintTaskRequestedEventArgs^ args);

     };

}
```

```cpp
//

// DirectXPage.xaml.cpp

// Implementation of the DirectXPage.xaml class.

//

#include "pch.h"

#include "DirectXPage.xaml.h"

#include "DocSource.h"

using namespace PrinterApplication;

using namespace Platform;

using namespace Windows::Foundation;

using namespace Windows::Foundation::Collections;

using namespace Windows::Graphics::Display;

using namespace Windows::UI::Input;

using namespace Windows::UI::Core;

using namespace Windows::UI::Xaml;

using namespace Windows::UI::Xaml::Controls;

using namespace Windows::UI::Xaml::Controls::Primitives;

using namespace Windows::UI::Xaml::Data;

using namespace Windows::UI::Xaml::Input;

using namespace Windows::UI::Xaml::Media;

using namespace Windows::UI::Xaml::Navigation;

using namespace Windows::Graphics::Printing;

DirectXPage::DirectXPage() :m_renderNeeded(true) {

     InitializeComponent();

     m_renderer = ref new SimpleTextRenderer();

     m_renderer->Initialize(

          Window::Current->CoreWindow,

          SwapChainPanel,

          DisplayProperties::LogicalDpi

          );

     Window::Current->CoreWindow->SizeChanged +=

          ref new TypedEventHandler<CoreWindow^,

          WindowSizeChangedEventArgs^>(this, &DirectXPage::OnWindowSizeChanged);

     DisplayProperties::LogicalDpiChanged +=

          ref new DisplayPropertiesEventHandler(this,

                &DirectXPage::OnLogicalDpiChanged);

     DisplayProperties::OrientationChanged +=

          ref new DisplayPropertiesEventHandler(this,

                &DirectXPage::OnOrientationChanged);

     DisplayProperties::DisplayContentsInvalidated +=

          ref new DisplayPropertiesEventHandler(this,

                &DirectXPage::OnDisplayContentsInvalidated);  

     m_eventToken = CompositionTarget::Rendering::add(

          ref new EventHandler<Object^>(this, &DirectXPage::OnRendering));

     m_timer = ref new BasicTimer();

     // Grab the print manager for the current view

     m_printManager =

          Windows::Graphics::Printing::PrintManager::GetForCurrentView();

     // Add an event handler to capture when the user requests a print task

     m_printManager->PrintTaskRequested +=

          ref new TypedEventHandler<PrintManager^,

                PrintTaskRequestedEventArgs^>(this,

                     &DirectXPage::SetPrintTask);

}

 

void DirectXPage::OnPointerMoved(Object^ sender, PointerRoutedEventArgs^ args) {

     m_renderNeeded = true;

}

 

void DirectXPage::OnPointerReleased(Object^ sender,

     PointerRoutedEventArgs^ args) { }

 

void DirectXPage::OnWindowSizeChanged(CoreWindow^ sender,

     WindowSizeChangedEventArgs^ args) {

     m_renderer->UpdateForWindowSizeChange();

     m_renderNeeded = true;

}

 

void DirectXPage::OnLogicalDpiChanged(Object^ sender) {

     m_renderer->SetDpi(DisplayProperties::LogicalDpi);

     m_renderNeeded = true;

}

 

void DirectXPage::OnOrientationChanged(Object^ sender) {

     m_renderer->UpdateForWindowSizeChange();

     m_renderNeeded = true;

}

 

void DirectXPage::OnDisplayContentsInvalidated(Object^ sender) {

     m_renderer->ValidateDevice();

     m_renderNeeded = true;

}

 

void DirectXPage::OnRendering(Object^ sender, Object^ args) {

     if (m_renderNeeded) {

          m_timer->Update();

          m_renderer->Update(m_timer->Total, m_timer->Delta);

          m_renderer->Render();

          m_renderNeeded = false;

     }

}

 

void DirectXPage::SetPrintTask(_In_ PrintManager^ sender,

     _In_ PrintTaskRequestedEventArgs^ args) {

// Create a new source requested handler

PrintTaskSourceRequestedHandler^ sourceRequestedHandler = ref new

     PrintTaskSourceRequestedHandler(

     [this](PrintTaskSourceRequestedArgs^ args)-> void {

          Microsoft::WRL::ComPtr<CDocumentSource> documentSource;

          DX::ThrowIfFailed (

                Microsoft::WRL::MakeAndInitialize<CDocumentSource>(

                     &documentSource,

                     reinterpret_cast<IUnknown*>(m_renderer)));

                // Cast the document to an object

                IPrintDocumentSource^ objSource(

                     reinterpret_cast<IPrintDocumentSource^>(documentSource.Get())

                );

          args->SetSource(objSource);

          });

// Create the print task

PrintTask^ printTask = args->Request->CreatePrintTask(L"Direct 2D Printing Example", sourceRequestedHandler);

}
```
