Targeting Modern C++ and Windows Store apps samples for Windows 8.1 – Part 1 of n

I have previously mentioned that I will not be updating “Modern C++ and Windows Store apps” to have an edition exclusively for Windows 8.1. My reason behind the decision is simple: The concepts and technologies I have introduced in the book continue to be relevant in Windows 8.1 and all of the samples will work on Windows 8.1. Having said that, during Windows 8.1 some WinRT APIs were deprecated and you will see warnings when you compile the samples for Windows 8.1. In this series of blog posts, I will show you how, with little effort and small changes, you can recompile the samples for Windows 8.1.

Note: This is just the first post in a series.

Let us begin with the very first example from the book, the DXBouncingBall sample. When you attempt to open the solution file in Visual Studio 2013, Visual Studio prompts you to retarget the solution for Windows 8.1 as shown in the dialog below. If you choose not to retarget, then you will have to install the Windows 8 development tools in order to build the solution.

For the purpose of this exercise, go ahead and retarget the solution to Windows 8.1 by first clicking on OK in the above dialog and then right click on the project in Solution Explorer and choose Retarget to Windows 8.1 as shown below.

Once you ask Visual Studio to retarget the solution to Windows 8.1, Visual Studio makes the necessary changes and opens the project. Once the project is fully loaded, try building the solution. You should see a series of warning messages indicating the usage of deprecated APIs. The warnings are all listed below.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(35): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayPropertiesEventHandler ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayProperties ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(35): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(41): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(41): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpiChanged::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(44): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(44): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::OrientationChanged::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(47): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(47): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::DisplayContentsInvalidated::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(61): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(177): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayPropertiesEventHandler ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayProperties ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(177): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(184): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::CurrentOrientation::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(214): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::CurrentOrientation::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

Time for action! If you were to closely observe the warning messages, it should be pretty clear that there is one runtimeclass and some methods/properties/events associated with that particular class that are generating the warnings. That class happens to be the Windows::Graphics::Display::DisplayProperties. I will not go into the finer details of why this class has been deprecated in Windows 8.1 but for a teaser, it has to do with better multi-mon support J. Anyways back to our exercise.

The warning also informs us to use the newer class, DisplayInformation. This is one of the benefits of the great Intellisense support for WinRT apps in Visual Studio. All we need to do now, is replace the usage of DisplayProperties with DisplayInformation. Let’s begin the steps. I am showing you a table of the Windows 8 code and the new Windows 8.1 code to make the illustration easier. Each item in the table corresponds to the warnings in the order they appear.

 

File Name

Line Number in Windows 8 Code

Windows 8 Code

Windows 8.1 Code

DirectXPage.xaml.cpp

35

m_renderer->Initialize(

Window::Current->CoreWindow,

SwapChainPanel,

DisplayProperties::LogicalDpi

);

auto info = DisplayInformation::GetForCurrentView();

 

m_renderer->Initialize(


Window::Current->CoreWindow,

SwapChainPanel,

info->LogicalDpi

);

DirectXPage.xaml.cpp

41

DisplayProperties::LogicalDpiChanged +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnLogicalDpiChanged);

info->DpiChanged +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnLogicalDpiChanged );

DirectXPage.xaml.cpp

44

DisplayProperties::OrientationChanged +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnOrientationChanged);

info->OrientationChanged +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnOrientationChanged );

DirectXPage.xaml.cpp

47

DisplayProperties::DisplayContentsInvalidated +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnDisplayContentsInvalidated);

info->DisplayContentsInvalidated +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnDisplayContentsInvalidated );

DirectXPage.xaml.cpp

61

m_renderer->SetDpi(DisplayProperties::LogicalDpi);

m_renderer->SetDpi( DisplayInformation::GetForCurrentView()->LogicalDpi );

DirectXBase.cpp

177

if (m_dpi != DisplayProperties::LogicalDpi)

{


return;

}

if ( m_dpi != DisplayInformation::GetForCurrentView()->LogicalDpi )

{


return;

}

DirectXBase.cpp

184

m_orientation != DisplayProperties::CurrentOrientation

m_orientation != DisplayInformation::GetForCurrentView()->CurrentOrientation

DirectXBase.cpp

214

m_orientation = DisplayProperties::CurrentOrientation

m_orientation = DisplayInformation::GetForCurrentView()->CurrentOrientation

DirectXPage.xaml.h

41-43

void OnLogicalDpiChanged(Platform::Object^ sender);

void OnOrientationChanged(Platform::Object^ sender);

void OnDisplayContentsInvalidated(Platform::Object^ sender);

void OnLogicalDpiChanged( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

void OnOrientationChanged( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

void OnDisplayContentsInvalidated( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

Change the method signatures appropriately in the DirectXPage.xaml.cpp too and rebuild the solution. It should build clean with no warnings and your app should work on Windows 8.1 without any issues.

Try to resize the application window to different sizes and see how resizing and redrawing are all handled smoothly without having to explicitly write code. Is it not cool to write future proof code? J

 

Have fun!

-Sridhar

One thought on “Targeting Modern C++ and Windows Store apps samples for Windows 8.1 – Part 1 of n

Leave a Reply

Your email address will not be published. Required fields are marked *