C# wpf 附加属性实现任意控件拖动调整大小

news2024/10/7 6:41:53

摘自这里 https://blog.csdn.net/u013113678/article/details/121719278
调试效果如下

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

namespace Wpf
{
    public class GridAdorner : Adorner
    { //4条边
        Thumb _leftThumb, _topThumb, _rightThumb, _bottomThumb;
        //4个角
        Thumb _leftTopThumb, _rightTopThumb, _rightBottomThumb, _leftbottomThumb;
        //布局容器,如果不使用布局容器,则需要给上述8个控件布局,实现和Grid布局定位是一样的,会比较繁琐且意义不大。
        Grid _grid;
        UIElement _adornedElement;

        public GridAdorner(UIElement adornedEleemnt) : base(adornedEleemnt)
        {
            _adornedElement = adornedEleemnt;
            //初始化thumb
            _leftThumb = new Thumb();
            _leftThumb.HorizontalAlignment = HorizontalAlignment.Left;
            _leftThumb.VerticalAlignment = VerticalAlignment.Center;
            _leftThumb.Cursor = Cursors.SizeWE;
            _topThumb = new Thumb();
            _topThumb.HorizontalAlignment = HorizontalAlignment.Center;
            _topThumb.VerticalAlignment = VerticalAlignment.Top;
            _topThumb.Cursor = Cursors.SizeNS;
            _rightThumb = new Thumb();
            _rightThumb.HorizontalAlignment = HorizontalAlignment.Right;
            _rightThumb.VerticalAlignment = VerticalAlignment.Center;
            _rightThumb.Cursor = Cursors.SizeWE;
            _bottomThumb = new Thumb();
            _bottomThumb.HorizontalAlignment = HorizontalAlignment.Center;
            _bottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
            _bottomThumb.Cursor = Cursors.SizeNS;
            _leftTopThumb = new Thumb();
            _leftTopThumb.HorizontalAlignment = HorizontalAlignment.Left;
            _leftTopThumb.VerticalAlignment = VerticalAlignment.Top;
            _leftTopThumb.Cursor = Cursors.SizeNWSE;
            _rightTopThumb = new Thumb();
            _rightTopThumb.HorizontalAlignment = HorizontalAlignment.Right;
            _rightTopThumb.VerticalAlignment = VerticalAlignment.Top;
            _rightTopThumb.Cursor = Cursors.SizeNESW;
            _rightBottomThumb = new Thumb();
            _rightBottomThumb.HorizontalAlignment = HorizontalAlignment.Right;
            _rightBottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
            _rightBottomThumb.Cursor = Cursors.SizeNWSE;
            _leftbottomThumb = new Thumb();
            _leftbottomThumb.HorizontalAlignment = HorizontalAlignment.Left;
            _leftbottomThumb.VerticalAlignment = VerticalAlignment.Bottom;
            _leftbottomThumb.Cursor = Cursors.SizeNESW;
            _grid = new Grid();
            _grid.Children.Add(_leftThumb);
            _grid.Children.Add(_topThumb);
            _grid.Children.Add(_rightThumb);
            _grid.Children.Add(_bottomThumb);
            _grid.Children.Add(_leftTopThumb);
            _grid.Children.Add(_rightTopThumb);
            _grid.Children.Add(_rightBottomThumb);
            _grid.Children.Add(_leftbottomThumb);
            AddVisualChild(_grid);
            foreach (Thumb thumb in _grid.Children)
            {
                thumb.Width = 16;
                thumb.Height = 16;
                thumb.Background = Brushes.Red;
                thumb.Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(new SolidColorBrush(Colors.Red))
                };
                thumb.DragDelta += Thumb_DragDelta;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _grid;
        }

        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Debug.WriteLine(finalSize.Width + ":" + finalSize.Height);
            //直接给grid布局,grid内部的thumb会自动布局。
            _grid.Arrange(new Rect(new Point(-_leftThumb.Width / 2, -_leftThumb.Height / 2), new Size(finalSize.Width + _leftThumb.Width, finalSize.Height + _leftThumb.Height)));
            Debug.WriteLine(finalSize.Width + ":1      " + finalSize.Height);

            return finalSize;
        }

        private FrameworkElementFactory GetFactory(SolidColorBrush back)
        {
            var fef = new FrameworkElementFactory(typeof(Ellipse));
            fef.SetValue(Ellipse.FillProperty, back);
            fef.SetValue(Ellipse.StrokeProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#999999")));
            fef.SetValue(Ellipse.StrokeThicknessProperty, (double)2);

            return fef;
        }

        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            var c = _adornedElement as FrameworkElement;
            var thumb = sender as FrameworkElement;
            double left, top, right, bottom, width, height;
            if (thumb.HorizontalAlignment == HorizontalAlignment.Left)
            {
                right = c.Margin.Right;
                left = c.Margin.Left + e.HorizontalChange;
                width = (double.IsNaN(c.Width) ? c.ActualWidth : c.Width) - e.HorizontalChange;
            }
            else
            {
                left = c.Margin.Left;
                right = c.Margin.Right - e.HorizontalChange;
                width = (double.IsNaN(c.Width) ? c.ActualWidth : c.Width) + e.HorizontalChange;
            }

            if (thumb.VerticalAlignment == VerticalAlignment.Top)
            {
                bottom = c.Margin.Bottom;
                top = c.Margin.Top + e.VerticalChange;
                height = (double.IsNaN(c.Height) ? c.ActualHeight : c.Height) - e.VerticalChange;
            }
            else
            {
                top = c.Margin.Top;
                bottom = c.Margin.Bottom - e.VerticalChange;
                height = (double.IsNaN(c.Height) ? c.ActualHeight : c.Height) + e.VerticalChange;
            }

            if (thumb.HorizontalAlignment != HorizontalAlignment.Center)
            {
                if (width > 0)
                {
                    c.Margin = new Thickness(left, c.Margin.Top, right, c.Margin.Bottom);
                    c.Width = width;
                }
            }
            if (thumb.VerticalAlignment != VerticalAlignment.Center)
            {
                if (height > 0)
                {
                    c.Margin = new Thickness(c.Margin.Left, top, c.Margin.Right, bottom);
                    c.Height = height;
                }
            }
        }
    }
}

用法

<Window>
<Grid>
        <Border x:Name="border" Width="200" Height="200" Background="Gray" />
    </Grid>
</Window>

public partial class WinResize : Window
    {
        public WinResize()
        {
            InitializeComponent();
            this.Loaded += (s, e) => {
               var layer  =  AdornerLayer.GetAdornerLayer(border);
                layer.Add(new GridAdorner(border));
            };
        }
    }

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/692300.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue修改数据后页面不重新渲染

使用vue&#xff0c;遇到几次修改了对象的属性后&#xff0c;页面并不重新渲染 一、直接添加属性的问题 <template><div><p v-for"(value,key) in item" :key"key">{{ value }}</p><button click"addProperty">…

【Linux】在Ubuntu下部署nginx——nginx的负载均衡

介绍 这里是小编成长之路的历程&#xff0c;也是小编的学习之路。希望和各位大佬们一起成长&#xff01; 以下为小编最喜欢的两句话&#xff1a; 要有最朴素的生活和最遥远的梦想&#xff0c;即使明天天寒地冻&#xff0c;山高水远&#xff0c;路远马亡。 一个人为什么要努力&a…

vs中的一些编码问题

1. 基本概念 文件编码格式 首先vs中编辑&#xff0c;保存文件实际上也是将文件内容以一定的编码格式存储。对于中文字符串 string s "一"; 按不同的编码格式保存后&#xff0c;通过16进制文件查看器&#xff0c;可以看到中文的编码是能对应上的。 开放高级保存…

select和pselect的关于信号打断的实验

select和pslect的man手册 SELECT(2) Linux Programmers Manual SELECT(2)NAMEselect, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O …

【HDFS实战】HDFS联合(联邦)集群的发展史

HDFS联合集群的发展史 文章目录 HDFS联合集群的发展史HDFS原始架构方案一 HDFS Federation方案二 ViewFs方案三 HDFS Router-based Federation常用命令常用配置RPC serverConnection to the NamenodesAdmin serverHTTP ServerState StoreRoutingNamenode monitoring 版本相关is…

nestJS打印多层对象以及铺平金额类型

console.dir(ret, { depth: 3 }); 金额是Decimal类型不好显示 Logger.verbose(JSON.stringify(ret, null, 2)); 利用JSON.stringify参数格式化 null: 不对数据进行提取&#xff0c; 2&#xff1a;缩进两个空格&#xff0c; 效果是直接帮忙平铺金额对象

java.lang.IllegalStateException Unable to find a @SpringBootConfiguration代码报错

文章目录 一. 问题场景二. 报错原因三. 解决方案 一. 问题场景 使用idea基于springBoot的项目进行单元测试时&#xff0c;出现异常&#xff0c;如下所示&#xff1a; Test ignored.java.lang.IllegalStateException: Unable to find a SpringBootConfiguration, you need to …

【电源专题】案例:电池保护芯片有是否能向0V电池充电的功能的区别

本案例发生在实际的工作之中。案例是这样的,有些产品因为各种原因没有按规范将电池与设备断开,而是插入机器(存在1mA的静态功耗)中并存放在仓库里2年后发现产品无法开机使用,并且电池无法充电。更换新的电池后运行正常,所以认定为是电池的问题。 可是为什么电池无法充电呢…

学妹:怎样才能设计出优秀的测试用例?

这篇文章我们主要聊一下测试工程师最通用的也是最根本的技能&#xff0c;测试用例的设计能力。 测试用例 测试用例是通过使用在测试计划中确定的测试技术&#xff0c;对于已确定的测试条件进行逐步推敲&#xff0c;精炼而设计出来的重点说明如何具体操作产生何种结果的文档。…

(免费分享)基于springboot,vue高校就业系统

管理员&#xff1a;10086/123 学生&#xff1a;10087/123 企业&#xff1a;10070/123 辅导员&#xff1a;10090/123 项目描述 高校就业管理系统 学生 : 个人信息、查看企业岗位信息、简历信息管理、我的应聘 辅导员 : 学生信息管理、三方协议书审核、查看班级就业统计信息 企…

App启动流程分析(一)

一、App启动涉及到的三个进程 1、Launcher进程&#xff1a;负责接收用户点击屏幕的事件&#xff0c;它其实就是一个Activity&#xff0c;屏幕上的各种Icon就是这个Activity中的Button&#xff0c;当点击Icon时&#xff0c;会触发启动App的流程。 2、SystemServer进程&#xff…

新手小白学JAVA_IDEA修改编辑与控制台字体大小

很多小白在刚刚使用IDEA的时候还不是很熟练 本文主要给大家提供一些使用的小技巧&#xff0c;希望能帮助到你 1.改变编辑窗口字体大小 1.1 将字体大小设置为固定值 我们可以将编辑窗口的字体大小设置为固定值 1.2 动态改变字体的大小 我们还可以通过Ctrl鼠标滚轮改变编辑窗口…

微信小程序怎么制作自己的小程序

小程序制作是指通过工具或开发语言制作微信平台上的小型应用程序&#xff0c;具有轻量、易用、无需下载安装等优点。随着移动互联网的飞速发展&#xff0c;小程序已经成为了各种企业和个人展示自己、提供服务的重要手段之一。 小程序制作的好处 小程序可以提高品牌曝光度。在…

SpringBoot + 规则引擎 URule,真的太强悍了!

一、背景 前段时间&#xff0c;在做项目重构的时候&#xff0c;遇到很多地方需要做很多的条件判断。当然可以用很多的if-else判断去解决&#xff0c;但是当时也不清楚怎么回事&#xff0c;就想玩点别的。于是乎&#xff0c;就去调研了规则引擎。 当然&#xff0c;市面上有很多…

Springboot 中RESTtemplate的使用

目录 一 概述 二 应用 1.get请求 2.post请求 一 概述 RESTtemplate提供了http请求连接的功能。spring 框架提供的 RestTemplate 类可用于在应用中调用 rest 服务&#xff0c;它简化了与 http 服务的通信方式&#xff0c;统一了 RESTful 的标准&#xff0c;封装了 http 链接…

行业报告 | 智能教育发展蓝皮书(下)

原创 | 文 BFT机器人 核心观点 Core point 在教育数字化转型的背景下&#xff0c;积极探索智能技术助力教学减负增效的实践路径&#xff0c;对于深化教育教学改革&#xff0c;促进学生全面发展、健康成长具有重要现实意义。 03 智能技术助力教学设计 教学设计以解决实际的教学…

【半监督语义分割 2023 CVPR】CCVC

【半监督语义分割 2023 CVPR】CCVC 论文题目&#xff1a;Conflict-Based Cross-View Consistency for Semi-Supervised Semantic Segmentation 中文题目&#xff1a;半监督语义分割的基于冲突的交叉视图一致性 论文链接&#xff1a;https://arxiv.org/abs/2303.01276 论文代码&…

Visual Basic 6 25 周年

Visual Basic 6.0 是 Visual Basic“经典”的最后一个版本&#xff08;VB.NET 之前的版本&#xff09;。它是迄今为止 32 位 Windows 95/NT 及更高版本最流行的版本。它非常受欢迎&#xff0c;并且仍在许多公司中使用。它于 1998 年中发布&#xff0c;与 5.0 版本相比在多个方面…

mysql-数据迁移

文章目录 1. 物理迁移1. 迁移前&#xff0c;配置mysql的输出目录1. 查看mysql的输出目录2. 修改mysql的输出目录 2. 文件迁移 1. 物理迁移 1. 迁移前&#xff0c;配置mysql的输出目录 1. 查看mysql的输出目录 在安装MySQL的会限制了导入与导出的目录权限。只允许在规定的目录…

基于.net core的微信小程序接入微信支付系列之环境搭建(1)

前言&#xff1a;c#语言本身是一门非常优雅的语言&#xff0c;但是在腾讯的api文档里面并不受到待见&#xff0c;所以只能靠自己看文档来逐步摸索&#xff0c;微信支付的逻辑看起来很复杂&#xff0c;其主要原因在于腾讯写文档的人可能是学体育专业的&#xff0c;简单的逻辑非要…