惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
D
Docker
IT之家
IT之家
博客园_首页
罗磊的独立博客
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Y
Y Combinator Blog
博客园 - 聂微东
量子位
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
Martin Fowler
Martin Fowler
Jina AI
Jina AI
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
月光博客
月光博客
G
Google Developers Blog
B
Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿

博客园 - wgscd

Git 无法访问 GitHub(unable to access ‘https://github.com/.../.git‘)问题解决教程 RapidOcrNet文字识别OCR C#+Audio绘制麦克风波形 一共免费大模型API AI聊天对话界面的HTML代码。 纯JavaScript代码实现保存Canvas图片到电脑 Fiddler自定义规则保存图片和提示The system proxy was changed自动重连 缩放 div 浏览器语音识别-webkitSpeechRecognition HTML获取摄像头画面,拍照截图保存 JS摄像头手势识别-Handsfree.js HTML,JS 模拟聊天界面UI JS获取元素相对窗口的位置和大小 C# 判断是否安装了ffmpeg shadow-root中的元素定位方法 JS MutationObserver监听DOM元素改变 Webview2动态设置页面video的Blob进行播放 C# 文件分割和文件合并 WPF刮刮乐 JS利用浏览器进行语言识别 WPF设置默认语言地区CultureInfo VS2022推送代码 到github错误: CertGetCertificateChain trust error CERT_TRUST_IS_PARTIAL_CHAIN的解决办法 C#正则表达式匹配候选词 使用ffmpeg去除音频静音
WPF鼠标拖动改变图片透明度
wgscd · 2026-02-02 · via 博客园 - wgscd
<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}";
        }
    }
      
 }