protected virtual void InitializeTimer() { this.timer = new System.Timers.Timer(); this.timer.Interval = 1000.0; this.timer.Elapsed += (ElapsedEventHandler)((o, e) => { try { this.Update(); } catch (Exception ex) { this.Log(LogLevel.Error, "更新: {0}", (object)ex.ToString()); } }); this.xivWindowTimer = new System.Timers.Timer(); this.xivWindowTimer.Interval = 1000.0; this.xivWindowTimer.Elapsed += (ElapsedEventHandler)((o, e) => { try { if (!this.Config.IsVisible || !this.PluginConfig.HideOverlaysWhenNotActive) { return; } IntPtr foregroundWindow = NativeMethods.GetForegroundWindow(); if (foregroundWindow == IntPtr.Zero) { return; } uint lpdwProcessId; int windowThreadProcessId = (int)NativeMethods.GetWindowThreadProcessId(foregroundWindow, out lpdwProcessId); string fileName = Process.GetProcessById((int)lpdwProcessId).MainModule.FileName; if (Path.GetFileName(fileName.ToString()) == "ffxiv.exe" || Path.GetFileName(fileName.ToString()) == "ffxiv_dx11.exe" || fileName.ToString() == Process.GetCurrentProcess().MainModule.FileName) { this.Overlay.Visible = true; } else { this.Overlay.Visible = false; } } catch (Exception ex) { this.Log(LogLevel.Error, "最终幻想14窗口监视: {0}", (object)ex.ToString()); } }); }
static void ActiveWindowChangedHandler(object sender, IntPtr changedWindow) { if (!config.HideOverlaysWhenNotActive || changedWindow == IntPtr.Zero) { return; } try { try { NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out uint pid); if (pid == 0) { return; } var exePath = Process.GetProcessById((int)pid).MainModule.FileName; var fileName = Path.GetFileName(exePath.ToString()); gameActive = (fileName == "ffxiv.exe" || fileName == "ffxiv_dx11.exe" || exePath.ToString() == Process.GetCurrentProcess().MainModule.FileName); } catch (System.ComponentModel.Win32Exception ex) { // Ignore access denied errors. Those usually happen if the foreground window is running with // admin permissions but we are not. if (ex.ErrorCode == -2147467259) // 0x80004005 { gameActive = false; } else { logger.Log(LogLevel.Error, "XivWindowWatcher: {0}", ex.ToString()); } } } catch (Exception ex) { logger.Log(LogLevel.Error, "XivWindowWatcher: {0}", ex.ToString()); } UpdateOverlays(); }
/// <summary> /// コンフィグのオーバーレイ設定を基に、オーバーレイを初期化・登録します。 /// </summary> private void InitializeOverlays() { // オーバーレイ初期化 this.Overlays = new List <IOverlay>(); foreach (var overlayConfig in this.Config.Overlays) { var parameters = new NamedParameterOverloads(); parameters["config"] = overlayConfig; parameters["name"] = overlayConfig.Name; var overlay = (IOverlay)Registry.Container.Resolve(overlayConfig.OverlayType, parameters); if (overlay != null) { RegisterOverlay(overlay); } else { Logger.Log(LogLevel.Error, "InitPlugin: Could not find addon for {0}.", overlayConfig.Name); } } xivWindowTimer = new System.Timers.Timer(); xivWindowTimer.Interval = 1000; xivWindowTimer.Elapsed += (o, e) => { if (Config.HideOverlaysWhenNotActive) { try { var shouldBeVisible = true; try { uint pid; var hWndFg = NativeMethods.GetForegroundWindow(); if (hWndFg == IntPtr.Zero) { return; } NativeMethods.GetWindowThreadProcessId(hWndFg, out pid); var exePath = Process.GetProcessById((int)pid).MainModule.FileName; if (Path.GetFileName(exePath.ToString()) == "ffxiv.exe" || Path.GetFileName(exePath.ToString()) == "ffxiv_dx11.exe" || exePath.ToString() == Process.GetCurrentProcess().MainModule.FileName) { shouldBeVisible = true; } else { shouldBeVisible = false; } } catch (System.ComponentModel.Win32Exception ex) { // Ignore access denied errors. Those usually happen if the foreground window is running with // admin permissions but we are not. if (ex.ErrorCode == -2147467259) // 0x80004005 { shouldBeVisible = false; } else { Logger.Log(LogLevel.Error, "XivWindowWatcher: {0}", ex.ToString()); } } foreach (var overlay in this.Overlays) { if (overlay.Config.IsVisible) { overlay.Visible = shouldBeVisible; } } } catch (Exception ex) { Logger.Log(LogLevel.Error, "XivWindowWatcher: {0}", ex.ToString()); } } }; xivWindowTimer.Start(); }
private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { if (!enabled) { return; } if (eventType != NativeMethods.EVENT_SYSTEM_FOREGROUND && eventType != NativeMethods.EVENT_SYSTEM_MINIMIZEEND && eventType != NativeMethods.EVENT_SYSTEM_MINIMIZESTART) { return; } bool newVisible = false; bool isGameWindow = false; var foregroundHwnd = NativeMethods.GetForegroundWindow(); if (!NativeMethods.IsWindow(foregroundHwnd)) { return; } if (NativeMethods.GetWindowThreadProcessId(foregroundHwnd, out uint pid) == 0) { return; } if (pid == actPid) { newVisible = true; } else { var sb = new StringBuilder(256); if (NativeMethods.GetClassName(foregroundHwnd, sb, sb.MaxCapacity) > 0) { newVisible = sb.ToString() == "FFXIVGAME"; } else { if (NativeMethods.GetWindowText(foregroundHwnd, sb, sb.MaxCapacity) == 0) { return; } newVisible = sb.ToString() == "FINAL FANTASY XIV"; } isGameWindow = true; } if (visibled != newVisible) { lock (overlay) overlay.ForEach(e => { try { if (e.Item2.Visible = (e.Item1.IsVisible && newVisible)) { if (isGameWindow && !IsOverlaysGameWindow(e.Item2.Handle, foregroundHwnd)) { EnsureTopMost(e.Item2.Handle); } } } catch { } }); visibled = newVisible; } }