Bring windo to focus on overlayOpen

pull/130/head
Peter Repukat 3 years ago
parent 211d530d25
commit aaacb49a7b

@ -16,6 +16,9 @@ limitations under the License.
#include "SteamTarget.h"
SteamTarget::SteamTarget(int argc, char *argv[]) : window_([this] { run_ = false; })
SteamTarget::SteamTarget(int argc, char *argv[])
: window_([this] { run_ = false; }),
detector_([this](bool overlay_open) { onOverlayChanged(overlay_open); }), target_window_handle_(window_.getSystemHandle())
{
}
@ -29,3 +32,42 @@ int SteamTarget::run()
}
return 1;
}
void SteamTarget::onOverlayChanged(bool overlay_open)
{
window_.setClickThrough(!overlay_open);
focusWindow(target_window_handle_);
}
void SteamTarget::focusWindow(WindowHandle hndl)
{
#ifdef _WIN32
//MH_DisableHook(&GetForegroundWindow); // TODO: when GetForegroundWindow hooked, unhook!
// store last focused window for later restore
last_foreground_window_ = GetForegroundWindow();
const DWORD fg_thread = GetWindowThreadProcessId(last_foreground_window_, nullptr);
//MH_EnableHook(&GetForegroundWindow); // TODO: when GetForegroundWindow hooked, re-hook!
// lot's of ways actually bringing our window to foreground...
const DWORD current_thread = GetCurrentThreadId();
AttachThreadInput(current_thread, fg_thread, TRUE);
SetForegroundWindow(hndl);
SetCapture(hndl);
SetFocus(hndl);
SetActiveWindow(hndl);
EnableWindow(hndl, TRUE);
AttachThreadInput(current_thread, fg_thread, FALSE);
//try to forcefully set foreground window at least a few times
sf::Clock clock;
while (!SetForegroundWindow(hndl) && clock.getElapsedTime().asMilliseconds() < 20)
{
SetActiveWindow(hndl);
Sleep(1);
}
#endif
}

@ -25,7 +25,7 @@ limitations under the License.
#include <dwmapi.h>
#endif
static const bool DEV_MODE = true;
static const bool DEV_MODE = false;
TargetWindow::TargetWindow(std::function<void()> on_close)
: on_close_(std::move(on_close))
@ -97,3 +97,8 @@ void TargetWindow::close()
window_.close();
on_close_();
}
WindowHandle TargetWindow::getSystemHandle() const
{
return window_.getSystemHandle();
}

@ -18,6 +18,14 @@ limitations under the License.
#include <SFML/Graphics/RenderWindow.hpp>
#ifdef _WIN32
#include <Windows.h>
using WindowHandle = HWND;
#else
using WindowHandle = int; // ???
#endif
class TargetWindow {
public:
explicit TargetWindow(std::function<void()> on_close = []() {});
@ -27,6 +35,8 @@ class TargetWindow {
void update();
void close();
WindowHandle getSystemHandle() const;
private:
const std::function<void()> on_close_;
sf::RenderWindow window_;

Loading…
Cancel
Save