首页 > 系统相关 >WPF System.Windows.Media.Color A value must be set, display ball and number in canvas ,mouse down to

WPF System.Windows.Media.Color A value must be set, display ball and number in canvas ,mouse down to

时间:2024-09-19 22:25:44浏览次数:1  
标签:canvas set Windows xaml System cr using public

 private Color GetRndColor()
 {
     Color cr = new Color();
     cr.A = 255;
     cr.R = (byte)(rnd.Next(0, 255));
     cr.G = (byte)(rnd.Next(0, 255));
     cr.B = (byte)(rnd.Next(0, 255));
     return cr;
 }

 

 

 

 

 

 

 

 

 

//usercontrol.xaml
<UserControl x:Class="WpfApp382.EllipseTextBlockBrush"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp382"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding FillColorBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding FillStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="30"
                   Foreground="Red"/>
    </Grid>
</UserControl>



//usercontrol.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp382
{
    /// <summary>
    /// Interaction logic for EllipseTextBlockBrush.xaml
    /// </summary>
    public partial class EllipseTextBlockBrush : UserControl
    {
        public EllipseTextBlockBrush()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public SolidColorBrush FillColorBrush
        {
            get { return (SolidColorBrush)GetValue(FillColorBrushProperty); }
            set { SetValue(FillColorBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FillColorBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FillColorBrushProperty =
            DependencyProperty.Register("FillColorBrush", typeof(SolidColorBrush),
                typeof(EllipseTextBlockBrush), new PropertyMetadata(null));




        public string FillStr
        {
            get { return (string)GetValue(FillStrProperty); }
            set { SetValue(FillStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FillStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FillStrProperty =
            DependencyProperty.Register("FillStr", typeof(string), 
                typeof(EllipseTextBlockBrush), new PropertyMetadata(""));



    }
}


//mainwindow.xaml
<Window x:Class="WpfApp382.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp382"
        mc:Ignorable="d" 
        WindowState="Maximized"
        MouseDown="Window_MouseDown"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Canvas x:Name="cvs"/>
    </Grid>
</Window>


//mainwindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp382
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random rnd { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            rnd = new Random();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DrawElps();
        }

        private void DrawElps()
        {
            int width = (int)this.ActualWidth - 100;
            int height = (int)this.ActualHeight - 100;
            for(int i=0;i<100;i++)
            {
                EllipseTextBlockBrush elp = new EllipseTextBlockBrush();
                elp.Width = 50;
                elp.Height = 50;
                elp.FillColorBrush = new SolidColorBrush(GetRndColor());
                elp.FillStr = $"{i + 1}";
                Canvas.SetLeft(elp,rnd.Next(0,width));
                Canvas.SetTop(elp,rnd.Next(0,height));
                if(!cvs.Children.Contains(elp))
                {
                    cvs.Children.Add(elp);
                }
            }
        }

        private Color GetRndColor()
        {
            Color cr = new Color();
            cr.A = 255;
            cr.R = (byte)(rnd.Next(0, 255));
            cr.G = (byte)(rnd.Next(0, 255));
            cr.B = (byte)(rnd.Next(0, 255));
            return cr;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            cvs.Children.Clear();
            DrawElps();
        }
    }
}

 

标签:canvas,set,Windows,xaml,System,cr,using,public
From: https://www.cnblogs.com/Fred1987/p/18421489

相关文章

  • WPF Customcontrol with ellipse and textblock display randomly in canvas of mainw
    //usercontrol.xaml<UserControlx:Class="WpfApp381.ElpImgTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • 一文读懂DaemonSet以及实践攻略
    一文读懂DaemonSet以及实践攻略目录1概念1.1什么是DaemonSet1.2DaemonSet的工作机制1.3适用场景1.4DaemonSet与Deployment的区别1.5DaemonSet的通信模式2实践案例:部署和更新Fluentd日志收集器2.1部署FluentdDaemonSet2.1.1定义FluentdDaemonSet2.1.2......
  • Windows 调试工具课程
    本文是我在集团内部上的课程记录而成的博客内容。在本次课程里面将和大家介绍一些在Windows上常用的调试工具,以及调查问题的常见套路。适合于伙伴们入门Windows调试本文以下内容是采用原本课程课件里面的一页页的内容组装而来,过程中补充一些讲课时的内容本次课程里面核心的......
  • 欧拉系统setfacl规则
    setfacl是Linux系统中用于设置文件和目录的访问控制列表(ACLAccessControlList)的命令。ACL提供了一种比传统的Unix权限(如rwx)更细粒度的权限控制机制,允许我们自己为特定的用户或者组设置单独的权限setfacl语法结构setfacl[options][rules]file...options:命令选项;r......
  • Windows 下 EGL PBuffer 测试代码
    Windows 上使用glad加载浏览器的LibEGL.dll 和LibGLESV2.dll,测试 EGL 在Windows 上的实现,代码:1#include<stdio.h>23#include"include/glad/egl.h"4#include"include/glad/gles3.2.h"56#defineSTB_IMAGE_WRITE_IMPLEMENTATION......
  • windows安装docker
    环境准备启用Hyper-V打开windows功能,找到Hyper-V并勾选,如果找不到Hyper-V新建txt,然后编辑内容pushd"%~dp0"dir/b%SystemRoot%\servicing\Packages\*Hyper-V*.mum>hyper-v.txtfor/f%%iin('findstr/i.hyper-v.txt2^>nul')dodism/online/norestart/add-p......
  • 修复Windows系统中mt6.dll文件缺失或损坏的问题——了解mt6.dll错误的原因及有效的解
    在使用Windows操作系统时,有时会遇到诸如“找不到mt6.dll”或“mt6.dll已损坏”等错误信息。这些问题可能会阻止应用程序的正常运行,给用户带来不便。本文将探讨这些问题的常见原因,并提供有效的解决方法,帮助用户快速恢复正常操作。原因分析文件缺失:用户可能无意中删除了mt6.......
  • Windows安装时调出系统的cmd功能 Shift+F10
    Windows安装时调出系统的cmd功能Shift+F10(笔记本可能是Shift+FN+F10)比如可以转换磁盘分区形式。 在Windows安装程序中同时按下【Shift+F10】键以打开命令提示符窗口,并按顺序输入以下命令。 diskpart listdisk(会显示磁盘列表) selectdisk1(1为要安装......
  • windows无法安装到这个磁盘,选中的磁盘采用gpt分区
    在安装Windows时,出现“windows无法安装到这个磁盘,选中的磁盘采用gpt分区”的错误提示,可以尝试两种解决方法:方法一,通过Diskpart工具将GPT转换为MBR;方法二,将引导模式更改为UEFI模式。以上是解决此问题的方法。摘要由作者通过智能技术生成有用有些用户反馈在安装Windows的过......
  • 从方法、操作流程等方面对Windows和Linux的命令进行对比
    Windows和Linux是两个常见的操作系统,它们都有自己的命令行接口。尽管两者的目的都是相同的——执行特定的任务,但它们的命令之间存在一些差异。下面将从方法、操作流程等方面对Windows和Linux的命令进行对比。一、文件和目录操作:列出目录中的文件:–Windows命令:dir–Linux命......