首页 > 其他分享 >WPF mouse down on canvas and draw shapes which render with random colors

WPF mouse down on canvas and draw shapes which render with random colors

时间:2024-08-30 15:28:59浏览次数:4  
标签:canvas render Windows System cvs shapes new using elp

//custom control
//xaml
<UserControl x:Class="WpfApp307.ElpTbk"
             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:WpfApp307"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding ElpBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding ElpStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</UserControl>


//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 WpfApp307
{
    /// <summary>
    /// Interaction logic for ElpTbk.xaml
    /// </summary>
    public partial class ElpTbk : UserControl
    {
        public ElpTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public SolidColorBrush ElpBrush
        {
            get { return (SolidColorBrush)GetValue(ElpBrushProperty); }
            set { SetValue(ElpBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpBrushProperty =
            DependencyProperty.Register("ElpBrush", typeof(SolidColorBrush),
                typeof(ElpTbk), new PropertyMetadata(new SolidColorBrush(Colors.Red)));




        public string ElpStr
        {
            get { return (string)GetValue(ElpStrProperty); }
            set { SetValue(ElpStrProperty, value); }
        }

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


    }
}

 

 

 

 

//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
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 WpfApp307
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random rnd { get; set; }
        private int idx = 0;
        Canvas cvs { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            rnd = new Random();
            cvs = new Canvas();
            cvs.HorizontalAlignment = HorizontalAlignment.Stretch;
            cvs.VerticalAlignment = VerticalAlignment.Stretch;
            cvs.Background =new SolidColorBrush(Colors.Transparent);
            cvs.MouseDown += cvs_MouseDown;
            this.Content = cvs;
        }

        private void cvs_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ElpTbk elp = new ElpTbk();
            elp.Width = 100;
            elp.Height = 50;
            Color clr = new Color();
            clr.A = 255;
            clr.R=(byte)rnd.Next(255);
            clr.G=(byte)rnd.Next(255);
            clr.B = (byte)rnd.Next(255);
            elp.ElpBrush = new SolidColorBrush(clr);
            if(!cvs.Children.Contains(elp))
            { 
                Point p = e.GetPosition(this);
                elp.ElpStr = $"{++idx}   ({p.X - 50},{p.Y - 25})";
                Canvas.SetLeft(elp, p.X-50);
                Canvas.SetTop(elp, p.Y-25);
                cvs.Children.Add(elp);
            }
            this.Content = cvs;
        }
    }
}

 

 

 

 

标签:canvas,render,Windows,System,cvs,shapes,new,using,elp
From: https://www.cnblogs.com/Fred1987/p/18388828

相关文章

  • 【Canvas与艺术】夏日绝句 李清照
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>夏日绝句</title><styletype="text/css">......
  • 【Canvas与艺术】六边形漩涡
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>六边形漩涡</title><styletype="text/cs......
  • 图片作为Canvas贴图时要等图片加载完才可以读取canvas
    一、效果二、第一步:canvas.js中封装canvas函数,生成一个canvas对象,标注文字为参数nameunctioncreateCanvas(name){  /**   *创建一个canvas对象,绘制几何图案或添加文字   */  constcanvas=document.createElement("canvas");  constarr=......
  • CSS 的了解text-rendering属性
    text-renderingCSS属性提供了对浏览器如何渲染文本的控制。它主要用于优化文本显示,尤其是在需要处理大量文本或特定字体样式的场景下。通过设置这个属性,开发者可以影响文本的可读性、清晰度或渲染速度。text-rendering属性主要有以下几个值:auto:默认值。浏览器将自动决......
  • canvas实现睡眠波2.0,框架兼容,支持大部分PC端
    npm引入方式npmi@JayVone/sleepWave//使用方式1import{sleepWindow}from'@JayVone/sleepWave'//获取dom然后绑定constapp=document.getElementById('app');sleepWindow.init(app);//在需要绘制的时候setOptionssleepWindow.setOption({types:['......
  • canvas 如何自动去换行
    在HTMLcanvas上绘制文本时,如果文本超出了canvas的宽度,它不会自动换行。要实现自动换行,你需要手动计算文本的长度并在适当的位置进行换行。以下是一个简单的JavaScript函数,它使用canvas的measureText方法来计算文本的长度,并在达到指定宽度时自动换行:functionwrapText(context,......
  • 【Canvas与艺术】十边曲线形光阑
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>十边曲线型光阑</title><styletype="text/css&q......
  • ReactDOM.render 和 ReactDOM.createRoot 二者的区别
    ReactDOM.render和ReactDOM.createRoot都是用于在React应用程序中渲染组件的方法,但它们之间存在一些区别:ReactDOM.render:这个方法是React早期版本中使用的,现在已经被ReactDOM.createRoot替代。ReactDOM.render方法接受两个参数:第一个参数是要渲染的React组件,第二个......
  • 【Canvas电脑桌面】蓝底金圈纹桌面(1920*1080)
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>蓝底金圈纹桌面</title><styletype="text/css&quo......
  • 基于html2canvas实现网页保存为图片及图片清晰度优化
    一、实现HTML页面保存为图片1.1已知可行方案现有已知能够实现网页保存为图片的方案包括:方案1:将DOM改写为canvas,然后利用canvas的toDataURL方法实现将DOM输出为包含图片展示的dataURI方案2:使用html2canvas.js实现(可选搭配Canvas2Image.js实现网页保存为图片)方案3:使用raster......