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);
作者:唐宋元明清2188
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。