弹来弹去跑马灯!

WPF鼠标拖动改变图片透明度

<Window x:Class="MovePicTest.PicWindow"
        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:MovePicTest"
        mc:Ignorable="d"
        Title="PicWindow" Height="450" Width="800">
    <Grid>
        <Image 
       Source="/Res/1.png" 
       Stretch="Uniform"/>
        <Image x:Name="TargetImage" 
               Source="/Res/0.png" 
               Stretch="Uniform"
               RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <ScaleTransform ScaleX="1" ScaleY="1"/>
            </Image.RenderTransform>
        </Image>

        <!-- 提示文本 -->
        <TextBlock HorizontalAlignment="Center" 
                   VerticalAlignment="Bottom"
                   Margin="10"
                   Foreground="White"
                   Background="#80000000"
                   Padding="5"
                   Text="按住鼠标左键上下拖动调整透明度"/>
    </Grid>
</Window>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MovePicTest
{
    /// <summary>
    /// PicWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PicWindow : Window
    {
        private bool isDragging = false;
        private Point startPoint;
        private double initialOpacity = 1.0;

        public PicWindow()
        {
            InitializeComponent();

            // 订阅鼠标事件
            TargetImage.MouseLeftButtonDown += Image_MouseLeftButtonDown;
            TargetImage.MouseMove += Image_MouseMove;
            TargetImage.MouseLeftButtonUp += Image_MouseLeftButtonUp;
            TargetImage.MouseLeave += Image_MouseLeave;
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;
            startPoint = e.GetPosition(this);
            initialOpacity = TargetImage.Opacity;

            // 捕获鼠标,确保在窗口外也能接收到事件
            TargetImage.CaptureMouse();
        }

        private void Image_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isDragging || e.LeftButton != MouseButtonState.Pressed)
                return;

            Point currentPoint = e.GetPosition(this);
            double deltaY = currentPoint.Y - startPoint.Y;

            // 计算透明度变化
            // 每移动100像素,透明度变化0.5(可根据需要调整灵敏度)
            double opacityChange = deltaY / 200.0;
            double newOpacity = initialOpacity - opacityChange;

            // 限制透明度在0.1到1.0之间
            newOpacity = Math.Max(0.0001, Math.Min(1.0, newOpacity));

            // 应用新的透明度
            TargetImage.Opacity = newOpacity;

            // 可选:显示当前透明度
            UpdateOpacityText(newOpacity);
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            StopDragging();
        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                StopDragging();
            }
        }

        private void StopDragging()
        {
            if (isDragging)
            {
                isDragging = false;
                TargetImage.ReleaseMouseCapture();
            }
        }

        private void UpdateOpacityText(double opacity)
        {
            // 可选:在界面上显示当前透明度
            this.Title = $"图片透明度控制 - 当前透明度: {opacity:F2}";
        }
    }
      
 }

  

posted @ 2026-02-02 09:35  wgscd  阅读(0)  评论(0)    收藏  举报