C# WPF Win7 标题栏添加按钮(模拟)

研究了一天,终于成功在Win7标题栏上加了按钮。
(使用AreoGlass模拟的标题栏)
效果图

废话不多说,代码:

[StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        };

        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(
            IntPtr hwnd,
            ref MARGINS pMarInset);

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Background = Brushes.Transparent;
            try
            {
                IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

                MARGINS margins = new MARGINS();
                margins.cxLeftWidth = -1;
                margins.cxRightWidth = -1;
                margins.cyTopHeight = -1;
                margins.cyBottomHeight = -1;

                int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
                if (hr < 0)
                {
                      //未启用AreoGlass
                      //在此可以参见[C#标题栏添加按钮],网上例子很多。
                }
            }
            catch (DllNotFoundException)
            {
                Application.Current.MainWindow.Background = Brushes.White;
                //未启用AreoGlass,同上。
            }
        }

XMAL:

<Window x:Class="UsersApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainForm" Height="400" Width="500" Loaded="Window_Loaded" Icon="/UsersApp;component/Images/Error.ico" WindowStartupLocation="CenterScreen" WindowStyle="None" MaxWidth="500" MaxHeight="400" MinHeight="400" MinWidth="500">
    <Grid Height="378">
        <Label Content="主窗体" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Margin="-3,-6,0,0" />
        <Button Content="" Height="19" HorizontalAlignment="Left" Margin="50,0,0,0" Name="button1" VerticalAlignment="Top" Width="39" />
    </Grid>
</Window>

然后在MainForm上加个Frame之类的控件,模拟用户区。
最后,因为是无边框窗体,所以必须自己写窗体拖动。
这个网上也有很多,自己搜吧。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值