How to activate an app (DirectX and C++)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

This topic shows how to define the activation experience for a Windows Runtime app using DirectX with C++.

Register the app activation event handler

First, register to handle the CoreApplicationView::Activated event, which is raised when your app is started and initialized by the operating system.

Note  For Windows Phone Store apps, this method is called each time the user launches the app from Start tile or app list, even when the app is currently suspended. Apps should check the activation parameters and decide whether to resume the previous experience or present a new screen when launched with new parameters.

Add this code to your implementation of the IFrameworkView::Initialize method of your view provider (named MyViewProvider in the example):

using namespace concurrency;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;

MyViewProvider::MyViewProvider() :
    // class fields
    // ...
    bool m_visible;
    bool m_windowsClosed;
{

}

void MyViewProvider::Initialize(
    _In_ CoreApplicationView^ applicationView
    )
{

   // ...

    applicationView->Activated +=
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &DirectXApp::OnActivated);

   // ...

}

Activate the CoreWindow instance for the app

When your app starts, you must obtain a reference to the CoreWindow for your app. CoreWindow contains the window event message dispatcher that your app uses to process window events. Obtain this reference in your callback for the app activation event by calling CoreWindow::GetForCurrentThread. Once you have obtained this reference, do the following:

  • Create and attach an event handler for the main app window activation event, CoreWindow::Activated. This event is fired when the main app window gains or loses focus.
  • Activate the main app window by calling CoreWindow::Activate.
void MyViewProvider::OnActivated(
    _In_ CoreApplicationView^ /* applicationView */,
    _In_ IActivatedEventArgs^ /* args */
    )
{
    CoreWindow::GetForCurrentThread()->Activated +=
        ref new TypedEventHandler<CoreWindow^, WindowActivatedEventArgs^>(this, &MyViewProvider::OnWindowActivationChanged);
    CoreWindow::GetForCurrentThread()->Activate();
}

Handle any changes to the main app window state

Now, implement a method to handle any changes in the state of main app window (CoreWindow). In the following sample, you handle:

The complete set of CoreWindow states are enumerated in CoreWindowActivationState.

void MyViewProvider::OnWindowActivationChanged(
    _In_ Windows::UI::Core::CoreWindow^ /* sender */,
    _In_ Windows::UI::Core::WindowActivatedEventArgs^ args
    )
{
    if (args->WindowActivationState == CoreWindowActivationState::Deactivated)
    {
        // CoreWindow does not have focus
    }
    else if (args->WindowActivationState == CoreWindowActivationState::CodeActivated
        || args->WindowActivationState == CoreWindowActivationState::PointerActivated)
    {
        // CoreWindow has focus
    }
}

Start processing event message for the main app window

Your callbacks occur as event messages processed by the CoreDispatcher for the app's CoreWindow. This callback will not be invoked if you do not call CoreDispatcher::ProcessEvents from your app's main loop (implemented in the IFrameworkView::Run method of your view provider).

// m_visible and m_windowsClose are class-level bool variables on MyViewProvider.

// ...

void MyViewProvider::Run()
{
    // your app's main loop!
    while (!m_windowClosed) // the app window is NOT closed
    {
        if (m_visible) // and if the app window is visible
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
                                                           CoreProcessEventsOption::ProcessAllIfPresent
                                                           );
        }
        else 
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
                                                           CoreProcessEventsOption::ProcessOneAndAllPending
                                                           );
        }
    }
}

How to suspend an app (DirectX and C++)

How to resume an app (DirectX and C++)

Guidelines for app suspend and resume

Application lifecycle (Store apps)