WPF之自定义绘图

1,创建自定义控件类

 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中添加该控件

 <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,相关代码

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

相关推荐

  1. WPF定义控件模版

    2024-05-03 06:06:04       12 阅读
  2. WPFDataGrid表格,定义表头、定义单元格

    2024-05-03 06:06:04       11 阅读
  3. WPF定义快捷命令

    2024-05-03 06:06:04       26 阅读
  4. Wpf-定义图标Button

    2024-05-03 06:06:04       16 阅读
  5. Wpf-定义状态控件

    2024-05-03 06:06:04       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-03 06:06:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-03 06:06:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-03 06:06:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-03 06:06:04       18 阅读

热门阅读

  1. opencv invert函数

    2024-05-03 06:06:04       9 阅读
  2. OpenGL 的内置矩阵种种

    2024-05-03 06:06:04       11 阅读
  3. 【.Net Core/.Net8教程】(三)如何优雅地校验参数

    2024-05-03 06:06:04       10 阅读
  4. iframe隐藏scrollbar并且还能够继续滚动

    2024-05-03 06:06:04       8 阅读
  5. 【Python】异常

    2024-05-03 06:06:04       9 阅读
  6. GESP一级 - 第三章 - 第1节 - 标准输入输出

    2024-05-03 06:06:04       9 阅读
  7. 目标跟踪难点及算法介绍

    2024-05-03 06:06:04       10 阅读
  8. 数据结构与算法——栈和队列

    2024-05-03 06:06:04       11 阅读
  9. 图文、视频处理等自媒体工具

    2024-05-03 06:06:04       9 阅读
  10. centos部署前后端项目

    2024-05-03 06:06:04       12 阅读
  11. Docker 虚拟机 WSL

    2024-05-03 06:06:04       11 阅读