首页 > 编程语言 >WPF C# create canvas and draw ellipse in canvas

WPF C# create canvas and draw ellipse in canvas

时间:2024-03-26 11:00:13浏览次数:24  
标签:canvas C# draw System int Windows new using elp

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 WpfApp17
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Canvas cvs = new Canvas();
            this.WindowState = WindowState.Maximized;
            int width = (int)this.ActualWidth-60;
            int height = (int)this.ActualHeight-60;
            SolidColorBrush brush = new SolidColorBrush(Colors.Red);
            Random rnd = new Random();
            for (int i=0;i<150;i++)
            {
                Ellipse elp = new Ellipse();
                elp.Width = 50;
                elp.Height = 30;
                switch(i%4)
                {
                    case 0:
                        brush=new SolidColorBrush(Colors.Red);
                        break;
                    case 1:
                        brush = new SolidColorBrush(Colors.Green);
                        break;
                    case 2:
                        brush = new SolidColorBrush(Colors.Blue);
                        break;
                    case 3:
                        brush = new SolidColorBrush(Colors.Yellow);
                        break;
                }
                elp.Fill = brush;
                elp.Stroke=new SolidColorBrush(Colors.Black);
                elp.StrokeThickness = 5;
                Canvas.SetLeft(elp, rnd.Next(0, width));
                Canvas.SetTop(elp, rnd.Next(0, height));
                cvs.Children.Add(elp);
                //this.AddChild(cvs);
                this.Content = cvs;
            }
            
            //for(int i=0;i<width;i=i+10)
            //{
            //    for (int j = 0; j < height; j = j + 10)
            //    {
            //        Ellipse elp = new Ellipse();
            //        elp.Width = 5;
            //        elp.Height = 5;
            //        elp.Fill = brush;
            //        elp.StrokeThickness = 2;
            //        Canvas.SetLeft(elp, i);
            //        Canvas.SetTop(elp,j);
            //        cvs.Children.Add( elp );
            //    }
            //}
        }
    }
}

 

 

 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
 {
     Canvas cvs = new Canvas();
     this.WindowState = WindowState.Maximized;
     int width = (int)this.ActualWidth-50;
     int height = (int)this.ActualHeight;
     SolidColorBrush brush = new SolidColorBrush(Colors.Red);
     Random rnd = new Random();

     for (int i = 0; i < width; i = i + 100)
     {
         for (int j = 0; j < height; j = j + 60)
         {
             Ellipse elp = new Ellipse();
             elp.Width = 50;
             elp.Height = 30;
             elp.Fill = brush;
             elp.StrokeThickness = 2;
             Canvas.SetLeft(elp, i);
             Canvas.SetTop(elp, j);
             cvs.Children.Add(elp);
         }
     }
     this.Content = cvs;
 }

 

标签:canvas,C#,draw,System,int,Windows,new,using,elp
From: https://www.cnblogs.com/Fred1987/p/18096115

相关文章

  • 别让 Docker 毁了你的 MySQL!
    Docker容器技术的火热,越来越多的应用开始被"装箱"。我们这些开发者享受着Docker带来的便捷和灵活,几乎恨不得把所有东西都塞进容器里。于是,连MySQL这样的有状态服务,也开始频频出现在Docker的舞台上。但是,把MySQL装进Docker,真的是个明智的选择吗?是否会给系统稳......
  • Nacos 未授权访问漏洞测试
    漏洞描述:2020年12月29日,Nacos官方在github发布的issue中披露AlibabaNacos存在一个由于不当处理User-Agent导致的未授权访问漏洞。通过该漏洞,攻击者可以进行任意操作,包括创建新用户并进行登录后操作影响版本:Nacos<=2.0.0-ALPHA.1漏洞地址:GEThttp://IP:端口/nacos/v1/auth/......
  • SAP BW4中CP与CP对应系统视图的字段关系
     可以发现CP:ZA1SD_M05的视图0BW:BIA:ZA1SD_M05的字段后续都加上的KEY,一般情况下去掉_KEY就是CP的源字段,不确定的话可以打开  可以看出来源字段及目标字段是那个。 ......
  • Windows 10无法登录Xbox及其附属产品(包括但不限于Game Bar,Minecraft Launcher)
     1. 问题描述:打开Xbox(如下图) 或GameBar(如下图)  后,单击登录,会弹出一个窗口,印有自己账户的头像,下方一行小字“欢迎回来,$昵称$”,如下图所示:  单击唯一的绿色按钮“现在就开始吧”,该窗口消失,马上又回到点击登录前的界面。循环尝试结果都不变。2.解决方法第一步......
  • How to get the client IP address with Javascript only
    LearnhowtogettheclientIPaddress(localandprivate)usingonlyjavascript.​​Javascript无法获取(也无法存储)客户端IP,但是Javascript能够创建Http请求,并且服务器端语言能够检索用户的公共IP,因此您可以利用这一优势。换句话说,如果你想检索用户的公共IP,你将依赖于对任......
  • .net core上传文件、.net core接收文件上传
    我要做一个winform程序,上传文件到.net8的文件上传接口winform///<summary>///选择图片上传///</summary>///<paramname="sender"></param>///<paramname="e"></param>privateasyncvoidbtnExtract_Click(objectsender,Even......
  • maven打包程序包com.sun.*** 包不存在
    需要在pom文件中加入<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><showWarnings>true</showWarnings>......
  • CPPB 表中的TXN_CATEGORY
    cst_pac_period_balanceTXN_CATEGORY1    期初    2    成本更新:新成本或百分比变动    3    自有成本事务处理    4    非返工完成    5    成本更新:值变动    6    返工发放    7    返工完成    8    组......
  • System.Security.Cryptography.RijndaelManaged()
    以下为ai生成:System.Security.Cryptography.RijndaelManaged 是.NET框架中的一个加密类,用于提供高级加密标准(AES)算法的实现。AES是一种强大的对称加密算法,它可以用于保护数据的安全。以下是一个使用RijndaelManaged进行数据加密和解密的简单例子:usingSystem;usingSystem.I......
  • 前端学习-TypeScript-001-了解、安装TypeScript
    菜鸟教程链接TypeScript简介TypeScript是JavaScript的超集,扩展了JavaScript的语法,因此现有的JavaScript代码可与TypeScript一起工作无需任何修改,TypeScript通过类型注解提供编译时的静态类型检查。TypeScript可处理已有的JavaScript代码,并只对其中的TypeScript......