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 @   唐宋元明清2188  阅读(300)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示
剑桥
16:15发布
剑桥
16:15发布
7°
西南风
3级
空气质量
相对湿度
66%
今天
2°/10°
周三
2°/8°
周四
中雨
2°/7°