【发布时间】:2019-10-03 13:18:07
【问题描述】:
我正在尝试通过单独的应用程序控制 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++ 没有窗口的内在概念。因此,为了移动窗口,您需要使用相关的系统 API,在本例中为 Windows API。请根据 Windows API 不工作的原因,而不是如何避免 Windows API 来重新表述您的问题。
-
@JaMiT 你是什么意思?我并不是要避免使用 Windows API。我一般尝试了 wpf 应用程序的典型 api,但它不起作用。我还没有找到允许我调整 UWP 应用程序的 UWP api。至于为什么不起作用,我猜是UWP不允许访问?
-
你可以get the window's HWND。但在另一个过程中这样做不太可能有好的结果。最重要的是,它可能已被暂停。 UWP 应用需要自己处理这个问题。
-
@JianweiLee 当您声明它们不起作用时(而不是询问它们为什么不起作用),听起来您不想要 Windows API 解决方案。加上标题中的短语“native C++ code”表明您正在寻找不使用 API 的解决方案。
-
@JianweiLee 嗯......如果你真的问过你想要的问题,也许会更容易理解?我突然想到,您的问题中唯一的问号是在代码注释中解释了
if条件的含义。所以从技术上讲,除非有人猜测,否则还没有要回答的问题。此外,明确的问题可以帮助集中答案。
标签: c++ uwp windows-mixed-reality