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

0 out of 1 rated this helpful - Rate this topic

This topic shows how to define the activation experience for a Windows Store app built for Windows 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 Windows 8.

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 ro 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
                                                           );
        }
    }
}

Related topics

How to suspend an app (DirectX and C++)
How to resume an app (DirectX and C++)
Guidelines for app suspend and resume
Application lifecycle (Windows Store apps)

 

 

Build date: 3/6/2013

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.