C# 禁用窗口激活

2023-07-10,,

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

 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);

C# 禁用窗口激活的相关教程结束。

《C# 禁用窗口激活.doc》

下载本文的Word格式文档,以方便收藏与打印。