首页 > 解决方案 > 通过本机 C++ 代码移动 UWP 应用程序窗口

问题描述

我正在尝试通过单独的应用程序控制 UWP APP(Windows 混合现实门户)的大小和位置。就我而言,为了简单起见,我正在使用控制台应用程序。命令脚本也适用于我想要实现的目标。

我已经尝试过诸如 MoveWindow、SetWindowPos 之类的 Windows api,但它们没有按预期工作,并且 GetWindowRect 返回一个 0,0,0,0 矩形。我可以获得窗口句柄,但不能更改大小/位置。

我这样做的原因是向应用程序发送虚拟鼠标键,以初始化 Windows Mixed Reality 系统的前端位置。发送虚拟键很好,但我无法自动移动 uwp 应用程序本身的位置。

#include <iostream>
#include <ShObjIdl.h>
#include <atlbase.h>
#include <tlhelp32.h>

BOOL CALLBACK EnumWindowsProcBack(HWND windowHandle, LPARAM lParam) {
    DWORD searchedProcessId = (DWORD)lParam;  // This is the process ID we search for (passed from BringToForeground as lParam)
    DWORD windowProcessId = 0;
    GetWindowThreadProcessId(windowHandle, &windowProcessId); // Get process ID of the window we just found
    if (searchedProcessId == windowProcessId) {  // Is it the process we care about?
        //std::cout << "moving window..\n";
        //bool s=MoveWindow(windowHandle, 0, 0, 1920, 1080, true);
        SetWindowPos(
            windowHandle,
            HWND_TOP,
            0,
            0,
            600,
            600,
            SWP_NOSIZE
        );
        return FALSE;  // Stop enumerating windows
    }
    return TRUE;  // Continue enumerating
}

void MoveWindowToFixedLocation(DWORD processId) {
    EnumWindows(&EnumWindowsProcBack, (LPARAM)processId);
}

HRESULT LaunchApp(LPCWSTR AUMID, DWORD &pid)
{
    HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
    if (FAILED(hr))
    {
        wprintf(L"LaunchApp %s: Failed to init COM. hr = 0x%08lx \n", AUMID, hr);
    }
    {
        CComPtr<IApplicationActivationManager> AppActivationMgr = nullptr;
        if (SUCCEEDED(hr))
        {
            hr = CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
                CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&AppActivationMgr));
            if (FAILED(hr))
            {
                wprintf(L"LaunchApp %s: Failed to create Application Activation Manager.hr = 0x%08lx \n", AUMID, hr);
            }
        }
        if (SUCCEEDED(hr))
        {
            //DWORD pid = 0;
            hr = AppActivationMgr->ActivateApplication(AUMID, nullptr, AO_NONE,
                &pid);
            if (FAILED(hr))
            {
                wprintf(L"LaunchApp %s: Failed to Activate App. hr = 0x%08lx \n", AUMID, hr);
            }
        }
    }
    CoUninitialize();
    return hr;
}


int main() {

    DWORD pid = 0;
    LaunchApp(L"Microsoft.Windows.HolographicFirstRun_cw5n1h2txyewy!App", pid);
    //cout << pid;
    MoveWindowToFixedLocation(pid);
}

标签: c++uwpwindows-mixed-reality

解决方案


不可能。UWP 应用在自己的封闭环境中运行。桌面应用程序无法向其发送任何信号。


推荐阅读