【问题标题】:Movable WPF Borderless Window可移动的 WPF 无边框窗口
【发布时间】:2014-10-09 03:34:34
【问题描述】:

我有一个无边框设计的 WPF Mdi 窗口,我希望它是可移动的。 我尝试了我在谷歌上找到的所有东西,但没有任何事情适合我的情况。在 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>

【问题讨论】:

    标签: wpf wpf-controls wpftoolkit powerbuilder


    【解决方案1】:

    我就是这样做的。

    吉姆

    在 XAML 中

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

    背后的代码

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

    【讨论】:

    • 嗨@Jim 感谢您的回答=) 但目前我发现了如何让它工作。当我使用 power builder 时,我将此代码 Send(handle(this), 274, 61458, 0) 放在鼠标按下事件上,然后就是这样 =)。无论如何感谢您的时间 =)))
    • 我的荣幸。很高兴你能回答你的情况:)
    【解决方案2】:

    我最近在另一篇文章中写了一个工作 WPF 无边界窗口的完整源代码。请查看我在此线程上的回答。

    WPF Borderless Window issues: Aero Snap & Maximizing

    【讨论】:

      【解决方案3】:

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

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

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

      这个答案是来自http://eric.aling.tripod.com/PB/pbfaq.htm的令牌。

      快乐编码 =)))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多