可移动的WPF无边框窗口

时间:2014-10-09 03:34:34

标签: wpf wpf-controls wpftoolkit powerbuilder

我有一个带无边框设计的WPF Mdi窗口,我希望它可以移动。 我尝试了我在Google上找到的所有内容,但没有什么能适合我的情况。这是否可以在WPF无边框窗口中进行?谢谢=)

这就是我使窗口无边框的方式。

 <pbwpf:Window x:Class="w_main" x:ClassModifier="internal" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:pbwpf="clr-namespace:Sybase.PowerBuilder.WPF.Controls;assembly=Sybase.PowerBuilder.WPF.Controls" Uid="30" WindowStyle="None" AllowsTransparency="True" Background="Transparent" MenuName="m_menu" WindowType="Mdi" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" VirtualizingStackPanel.VirtualizationMode="Recycling" xmlns:sys="clr-namespace:System;assembly=mscorlib" Center="True" ResizeMode="CanResize" Height="740" Width="1024" WindowState="Maximized" Resizable="True" ControlMenu="True">
    <Grid SnapsToDevicePixels="True" Height="700" Width="1009">
        <Grid.Effect>
            <DropShadowEffect Color="Black" BlurRadius="15" Direction="721" ShadowDepth="1" RenderingBias="Quality" />
        </Grid.Effect>
        <Border Background="White" />
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="90" />
                <ColumnDefinition Width="680" />
                <ColumnDefinition Width="69" />
                <ColumnDefinition Width="170" />
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" Grid.Row="0">
                <Image Source="Images/sample_bms.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
            </Border>
            <Border UseLayoutRounding="True" Grid.Row="0" Grid.Column="2" Background="#2cb6d9" BorderBrush="#25a6c7" BorderThickness="1,0,1,1">
                <Grid>
                    <Button Name="button_lgout" IsCancel="True">
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type Button}">
                                            <Border Background="{TemplateBinding Background}">
                                                <ContentPresenter TextElement.Foreground="{TemplateBinding Foreground}" />
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Background" Value="#2000" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Button.Style>
                        <StackPanel HorizontalAlignment="Center">
                            <Image Source="Images/logout.png" Height="21" HorizontalAlignment="Center" Margin="0,5,0,0" />
                            <Label Name="lbl_lgout" Content="LOGOUT" FontSize="12" Foreground="White" FontFamily="Calibri" HorizontalAlignment="Center" Height="27" />
                        </StackPanel>
                    </Button>
                </Grid>
            </Border>
            <Border Grid.Row="0" Grid.Column="3" Background="#2cb6d9" BorderBrush="#25a6c7" BorderThickness="0,0,0,1">
                <Grid VerticalAlignment="Center">
                    <Image Source="Images/user_male2-32.png" UseLayoutRounding="True" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,18,0" Height="25" />
                    <pbwpf:SingleLineEdit Name="txt_user" FontFamily="Calibri" FontSize="22" Foreground="White" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,52,0" Width="98" Height="30" PBHeight="120" DisplayOnly="True" />
                </Grid>
            </Border>
            <Border Grid.Row="0" Grid.Column="1" Background="#2cb6d9" BorderBrush="#25a6c7" BorderThickness="0,0,0,1" />
            <Border Grid.Row="1" Grid.Column="0" Background="#dedede" BorderBrush="#d9dcdf" BorderThickness="0,0,1,0">
                <StackPanel Orientation="Vertical" Height="750" Background="#111111">
                    <StackPanel.Resources>
                        <Style TargetType="my:RibbonButton">
                            <Style.Resources>
                                <sys:Double x:Key="buttonSize">60</sys:Double>
                                <CornerRadius x:Key="buttonRadius">30</CornerRadius>
                                <sys:Double x:Key="scaleOffset">30</sys:Double>
                            </Style.Resources>
                            <Setter Property="Margin" Value="0,10,0,0" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="FontWeight" Value="SemiBold" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="my:RibbonButton">
                                        <Border Background="Transparent" Width="{StaticResource buttonSize}" Height="{StaticResource buttonSize}" CornerRadius="{StaticResource buttonRadius}">
                                            <Grid>
                                                <Border Background="#22ffffff" CornerRadius="{StaticResource buttonRadius}" x:Name="content">
                                                    <Image Height="27" x:Name="image" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" Source="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"></Image>
                                                </Border>
                                                <Ellipse x:Name="ring" StrokeThickness="15" Opacity="0" IsHitTestVisible="False">
                                                    <Ellipse.Stroke>
                                                        <RadialGradientBrush>
                                                            <GradientStop Color="Transparent" Offset="0.83" />
                                                            <GradientStop Color="LightGray" Offset="0.84" />
                                                            <GradientStop Color="Transparent" Offset="0.85" />
                                                            <GradientStop Color="Transparent" Offset=".93" />
                                                            <GradientStop Color="#55ffffff" Offset=".97" />
                                                            <GradientStop Color="#55ffffff" Offset="1" />
                                                        </RadialGradientBrush>
                                                    </Ellipse.Stroke>
                                                    <Ellipse.RenderTransform>
                                                        <ScaleTransform CenterX="{StaticResource scaleOffset}" CenterY="{StaticResource scaleOffset}" x:Name="ringScale" />
                                                    </Ellipse.RenderTransform>
                                                </Ellipse>
                                            </Grid>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="true">
                                                <Setter Property="Opacity" Value="0.2" />
                                                <Setter TargetName="content" Property="RenderTransform">
                                                    <Setter.Value>
                                                        <ScaleTransform CenterX="{StaticResource scaleOffset}" CenterY="{StaticResource scaleOffset}" ScaleX=".9" ScaleY=".9" />
                                                    </Setter.Value>
                                                </Setter>
                                                <Setter Property="Opacity" Value="1" />
                                                <Trigger.EnterActions>
                                                    <BeginStoryboard>
                                                        <Storyboard Duration="0:0:2">
                                                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ring" To="1" Duration="0:0:0" />
                                                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ring" From="1" To="0" />
                                                            <DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="ringScale" From="1" To="1.5" />
                                                            <DoubleAnimation Storyboard.TargetProperty="ScaleY" Storyboard.TargetName="ringScale" From="1" To="1.5" />
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </Trigger.EnterActions>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </StackPanel.Resources>
                    <my:RibbonButton Name="rb_new" Margin="0,50,0,0" Content="Images/new_light.png" KeyTip="N" />
                    <pbwpf:StaticText Name="st_new" Text="_New" Foreground="#eeeeee" Margin="33,-20,0,0" FontFamily="Calibri" />
                    <my:RibbonButton Name="rb_edit" Margin="0,10,0,0" Content="Images/edit_light.png" KeyTip="E" />
                    <pbwpf:StaticText Name="st_edit" Text="_Edit" Foreground="#eeeeee" Margin="33,-20,0,0" FontFamily="Calibri" PBWidth="402" Width="88" PBHeight="84" Height="21" />
                    <my:RibbonButton Name="rb_save" Margin="0,10,0,0" Content="Images/save_light.png" KeyTip="S" />
                    <pbwpf:StaticText Name="st_save" Text="_Save" Foreground="#eeeeee" Margin="33,-20,0,0" FontFamily="Calibri" PBWidth="402" Width="88" PBHeight="84" Height="21" />
                    <my:RibbonButton Name="rb_abort" Margin="0,10,0,0" Content="Images/cancel_light.png" KeyTip="A" />
                    <pbwpf:StaticText Name="st_abort" Text="_Abort" Foreground="#eeeeee" Margin="33,-21,0,0" FontFamily="Calibri" PBWidth="402" Width="88" PBHeight="84" Height="21" />
                    <my:RibbonButton Name="rb_trash" Margin="0,10,0,0" Content="Images/delete_light.png" KeyTip="D" />
                    <pbwpf:StaticText Name="st_trash" Text="_Trash" Foreground="#eeeeee" Margin="33,-21,0,0" FontFamily="Calibri" PBHeight="84" PBWidth="402" Width="88" />
                    <my:RibbonButton Name="rb_print" Margin="0,10,0,0" Content="Images/print_light.png" KeyTip="P" />
                    <pbwpf:StaticText Name="st_print" Text="_Print" Foreground="#eeeeee" Margin="33,-21,0,0" FontFamily="Calibri" PBHeight="84" PBWidth="0" Width="Auto" />
                    <my:RibbonButton Name="rb_search" Margin="0,10,0,0" Content="Images/search_light.png" Visibility="Hidden" />
                </StackPanel>
            </Border>
            <Border Grid.Row="1" Grid.Column="1">
                <pbwpf:MDIClient Visibility="Visible" Name="mdi_1" Margin="0" Background="#ffffff" Width="920" />
            </Border>
        </Grid>
    </Grid>
</pbwpf:Window>

3 个答案:

答案 0 :(得分:6)

我就是这样做的。

吉姆

在XAML中

WindowStyle="None" AllowsTransparency="False" MouseDown="Window_MouseDown" ResizeMode="NoResize"

背后的代码

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DragMove();
        }
    }

答案 1 :(得分:1)

我有一个工作的WPF无边框窗口的完整源代码,我刚刚在另一篇文章中写道。请在这个帖子上查看我的答案。

WPF Borderless Window issues: Aero Snap & Maximizing

答案 2 :(得分:0)

就我而言,我使用的是PowerBuilder 12.5。因此,如果您想让WPF无边框窗口可移动,请快速提示。

只需将此代码放入Mousedown事件: 发送(句柄(this),274,61458,0)

如果控件没有mousedown事件,则将事件定义为: ue_mousedown pbm_lbuttondown

此答案来自http://eric.aling.tripod.com/PB/pbfaq.htm

快乐编码=)))