如何强制图像覆盖在我的WPF应用程序的标题栏上?

时间:2016-08-04 14:24:50

标签: c# wpf xaml

我有一个WPF应用程序,其动画.gif用于简单地引导用户注意。 .gif位于我的应用程序窗口的边界之外,这样它就在标题栏的下方,并被标题栏覆盖。

见下文:

有没有办法强制它叠加在上面?它在XAML中的定义如下:

<Grid>
    <Image Margin="-5 -45 0 0" DockPanel.Dock="Left" gif:ImageBehavior.AnimatedSource="/Resources/jump.gif" 
            Width="30" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="45"/>
                <TranslateTransform/>
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
</Grid>

它在设计时看起来很好:

我尝试使用DockPanel代替Grid作为其容器无济于事。

最后,是否有可能让它表现得像折叠一样?也就是说,不要占用ComboBox和图例Label

之间的水平空格

2 个答案:

答案 0 :(得分:2)

您需要使用Popup才能获得自己的窗口句柄。这也将使它不占用布局中的空间。作为额外的奖励(或者可能令人头痛),您可以使用PlacementTargetPlacementMode属性来定位它,因为看起来这就是您要尝试做的事情。

答案 1 :(得分:0)

我使用了Popup并且它工作得很好,但是有一些非常小的闪烁。

<Window x:Name="Window1" .../>
      <Grid>
        <Button Content="Show" HorizontalAlignment="Left" Margin="160,114,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <Button Content="Hide" HorizontalAlignment="Left" Margin="265,114,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
        <Popup x:Name="Popup1" UseLayoutRounding="True" IsOpen="False" Placement="Top" PlacementTarget="{Binding ElementName=Window1}">
            <Image Source="C:\\Users\\Public\\Pictures\\Sample Pictures\\desert.jpg" Stretch="Fill" Width="75" Height="25"/>
        </Popup>
      </Grid>
</Window>

代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    Popup1.IsOpen = true;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
    Popup1.IsOpen = false;
}

private void Window1_LocationChanged(object sender, EventArgs e)
{
    double offset = Popup1.HorizontalOffset;
    Popup1.HorizontalOffset = offset + 1;
    Popup1.HorizontalOffset = offset;
}   

另一种没有弹出窗口的方法(推荐)!

XAML

MainWindow.xaml

<Window x:Class="WpfWindow.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window1" Title="MainWindow" Height="350" Width="525" 
        Closing="Window1_Closing" Activated="Window1_Activated" LocationChanged="Window1_LocationChanged">    

        <Grid x:Name="root"/>

</Window>

TitleBarWindow.xaml

<Window x:Class="WpfWindow.TitleBarWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Topmost="True"
        Title="TitleBarWindow" AllowsTransparency="True" WindowStyle="None" Height="25" Width="200">
    <Grid>
        <Image Source="g:\\jellyfish.jpg" Stretch="Fill" HorizontalAlignment="Stretch"/>
    </Grid>
</Window>

MainWindow.xaml.cs

namespace WpfWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TitleBarWindow w = new TitleBarWindow();

        public MainWindow()
        {
            InitializeComponent();

            w.ShowActivated = true;
            w.Background = Brushes.Red;
        }

        private void Window1_LocationChanged(object sender, EventArgs e)
        {
            Point pt = Window1.PointToScreen(new Point(0, 0));
            w.Top = pt.Y - 27;
            w.Left = pt.X;
        }      

        private void Window1_Activated(object sender, EventArgs e)
        {
            Point pt = Window1.PointToScreen(new Point(0, 0));
            w.Top = pt.Y-27;
            w.Left = pt.X;

            w.Show();
        }

        private void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            w.Close();
        }
    }
}

第二种方法非常好,运作顺畅。