研究了一天,终于成功在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之类的控件,模拟用户区。
最后,因为是无边框窗体,所以必须自己写窗体拖动。
这个网上也有很多,自己搜吧。