最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)的編寫(xiě)如下使用示例,以說(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="畫(huà)筆"  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#實(shí)現(xiàn)記事本查找與替換功能

    C#實(shí)現(xiàn)記事本查找與替換功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)記事本查找與替換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Unity實(shí)現(xiàn)仿3D輪轉(zhuǎn)圖效果

    Unity實(shí)現(xiàn)仿3D輪轉(zhuǎn)圖效果

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)仿3D輪轉(zhuǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 基于C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的漏洞掃描器

    基于C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的漏洞掃描器

    本文介紹了使用C#開(kāi)發(fā)一個(gè)簡(jiǎn)單的漏洞掃描器,具備端口掃描和基礎(chǔ)Web漏洞檢測(cè)功能,如SQL注入和XSS檢測(cè),文章提供了項(xiàng)目創(chuàng)建方法、完整代碼、使用說(shuō)明以及功能說(shuō)明,還提到了局限性和改進(jìn)方向,最后建議了功能拓展方向,需要的朋友可以參考下
    2026-04-04
  • c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式

    c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式

    這篇文章主要給大家介紹了關(guān)于c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • C#結(jié)束進(jìn)程及子進(jìn)程

    C#結(jié)束進(jìn)程及子進(jìn)程

    這篇文章介紹了C#操作結(jié)束進(jìn)程及子進(jìn)程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#中跨線程訪問(wèn)控件的實(shí)現(xiàn)方法

    C#中跨線程訪問(wèn)控件的實(shí)現(xiàn)方法

    C#中不允許跨線程直接訪問(wèn)界面控件,即一個(gè)線程中如主線程創(chuàng)建的控件不允許被其他線程例如子線程直接訪問(wèn),在一個(gè)線程中設(shè)置其他線程所有的控件屬性通常有兩種方法,本文將詳細(xì)的給大家介紹一下,需要的朋友可以參考下
    2024-12-12
  • C#?wpf實(shí)現(xiàn)任意控件更多調(diào)整大小功能

    C#?wpf實(shí)現(xiàn)任意控件更多調(diào)整大小功能

    這篇文章主要為大家詳細(xì)介紹了C#?wpf實(shí)現(xiàn)任意控件更多調(diào)整大小功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • c# 實(shí)現(xiàn)KMP算法的示例代碼

    c# 實(shí)現(xiàn)KMP算法的示例代碼

    這篇文章主要介紹了c# 實(shí)現(xiàn)KMP算法的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • 純C#實(shí)現(xiàn)智能PDF目錄提取工具PdfTocExtractor

    純C#實(shí)現(xiàn)智能PDF目錄提取工具PdfTocExtractor

    這篇文章主要為大家詳細(xì)介紹了如何通過(guò)C#實(shí)現(xiàn)智能PDF目錄提取工具PdfTocExtractor,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-08-08
  • C#中的協(xié)變與逆變小結(jié)

    C#中的協(xié)變與逆變小結(jié)

    這篇文章主要介紹了C#中的協(xié)變與逆變的相關(guān)知識(shí),在泛型或委托中,如果不使用協(xié)變或逆變,那么泛型類型是一個(gè)固定類型,而使用協(xié)變或逆變的話,則泛型類型可以實(shí)現(xiàn)多態(tài)化,需要的朋友可以參考下
    2021-10-10

最新評(píng)論

婺源县| 抚松县| 泰来县| 万山特区| 安阳市| 通化县| 镇宁| 会理县| 栾川县| 九江市| 西乡县| 广汉市| 永兴县| 阜宁县| 驻马店市| 新晃| 冀州市| 东乌珠穆沁旗| 连山| 绥宁县| 镇宁| 惠安县| 调兵山市| 崇义县| 同心县| 志丹县| 社会| 霍邱县| 渝中区| 宜州市| 孝昌县| 淳安县| 鄢陵县| 四川省| 紫阳县| 永城市| 葫芦岛市| 惠州市| 乌审旗| 刚察县| 炎陵县|