Fork me on GitHub
Toggling Focus Assist mode in Win 10 Programmatically

Toggling Focus Assist mode in Win 10 Programmatically

 1 using System.Diagnostics;
 2 using System.Runtime.InteropServices;
 3 
 4 namespace ConsoleApp1
 5 {
 6     internal class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Console.WriteLine("Hello, World!");
11             var state = WnfHelper.GetFocusAssistValue();
12             Console.WriteLine($"{state.ToString()}");
13             Console.ReadLine();
14         }
15     }
16 
17     public static class WnfHelper
18     {
19         public const ulong WNF_SHEL_QUIETHOURS_ACTIVE_PROFILE_CHANGED = 0x0d83063ea3bf1c75UL;
20 
21         [StructLayout(LayoutKind.Sequential)]
22         private struct WNF_STATE_NAME
23         {
24             public uint Data1;
25 
26             public uint Data2;
27         }
28 
29         [DllImport("ntdll.dll", SetLastError = true)]
30         private static extern int NtQueryWnfStateData(
31                                         ref ulong stateName,
32                                         IntPtr typeId,
33                                         IntPtr explicitScope,
34                                         out uint changeStamp,
35                                         byte[] buffer,
36                                         ref uint bufferSize);
37 
38         public static FocusAssistState GetFocusAssistValue()
39         {
40             return
41                 (FocusAssistState)
42                     GetWnfStateValue(
43                             WNF_SHEL_QUIETHOURS_ACTIVE_PROFILE_CHANGED);
44         }
45 
46         public static int GetWnfStateValue(ulong stateNameIdent)
47         {
48             var buffer = new byte[4];  // DWORD
49             var bufferSize = (uint)buffer.Length;
50 
51             var status =
52                 NtQueryWnfStateData(
53                         ref stateNameIdent,
54                         IntPtr.Zero,
55                         IntPtr.Zero,
56                         out var changeStamp,
57                         buffer,
58                         ref bufferSize);
59 
60             if (status == 0)
61             {
62                 var mode = BitConverter.ToInt32(buffer, 0);
63 
64                 Debug.WriteLine($"Mode:  '{mode}'");
65 
66                 return mode;
67             }
68             else
69             {
70                 Debug.WriteLine($"Failed to read the state value. 'NTSTATUS': '0x{status:X8}'");
71             }
72 
73             return -1;
74         }
75     }
76 
77     public enum FocusAssistState : uint
78     {
79         Off = 0,
80         Game = 1,
81         Fullscreen = 2,
82     }
83 }

 

 

https://martinsuchan.github.io/ApiPeek/Diffs/win11.22526.to.win11.22572.fulldiff.html

https://helpdeskgeek.com/what-is-focus-assist-in-windows-11-and-how-to-use-it/

https://stackoverflow.com/questions/55477041/toggling-focus-assist-mode-in-win-10-programmatically

posted on 2025-07-22 23:18  HackerVirus  阅读(37)  评论(0)    收藏  举报