WPF 无界面方案
第一步,设置窗体
1 | WindowStyle= "None" ResizeMode= "CanMinimize" Loaded= "Window_Loaded" WindowChrome.WindowChrome= "{DynamicResource WindowChromeKey}" MouseLeftButtonDown= "Window_MouseLeftButtonDown" WindowStartupLocation= "CenterScreen" |
1 2 3 4 5 6 7 | <Window.Resources> <WindowChrome x:Key= "WindowChromeKey" > <WindowChrome.ResizeBorderThickness> <Thickness>0</Thickness> </WindowChrome.ResizeBorderThickness> </WindowChrome> </Window.Resources> |
第二步,在窗体顶部按钮属性上写
1 | <Button Grid.ColumnSpan= "2" WindowChrome.IsHitTestVisibleInChrome= "True" /> |
完整示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <Window x:Class= "BeehijemwaboHaihafobe.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local= "clr-namespace:BeehijemwaboHaihafobe" mc:Ignorable= "d" Title= "MainWindow" WindowStyle= "None" Height= "450" Width= "800" > <WindowChrome.WindowChrome> <WindowChrome CaptionHeight= "20" /> </WindowChrome.WindowChrome><br> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Button Grid.ColumnSpan= "2" WindowChrome.IsHitTestVisibleInChrome= "True" /> <Button IsEnabled= "False" /> </Grid> </Window> |
在 WPF 里面,默认标题栏的交互相对复杂,如按钮没有设置 WindowChrome.IsHitTestVisibleInChrome 属性为 True 那按钮是拿不到点击事件的。
本文来告诉大家按钮 Button 的 IsEnabled 属性对 WindowChrome 的 IsHitTestVisibleInChrome 的影响
在 WPF 中的默认交互是点击标题栏的时候,如果是双击标题栏,那么将会进入最大化窗口。而如果在标题栏放一个按钮,此时按钮默认是没有收到点击的,详细请看 WPF 非客户区的触摸和鼠标点击响应,如果想要让按钮能收到点击,需要使用 WPF 非客户区的触摸和鼠标点击响应 的方法,让 WPF 的按钮加上 WindowChrome.IsHitTestVisibleInChrome 属性
此时点击按钮的时候,如果是在标题栏的地方,是让按钮收到点击。此时双击标题栏的按钮不会让窗口最大化,但如果此时的按钮设置 IsEnabled="False" 那么此时双击将依然让窗口最大化
而有趣的是如果在按钮 A 设置了 WindowChrome.IsHitTestVisibleInChrome 属性为 True 同时 IsEnabled="True" 然后在按钮 A 上方再放一个按钮 B 设置 IsEnabled="False" 那么此时双击将依然让窗口最大化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!