WPF之自定义绘图

1,创建自定义控件类

cs 复制代码
 class CustomDrawnElement:FrameworkElement
    {
        public static readonly DependencyProperty BackgroundColorProperty;

        static CustomDrawnElement()
        {
            FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(Colors.SkyBlue);
            meta.AffectsRender = true;//依赖属性改变时要求重绘
            BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(CustomDrawnElement), meta);
        }
        /// <summary>
        /// 背景颜色(依赖属性)
        /// </summary>
        [Bindable(true),Category("自定义设置"),Browsable(true)]
        public Color BackgroundColor
        {
            get
            {
                return (Color)GetValue(BackgroundColorProperty);
            }
            set
            {
                SetValue(BackgroundColorProperty, value);
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.InvalidateVisual();
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            this.InvalidateVisual();
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
            //自定义绘图
            drawingContext.DrawRectangle(GetBrush(), null, new Rect(0, 0, this.ActualWidth, this.ActualHeight));
            base.OnRender(drawingContext);
        }
        Brush GetBrush()
        {
            if (!this.IsMouseOver)
            {
                return new SolidColorBrush(BackgroundColor);
            }
            Point curPoint = Mouse.GetPosition(this);
            //计算相对位置
            RadialGradientBrush radialBrush = new RadialGradientBrush(Colors.White, BackgroundColor);
            Point relativePoint = new Point(curPoint.X / this.ActualWidth, curPoint.Y / this.ActualHeight);
            radialBrush.Center = relativePoint;
            radialBrush.GradientOrigin = relativePoint;
            return radialBrush;
            // radialBrush.GradientOrigin

        }
    }

2,在UI中添加该控件

cs 复制代码
 <Grid>
       
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <local:CustomDrawnElement BackgroundColor="{Binding ElementName=combo01, Path=Text}"></local:CustomDrawnElement>
        <StackPanel Margin="10" Orientation="Horizontal" Grid.Row="1">
            <TextBlock VerticalAlignment="Center" Text="选择背景色:" FontSize="16"></TextBlock>
            <ComboBox Width="150" FontSize="16" VerticalContentAlignment="Center" x:Name="combo01"></ComboBox>
        </StackPanel>
    </Grid>

3,相关代码

cs 复制代码
 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<string> colors = new List<string>();
            foreach (var item in typeof(Colors).GetProperties(System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.Static))
            {
                colors.Add(item.Name);
            }
            combo01.ItemsSource = colors;
            combo01.SelectedIndex = 0;
        }
    }

4,效果

相关推荐
土司大王4 小时前
LeetCode hot100——394.字符串解码:Java 双栈模拟
java·算法·leetcode
邪修king4 小时前
Re:Linux系统篇(二十五):文件系统(一):磁盘硬件底层原理:从物理结构到 CHS/LBA 寻址,搞懂硬盘数据的定位逻辑
java·linux·运维·gpt
驭渊的小故事4 小时前
Spring Boot 注解详解01
java·前端·spring boot
泡海椒4 小时前
jquick-pdf 超详细入门教程:Java 轻量级 HTML 模板生成 PDF 工具
java·开发语言
蛋先生DX4 小时前
Java和Go都拍胸脯说"内存我包了",可它们到底是怎么下手的?
java·go·编程语言
八年。。4 小时前
WSL学习(三)——搭建Linux + C++ + SOME/IP 基础开发环境
开发语言·笔记·ubuntu
SimonKing4 小时前
阅后即焚的加密便签:Cryptgeon,你口袋里的秘密信使
java·后端·程序员
孙启超4 小时前
【AI开发之Rust】第 6 课:结构体、枚举与方法 —— 开始定义自己的类型
开发语言·人工智能·后端·ai·rust
ss2734 小时前
Java全栈实战 | 1.3-03 索引原理:联合索引 (a,b,c) 只查 b 走不了索引?B+ 树一图讲透所有索引玄学
java·数据库
电商API_180079052474 小时前
电商平台数据分析实战_帖子
开发语言·爬虫·python·数据采集·京东