C++/DirectX/CX: WinRT investigation, creating the WinRT project

Next Blog: Consuming the WinRT Project in a C++ app

Well working with WinRT, this is a mystery to many college students, especially the motivation.to use WinRT.  Basically the motivation is to be able to write code using the best possible language and then consume it in a different language or even the same language.

In most of the documentation it seems as if you would only use WinRT with C++, this isn’t the case.  You could use C# say to build a XML Reader and Writer and then consume it in C++.

Let’s dig into the creation of the WinRT project in this blog, make sure to scroll down to see how to create the WinRT Project.

Creating the WinRT project:

First open Visual Studio 2012 or Visual Studio 2013 and create a new Windows Store Project of the type WinRT, make sure to change  the filename to WinRTDemo and change the class filenames to match:

image

Header file

Ok, the following in the “pretend” physics header file, we are going to just do a simple calculator, but it could be the more complicated physics header.

Scroll down to get the code to copy and paste into your app.

image

//************ Begin the copy and pasting of your code, my app is named WinRTDemo*******

#pragma once

namespace WinRTDemo
{
    ref class Rule;
    public delegate void DivideByZeroHandler(Rule^ sender);

    public ref class Rule sealed
    {
       
    public:
        Rule();

        // ctor
        Rule(double initial);
        Rule();

        // operations
        void Bounce_Physics(double value);
        void Add_Physics(double value);
        void Subtract_Physics(double value);
        void Multiply_Physics(double value);
         void Divide_Physics(double value);

        void Reset_Physics(double value);
        void Reset_Physics();

        property double Result_Physics
        {
            double get();
        }

        event DivideByZeroHandler^ DivideByZero;

    private:
        double _result;
    };
}

//*****************End copy and paste code for header file*************

Now for the code in the source file

Scroll down for the copy and paste code

image

//***********Copy and paste code***************

// Class1.cpp
#include "pch.h"
#include "Physics.h"

using namespace WinRTDemo;
using namespace Platform;

Rule::Rule(double initial) : _result(initial)
{   
}

Rule::Rule() : _result(0)
{
}

void Rule::Add_Physics(double value)
{
    _result += value;
}
void Rule::Subtract_Physics(double value)
{
    _result -= value;
}
void Rule::Multiply_Physics(double value)
{
    _result *= value;
}
void Rule::Divide_Physics(double value)
{
    _result /= value;
}
void Rule::Reset_Physics()
{
    _result = 0.0;
}

//********* End of copy and paste ***********************

That’s it you have a simple example of a WinRT code that you can consume in other languages or even the same language.