【问题标题】:Qt 5.5 - Windows API not Working as usualQt 5.5 - Windows API 无法正常工作
【发布时间】:2016-03-23 16:09:22
【问题描述】:

我在 Windows 10 上使用 Qt 5.5,我想在 Foreground 中打开 QWidget 并希望 关注 LineEdit,类似于 Windows 上的 RUN (WIN + R)。问题是应用程序在后台运行,我只有一个键盘记录器来注册一个快捷键(LCTRL + LWIN + T)来切换窗口(显示+焦点/隐藏)。

如果按下快捷键,我执行以下代码:

   if(this->isHidden()){

      this->show();

      //Windows API Methods:
      SetActiveWindow((HWND) this->winId());
      SetForegroundWindow((HWND) this->winId());
      SetFocus((HWND) this->winId());

      this->_edit->setFocus();

      qDebug() << "[OUT][DONT WORKING] Window shoud be shown and focused";
    }else{
      this->hide();
      qDebug() << "[OUT][WORKING] Window shoud be hidden";
    }

如果我现在按 LCTRL + LWIN + T,它会在后台打开窗口,这不是我想要的。有人可以解释为什么这不起作用吗?如果窗口在前台打开并且文本框聚焦,我该怎么办?而且我不想设置标志StayAlwaysOnTop,因为那时文本字段仍然没有聚焦。

我希望你能帮助我。 非常非常感谢!

【问题讨论】:

    标签: c++ windows qt c++11 winapi


    【解决方案1】:

    [已解决]

    这是不可能的,因为要设置 ForegroundWindow 我必须获取当前输入,我做了以下操作:

    构造函数:

    HWND wId = (HWND) winId();
    DWORD pId = GetWindowThreadProcessId(wId, NULL);
    AllowSetForegroundWindow(pId);
    

    onShortcutPressed:

    this->show();
    HWND wId = (HWND) winId();
    DWORD pId = GetWindowThreadProcessId(wId, NULL);
    DWORD fpId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
    
    //Attach Thread to get the Input - i am now allowed to set the Foreground window!
    AttachThreadInput(fpId, pId, true);
    SetActiveWindow(wId);
    SetForegroundWindow(wId);
    SetFocus(wId);
    AttachThreadInput(fpId, pId, false);
    

    它现在可以工作了!

    【讨论】:

      猜你喜欢
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多