亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# wpf 實(shí)現(xiàn)窗口任意區(qū)域點(diǎn)擊拖動(dòng)

 更新時(shí)間:2024年03月27日 11:15:59   作者:CodeOfCC  
在wpf要實(shí)現(xiàn)此功能簡(jiǎn)單形式還是比較容易的,但是有一些細(xì)節(jié)需要專門處理,比如與按鈕的點(diǎn)擊事件沖突問(wèn)題,解決事件沖突問(wèn)題后拖動(dòng)的靈敏度,可復(fù)用性等,這篇文章主要介紹了C# wpf 實(shí)現(xiàn)窗口任意區(qū)域點(diǎn)擊拖動(dòng),需要的朋友可以參考下

前言

點(diǎn)擊窗口任意區(qū)域可移動(dòng)功能,在一些業(yè)務(wù)場(chǎng)景中會(huì)使用到,比如工具條或者球形狀的窗口等。在wpf要實(shí)現(xiàn)此功能簡(jiǎn)單形式還是比較容易的,但是有一些細(xì)節(jié)需要專門處理,比如與按鈕的點(diǎn)擊事件沖突問(wèn)題,解決事件沖突問(wèn)題后拖動(dòng)的靈敏度,可復(fù)用性等。

一、簡(jiǎn)單拖動(dòng)

基礎(chǔ)的拖動(dòng)功能直接使用Window類的DragMove即可實(shí)現(xiàn):
在Window的PreviewMouseLeftButtonDown中調(diào)用DragMove即可。
示例如下:

<Window PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown" />
void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    DragMove();
}

注:本文實(shí)現(xiàn)的是窗口任意區(qū)域拖動(dòng)(比如按鈕占滿整個(gè)窗口),不適用于一般窗口拖動(dòng),一般窗口拖動(dòng)請(qǐng)使用MouseLeftButtonDown事件

二、事件沖突問(wèn)題

根據(jù)上述方法實(shí)現(xiàn)窗口拖動(dòng)后發(fā)現(xiàn)出現(xiàn)了事件沖突,即窗口內(nèi)的控件無(wú)法相應(yīng)鼠標(biāo)點(diǎn)擊事件了。因?yàn)镈ragMove的內(nèi)部實(shí)現(xiàn)使用了SC_MOVE,使標(biāo)題欄(win32窗口)捕獲鼠標(biāo),原本窗口失去鼠標(biāo)捕獲。窗口內(nèi)的控件無(wú)法響應(yīng)鼠標(biāo)消息,因此上述簡(jiǎn)單拖動(dòng)在有控件的窗口中是不可行的(局部拖動(dòng)是可行的,但本文講的是任意區(qū)域拖動(dòng))。

三、解決方法

解決方法是不在鼠標(biāo)按下事件中觸發(fā)拖動(dòng),而是在鼠標(biāo)移動(dòng)后觸發(fā)拖動(dòng)操作。具體步驟如下:
1、注冊(cè)3個(gè)事件如下:

<Window PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
        PreviewMouseMove="Window_PreviewMouseMove"
        PreviewMouseLeftButtonUp="Window_PreviewMouseLeftButtonUp"/>

2、定義2個(gè)變量記錄信息。

Point _pressedPosition ;
bool _isDragMoved = false;

3.記錄鼠標(biāo)按下位置

void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    _pressedPosition = e.GetPosition(this);
}

4.鼠標(biāo)移動(dòng)觸發(fā)拖動(dòng)

 void Window_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
 {
     if (Mouse.LeftButton==MouseButtonState.Pressed && _pressedPosition != e.GetPosition(this))
     {
         _isDragMoved = true;
         DragMove();
     }
 }

5.鼠標(biāo)彈起屏蔽消息

 void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
     if (_isDragMoved)
     {
         _isDragMoved = false;
         e.Handled = true;
     }
 }

四、效果預(yù)覽

五、使用示例

由于評(píng)論區(qū)反饋上述方法存在bug,但是無(wú)奈筆者始終沒(méi)有重現(xiàn),懷疑是使用方法不正確,或者對(duì)本博文講述功能理解上的差異導(dǎo)致的,在此根據(jù)上述方法初版原封不動(dòng)的編寫如下使用示例,以說(shuō)明使用場(chǎng)景以及使用方法

1、白板的工具條

(1)、MainWindow.xaml

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Background="Transparent"
        AllowsTransparency="True"
        WindowStyle="None"
        Title="MainWindow" Height="60" Width="410" 
        PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
        PreviewMouseMove="Window_PreviewMouseMove"
        PreviewMouseLeftButtonUp="Window_PreviewMouseLeftButtonUp">
    <Border Background="White"  CornerRadius="25" Margin="5">
        <Border.Effect>
            <DropShadowEffect ShadowDepth="0" BlurRadius="5" Opacity="0.8" Color="#AAAAAA"/>
        </Border.Effect>
        <StackPanel Margin="20,0,0,0" Orientation="Horizontal">
            <RadioButton     Content="畫筆"  Width="50" Height="50" Cursor="Hand">
                <RadioButton.Template>
                    <ControlTemplate TargetType="RadioButton"  >
                        <Border Background="Transparent">
                            <TextBlock x:Name="txt"  Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter TargetName="txt" Property="Foreground" Value="red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton     Content="矩形"  Width="50" Height="50" Cursor="Hand">
                <RadioButton.Template>
                    <ControlTemplate TargetType="RadioButton"  >
                        <Border Background="Transparent">
                            <TextBlock x:Name="txt"  Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter TargetName="txt" Property="Foreground" Value="red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton     Content="文本"  Width="50" Height="50" Cursor="Hand">
                <RadioButton.Template>
                    <ControlTemplate TargetType="RadioButton"  >
                        <Border Background="Transparent">
                            <TextBlock x:Name="txt"  Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter TargetName="txt" Property="Foreground" Value="red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton     Content="箭頭"  Width="50" Height="50" Cursor="Hand">
                <RadioButton.Template>
                    <ControlTemplate TargetType="RadioButton"  >
                        <Border Background="Transparent">
                            <TextBlock x:Name="txt"  Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter TargetName="txt" Property="Foreground" Value="red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton     Content="圖片"  Width="50" Height="50" Cursor="Hand">
                <RadioButton.Template>
                    <ControlTemplate TargetType="RadioButton"  >
                        <Border Background="Transparent">
                            <TextBlock x:Name="txt"  Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter TargetName="txt" Property="Foreground" Value="red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <Border Margin="10,0,0,0" BorderThickness="1,0,0,0" BorderBrush="#999999"></Border>
            <Button  Name="btn_upload" Margin="0,0,0,0"   Content="上傳"  Width="50" Height="50" Cursor="Hand" Click="btn_upload_Click">
                <Button.Template>
                    <ControlTemplate TargetType="Button"  >
                        <Border Background="Transparent">
                            <TextBlock Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Name="btn_close" Margin="0,0,0,0"   Content="關(guān)閉"  Width="50" Height="50"  Cursor="Hand"  Click="btn_close_Click">
                <Button.Template>
                    <ControlTemplate TargetType="Button"  >
                        <Border Background="Transparent">
                            <TextBlock Background="Transparent"  TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#666666" FontSize="18"
                         Text="{TemplateBinding Content}"></TextBlock>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </StackPanel>
    </Border>
</Window>

(2)、MainWindow.xaml.cs

using System.Windows;
using System.Windows.Input;
namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Point _pressedPosition;
        bool _isDragMoved = false;
        void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _pressedPosition = e.GetPosition(this);
        }
        void Window_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPosition != e.GetPosition(this))
            {
                _isDragMoved = true;
                DragMove();
            }
        }
        void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isDragMoved)
            {
                _isDragMoved = false;
                e.Handled = true;
            }
        }
        private void btn_upload_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("上傳成功");
        }
        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }   
    }
}

(3)、效果預(yù)覽

總結(jié)

本文介紹了在窗口任意區(qū)域點(diǎn)擊拖動(dòng)的方法。簡(jiǎn)單的拖動(dòng)是不可行的,往深一步探究,發(fā)現(xiàn)了巧妙的解決方法:在鼠標(biāo)移動(dòng)時(shí)觸發(fā)拖動(dòng)。但這也不是一蹴而就的,這是筆者曾經(jīng)做項(xiàng)目經(jīng)過(guò)了一定的探究最終得以實(shí)現(xiàn),而本文呈現(xiàn)的是優(yōu)化后的精簡(jiǎn)版本,易于實(shí)現(xiàn)也很好理解,即是所謂的大道至簡(jiǎn)。

到此這篇關(guān)于C# wpf 實(shí)現(xiàn)窗口任意區(qū)域點(diǎn)擊拖動(dòng)的文章就介紹到這了,更多相關(guān)C# wpf窗口拖動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# 列表List的常用屬性和方法介紹

    C# 列表List的常用屬性和方法介紹

    這篇文章主要介紹了C# 列表List的常用屬性和方法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 詳解c#索引(Index)和范圍(Range)

    詳解c#索引(Index)和范圍(Range)

    這篇文章主要介紹了c#索引(Index)和范圍(Range)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-10-10
  • C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié)

    C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié)

    這篇文章主要介紹了C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié),需要的朋友可以參考下
    2017-06-06
  • C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出

    C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出

    這篇文章主要介紹了C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器

    C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器

    這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差

    C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差

    本文通過(guò)一段簡(jiǎn)單的代碼給大家介紹了C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差,代碼簡(jiǎn)單易懂,需要的朋友參考下吧
    2017-08-08
  • 再談異常處理try catch finally

    再談異常處理try catch finally

    這篇文章主要介紹了再談異常處理try catch finally 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 談一談autofac組件的實(shí)例范圍

    談一談autofac組件的實(shí)例范圍

    這篇文章主要和大家聊一聊autofac組件的實(shí)例范圍,探討autofac組件的實(shí)例范圍,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 一篇文章看懂C#中的協(xié)變、逆變

    一篇文章看懂C#中的協(xié)變、逆變

    這篇文章主要給大家介紹了如何通過(guò)一篇文章看懂C#中協(xié)變、逆變的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • C#利用Task實(shí)現(xiàn)任務(wù)超時(shí)多任務(wù)一起執(zhí)行的方法

    C#利用Task實(shí)現(xiàn)任務(wù)超時(shí)多任務(wù)一起執(zhí)行的方法

    這篇文章主要給大家介紹了關(guān)于C#利用Task實(shí)現(xiàn)任務(wù)超時(shí),多任務(wù)一起執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來(lái)一起看看吧。
    2017-12-12

最新評(píng)論