目录

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

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
烁3474 分钟前
每日一题(小白)模拟娱乐篇13
java·算法·娱乐·暴力
2301_793069825 分钟前
前后端分离下,Spring Boot 请求从发起到响应的完整执行流程
java·spring boot·mvc
Knock man5 分钟前
QML和C++交互
开发语言·c++·交互
褚瑱琅8 分钟前
T-SQL语言的压力测试
开发语言·后端·golang
烁34713 分钟前
每日一题(小白)模拟娱乐篇14
java·开发语言·算法·娱乐·暴力
✿ ༺ ོIT技术༻14 分钟前
C++11:lambda表达式
开发语言·c++
xiaolingting2 小时前
Java 二叉树非递归遍历核心实现
java··二叉树非递归遍历
嘵奇2 小时前
深入解析 Java 8 Function 接口:函数式编程的核心工具
java·开发语言
东方靖岚3 小时前
R语言的数据库交互
开发语言·后端·golang
一路向北North4 小时前
IDEA加载项目时依赖无法更新
java·ide·intellij-idea