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

相关推荐
Thomas.Sir几秒前
第4课:配置文件详解:application.yml/properties,多环境配置、配置分组、配置校验
java·spring boot
凯瑟琳.奥古斯特10 分钟前
动态调优IoU提升OCR文本合并精度
开发语言·python·算法
陌陌龙11 分钟前
【紧急预警!PHP 核弹级漏洞 php2shell】
开发语言·php
白露与泡影21 分钟前
一线 Java 后端面试八股(2026 更新版,含完整答案)
java·开发语言·面试
SL_staff23 分钟前
APS落地失败的5个隐形坑:离散制造排产为何总在Excel里打转?
java·人工智能·开源
企业解惑小能手-域智盾系统24 分钟前
内网监控软件策略不生效、日志缺失?一套从现象到根因的排查框架
开发语言·php·域智盾
1024奇点28 分钟前
【仓颉语言入门 · 第二篇 第 10 课】
开发语言·数据库·ide·mysql·开源
Code-zyc31 分钟前
读取电表数据并展示
java
布莱克60532 分钟前
A* 算法详解:从原理到实现
开发语言·c++·算法
多弗朗皮卡丘33 分钟前
C++ 智能指针
开发语言·c++·c++11·智能指针