C# 禁用窗口激活

如果界面点击时,不想让窗口激活,可以按如下操作:

 1     public MainWindow()
 2     {
 3         InitializeComponent();
 4         SourceInitialized += OnSourceInitialized;
 5     }
 6     private void OnSourceInitialized(object sender, EventArgs e)
 7     {
 8         var handle = (PresentationSource.FromVisual(this) as HwndSource).Handle;
 9         var exstyle = User32.GetWindowLong(handle, GWL_EXSTYLE);
10         User32.SetWindowLong(handle, GWL_EXSTYLE, new IntPtr(exstyle.ToInt32() | WS_EX_NOACTIVATE));
11     }
12     public const int WS_EX_NOACTIVATE = 0x08000000;
13     public const int GWL_EXSTYLE = -20;

User32函数:

1     [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
2     public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
3 
4     [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
5     public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

 

posted @ 2022-03-29 00:51  唐宋元明清2188  阅读(283)  评论(0编辑  收藏  举报