如何激活应用(DirectX 和 C++)

本主题介绍了如何为通用 Windows 平台 (UWP) DirectX 应用定义激活体验。

注册应用激活事件处理程序

首先,注册以处理 CoreApplicationView::Activated 事件,当启动你的应用并由操作系统对你的应用进行初始化时会引发该事件。

将此代码添加到你的查看提供程序(在此示例中称为 MyViewProvider)的 IFrameworkView::Initialize 方法的实现中:

void App::Initialize(CoreApplicationView^ applicationView)
{
    // Register event handlers for the app lifecycle. This example includes Activated, so that we
    // can make the CoreWindow active and start rendering on the window.
    applicationView->Activated +=
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
  
  //...

}

为应用激活 CoreWindow 实例

当你的应用启动时,你必须为你的应用获取对 CoreWindow 的引用。 CoreWindow 包含你的应用用于处理窗口事件的窗口事件消息调度程序。 通过调用 CoreWindow::GetForCurrentThread 在你的回调中为应用激活事件获取此引用。 获取此引用后,通过调用 CoreWindow::Activate 激活主应用窗口。

void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
    // Run() won't start until the CoreWindow is activated.
    CoreWindow::GetForCurrentThread()->Activate();
}

为主屏窗口启动处理事件消息

对于应用的 CoreWindow,你的回调作为由 CoreDispatcher 处理的事件消息发生。 如果你没有从你的应用的主回路调用 CoreDispatcher::ProcessEvents(在你的查看提供程序的 IFrameworkView::Run 方法中实现),那么将不会调用此回调。

// This method is called after the window becomes active.
void App::Run()
{
    while (!m_windowClosed)
    {
        if (m_windowVisible)
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);

            m_main->Update();

            if (m_main->Render())
            {
                m_deviceResources->Present();
            }
        }
        else
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
        }
    }
}