none
WPF程序,拖动无窗口边框的窗口? RRS feed

  • 问题

  • <Window x:Class="WhiteBoard.DesktopDlg"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="DesktopDlg"  WindowStyle="None" Height="100" WindowStartupLocation="Manual" Left="0"  ShowInTaskbar="False" ResizeMode="CanResizeWithGrip" ShowActivated="False" MinHeight="100" MaxHeight="100" MinWidth="100" MaxWidth="280" Loaded="DesktopDlgLoaded" Width="280"  Topmost="True"  MouseMove="MouseMove">
        <Grid>
            <Grid.RowDefinitions>          
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0"  Background="CornflowerBlue"  HorizontalAlignment="Left"  Orientation="Horizontal"   Margin="0,0,0,0" >          
                <Button x:Name="desktopBtn"  Visibility="Visible" Width="80" Height="80"  Margin="10,0,0,0" Click="btnDeskTop_Click">
                    <Button.Content>
                        <Image x:Name="desktopBtnContent" Source="Resources/goback.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
                <Button x:Name="graffitiBtn"  Visibility="Visible"  Width="80" Height="80"  Margin="10,0,0,0" Click="btnGraffiti_Click"  IsDefault="True" TabIndex="0">
                    <Button.Content>
                        <Image x:Name="graffitiBtnContent" Source="Resources/annotate.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
                <Button x:Name="SwitchBtn"  Width="80" Height="80"  Margin="10,0,0,0" Click="btnSwitch_Click">
                    <Button.Content>
                        <Image  x:Name="SwitchBtnContent" Source="Resources/stretch.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
            </StackPanel>
        </Grid>
    </Window>

    根据网上的信息,拖动无边框的窗口为:

    private void MouseMove(object sender, MouseEventArgs e) 
            { 
                if (e.LeftButton == MouseButtonState.Pressed) 
                { 
                    this.DragMove(); 
                }
            }

    可是这样写:怎么会出现错误呢??

    错误为:

    怎么回事呢??

    2014年10月30日 7:04

答案

  • 你好,

    >>可是怎么在触摸屏下面就不能拖动了呢,触摸屏下面怎么拖动无边框的窗口呢???

    我重建了你的Demo,在Surface上,可以正常地拖动无边框窗口,对于你遇到的问题,可能的原因是触摸位置位于Button上方,这样就无法响应Window的 PreviewMouseMove 事件,自然是无法拖动了。

    请注意触摸到非控件区域,这样既可正常拖动。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 heller灬 2014年11月6日 1:13
    2014年10月31日 9:00
    版主

全部回复

  • <Window x:Class="WhiteBoard.DesktopDlg"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="DesktopDlg"  WindowStyle="None" Height="100" WindowStartupLocation="Manual" Left="0"  ShowInTaskbar="False" ResizeMode="CanResizeWithGrip" ShowActivated="False" MinHeight="100" MaxHeight="100" MinWidth="100" MaxWidth="280" Loaded="DesktopDlgLoaded" Width="280"  Topmost="True"  MouseMove="MouseMove">
        <Grid>
            <Grid.RowDefinitions>          
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0"  Background="CornflowerBlue"  HorizontalAlignment="Left"  Orientation="Horizontal"   Margin="0,0,0,0" >          
                <Button x:Name="desktopBtn"  Visibility="Visible" Width="80" Height="80"  Margin="10,0,0,0" Click="btnDeskTop_Click">
                    <Button.Content>
                        <Image x:Name="desktopBtnContent" Source="Resources/goback.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
                <Button x:Name="graffitiBtn"  Visibility="Visible"  Width="80" Height="80"  Margin="10,0,0,0" Click="btnGraffiti_Click"  IsDefault="True" TabIndex="0">
                    <Button.Content>
                        <Image x:Name="graffitiBtnContent" Source="Resources/annotate.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
                <Button x:Name="SwitchBtn"  Width="80" Height="80"  Margin="10,0,0,0" Click="btnSwitch_Click">
                    <Button.Content>
                        <Image  x:Name="SwitchBtnContent" Source="Resources/stretch.png" Width="80" Height="80" Stretch="Uniform"></Image>
                    </Button.Content>
                </Button>
            </StackPanel>
        </Grid>
    </Window>

    根据网上的信息,拖动无边框的窗口为:

    private void MouseMove(object sender, MouseEventArgs e) 
            { 
                if (e.LeftButton == MouseButtonState.Pressed) 
                { 
                    this.DragMove(); 
                }
            }

    可是这样写:怎么会出现错误呢??

    错误为:

    怎么回事呢??

    错误解决:把MouseMove改成PreviewMouseMove,就可以解决在鼠标下面拖动窗口

    可是怎么在触摸屏下面就不能拖动了呢,触摸屏下面怎么拖动无边框的窗口呢???

    2014年10月30日 7:53
  • 你好,

    >>可是怎么在触摸屏下面就不能拖动了呢,触摸屏下面怎么拖动无边框的窗口呢???

    我重建了你的Demo,在Surface上,可以正常地拖动无边框窗口,对于你遇到的问题,可能的原因是触摸位置位于Button上方,这样就无法响应Window的 PreviewMouseMove 事件,自然是无法拖动了。

    请注意触摸到非控件区域,这样既可正常拖动。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 heller灬 2014年11月6日 1:13
    2014年10月31日 9:00
    版主