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,效果

相关推荐
努力的lpp7 小时前
php反序列化一(大白话版)
开发语言·web安全·php·ctf·反序列化
菜鸟~noob2338 小时前
【电子战】第03篇:CFAR(恒虚警)处理【含matlab代码】
开发语言·matlab
lhldsg8 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都8 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
泡海椒8 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
hqyjzsb9 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
小羊没烦恼!9 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
Keven_119 小时前
算法札记:ACM适用的很骚的C++语法(持续更新)
开发语言·c++
脉动数据行情110 小时前
C# 实现德指 DAX 期货 WebSocket 实时行情监听
开发语言·websocket·c#