在Mac OS中移动第三方窗口

时间:2018-07-11 10:36:42

标签: c++ macos qt

我尝试在Mac OS中支持该程序。该程序必须将另一个程序的一个特定窗口(该程序必须打开多个窗口)移动到指定的监视器,并将其展开到全屏。对于Windows和Linux,这是使用本机API实现的,但对于MacOS,它没有找到可从其应用程序更改Windows的API。可以找到获取窗口ID但不更改其状态的方法。

m_currentWindow->mac = 0;
CGWindowListOption option = kCGWindowListOptionAll;
CGWindowID id = 0;
CFArrayRef windows = CGWindowListCreate(option, id);
if(windows == nullptr)
{
  qCCritical(actOp) << "windows is null";
  return;
}
CFArrayRef desc = CGWindowListCreateDescriptionFromArray(windows);
if(desc == nullptr)
{
  qCCritical(actOp) << "windows description is null";
  return;
}
CFIndex count = CFArrayGetCount(desc);
qCDebug(actOp) << "finded " << count << " window";
QList<quint32> allWindows;
for(CFIndex i=0; i<count; i++)
{
  CFDictionaryRef dictionary = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(desc, i));
  quint32 id;
  CFNumberGetValue(static_cast<CFNumberRef>(CFDictionaryGetValue(dictionary, kCGWindowNumber)),
                   kCFNumberSInt32Type, &id);
  allWindows << id;
}
CFRelease(desc);
CFRelease(windows);
do
{
  QThread::currentThread()->msleep(100);
  windows = CGWindowListCreate(option, id);
  desc = CGWindowListCreateDescriptionFromArray(windows);
  count = CFArrayGetCount(desc);
  for(CFIndex i=0; i<count; i++)
  {
    CFDictionaryRef dictionary = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(desc, i));
    quint32 id;
    CFNumberGetValue(static_cast<CFNumberRef>(CFDictionaryGetValue(dictionary, kCGWindowNumber)),
                     kCFNumberSInt32Type, &id);
    if(allWindows.contains(id))
      continue;
    QString name = QString::fromCFString(static_cast<CFStringRef>(
                                           CFDictionaryGetValue(dictionary, kCGWindowOwnerName)));
    qCDebug(actOp) << static_cast<CFNumberRef>(CFDictionaryGetValue(dictionary, kCGWindowNumber))
                   << " "
                   << static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, kCGWindowOwnerName));
    if(name.contains(m_browserTitle))
    {
      m_currentWindow->mac = id;
      qCDebug(actOp) << "window is finded";
      return;
    }
    else
      allWindows << id;
  }
  CFRelease(desc);
  CFRelease(windows);
}
while(m_currentWindow->mac == 0 && !timer.hasExpired(maxTime));

我试图朝QWindow的方向看,但是对于方法NSView,只有所需的窗口Window::fromWinId,却找不到如何得到CGWindowID

告诉我您如何实现这样的任务。谢谢。

0 个答案:

没有答案