C# Winform代码片段-大二下学期的垃圾代码

时间:2022-09-28 23:33:42

1.图片缩放

using System;
using System.Windows.Forms;
using System.Drawing;
class haha : Form
{
    static void Main()
    {
        Application.Run(new haha());
    }
    Bitmap bit;
    int x, y;
    haha()
    {
        ClientSize = new Size(400, 400);
        bit = new Bitmap(800, 800);
        Graphics.FromImage(bit).DrawImage(Image.FromFile("beauty.jpg"),new Rectangle(new Point(0,0),new Size(bit.Width,bit.Height)));
        KeyDown += roll;
        MouseWheel += zoom;
        x = y = 0;
    }
    void zoom(object o, MouseEventArgs e)
    {
        if ((bit.Width < 100||bit.Height<100)&&e.Delta<0) return;
        if (bit.Width + e.Delta < 0 || bit.Height + e.Delta < 0) return;
        Bitmap b = new Bitmap(bit.Width + e.Delta, bit.Height + e.Delta);
        Graphics.FromImage(b).DrawImage(bit, new Rectangle(0, 0, b.Width, b.Height));
        bit = b;
        draw();
    }
    void draw()
    {
        if (x >= bit.Width - ClientSize.Width) x = bit.Width - 1 - ClientSize.Width;
        if (y >= bit.Height - ClientSize.Height) y = bit.Height - ClientSize.Height - 1;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        Bitmap b = new Bitmap(ClientSize.Width, ClientSize.Height);
        int i, j;
        for (i = 0; i < ClientSize.Width; i++)
            for (j = 0; j < ClientSize.Height; j++)
            {
                if (x + i >= bit.Width || y + j >= bit.Height)
                    b.SetPixel(i, j, Color.AliceBlue);
                else
                    b.SetPixel(i, j, bit.GetPixel(x + i, y + j));
            }
        CreateGraphics().DrawImage(b, 0, 0);
    }
    void roll(object o, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up: y -= 10; break;
            case Keys.Down: y += 10; break;
            case Keys.Right: x += 10; break;
            case Keys.Left: x -= 10; break;
        }
        draw();
    }
}

2.位图操作-乱画

using System.Drawing;
using System.Windows.Forms;
using System;
class haha:Form
{
    Bitmap bit;
    Random r = new Random();
    static void Main()
    {
        Application.Run(new haha());
    }
    void circle(int xx, int yy, int r,Color c)
    {
        int x; double y;
        for (x = xx - r; x < xx + r; x++)
        {
            y = Math.Sqrt(r * r - (xx - x) * (xx - x));
            if (x < 0 || yy - y < 0 || x >= bit.Width || yy + y >= bit.Height) continue;
            bit.SetPixel(x, (int)(y+yy), c);
            bit.SetPixel(x, (int)( yy-y), c);
        }
    }
    void line(int x1,int y1,int x2,int y2,Color c)
    {
        int fx, fy, tx, ty;
        if(x1==x2){
             if(y1>y2){
                fx=x2;fy=y2;
                tx=x1;ty=y1;
            }
            else {
                fx=x1;fy=y1;
                tx=x2;ty=y2;
            }
            int i;
            for (i = fy; i < ty; i++)
            {
                bit.SetPixel(x1, i, c);
            }
            CreateGraphics().DrawImage(bit,0,0);
            return;
        }
        if (x1 > x2)
        {
            fx = x2; fy = y2;
            tx = x1; ty = y1;
        }
        else
        {
            fx = x1; fy = y1;
            tx = x2; ty = y2;
        }
        double k = (ty - fy) / (tx - fx);
        double y;
        int x;
        for (x = fx; x < tx; x++)
        {
            y = k * x + ((double)y1 - k * x1);
            bit.SetPixel(x, (int)y, c);
        }
        CreateGraphics().DrawImage(bit, 0, 0);
    }
    haha()
    {
        ClientSize = new Size(500, 500);
        bit = new Bitmap(ClientSize.Width, ClientSize.Height);
        Graphics.FromImage(bit).Clear(Color.White);
        Timer t1 = new Timer();
        t1.Interval = 5000;
        t1.Tick += draw_line;
        //t1.Start();
        Timer t2 = new Timer();
        t2.Interval = 10;
        t2.Tick += draw_circle;
        t2.Start();
    }
    void draw_line(object o,EventArgs e)
    {
        line(r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, Color.FromArgb(r.Next()));
    }
    void draw_circle(object o, EventArgs e)
    {
        int rr=r.Next() % 100 + 1;
        int xx=r.Next() % ClientSize.Width;
        int yy=r.Next() % ClientSize.Height;
        for (int i = 0; i < 16&&rr>i;i++ )
            circle(xx,yy , rr-i, Color.FromArgb(r.Next()));
        CreateGraphics().DrawImage(bit, 0, 0);
    }
}

3.图片旋转

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyDown += draw;
            bitmap = new Bitmap(Image.FromFile("0.jpg"));
            ClientSize = new Size(700,700);
        }
        Bitmap rotate(Bitmap source)
        {
            Bitmap bit = new Bitmap(source.Height, source.Width);
            for (int x = 0; x < bit.Width; x++)
            {
                for (int y = 0; y < bit.Height; y++)
                {
                    bit.SetPixel(x, y, source.GetPixel(bit.Height- y-1, x));
                }
            }
            return bit;
        }
        Bitmap bitmap;
        void draw(object o, EventArgs e)
        {
            CreateGraphics().Clear(Color.Black);
            bitmap = rotate(bitmap);
            CreateGraphics().DrawImage(bitmap, 0, 0);
        }
    }

4.鼠标单击拖动图片

using System;
using System.Windows.Forms;
using System.Drawing;
class window : Form
{
    Rectangle rec=new Rectangle(23,23,23,23);
    Bitmap b=new Bitmap("beauty.jpg");
    int x, y;
    int dx, dy;
    bool move;
    public window(){
        MouseClick +=click;
        MouseMove += OnMove;
        Paint += paint;
        Size = new Size(800,800);
        x = y = 0;
    }
    void OnMove(object o, MouseEventArgs e)
    {
        if (move)
        {
            Bitmap bit = new Bitmap(Width,Height);
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.AliceBlue);
            g.DrawImage(b, e.X+dx, e.Y+dy);
            for (int i = 10; i < bit.Width; i++)
                bit.SetPixel(i, 100, Color.Red);
            CreateGraphics().DrawImage(bit, 0, 0);
        }
    }
    void paint(object o, EventArgs e)
    {
        CreateGraphics().DrawImage(b,0,0);
    }
    void click(object o, MouseEventArgs e)
    {
        if (e.X > x && e.X < x + b.Width && e.Y > y && e.Y < y + b.Height && move == false) { move = true; dx = x - e.X; dy = y - e.Y; }
        else if (move == true)
        {
            x = e.X+dx; y = e.Y+dy;
            move = false;
        }
    }

    public static void Main()
    {
        Application.Run(new window());
    }
}

5.没有边框的窗口

    public partial class Form1 : Form
    {
        public Form1()
        {
            ClientSize = Screen.PrimaryScreen.Bounds.Size;
            FormBorderStyle = FormBorderStyle.None;
            Paint += draw;
            TransparencyKey = DefaultBackColor;
            ShowInTaskbar = false;
        }
        void draw(object o, EventArgs e)
        {
            CreateGraphics().Clear(DefaultBackColor);
            Bitmap bit=new Bitmap(ClientSize.Width,ClientSize.Height);
            bit.MakeTransparent(Color.White);
            Graphics.FromImage(bit).DrawString("东大微雕\n文韬武略\n天下第一", new Font(new FontFamily("Consolas"),200,GraphicsUnit.Pixel),new SolidBrush(Color.Red),new Rectangle(30,30,ClientRectangle.Right,ClientRectangle.Bottom));
            //BackgroundImage = bit;
            //释放上面这句就会导致闪烁
            CreateGraphics().DrawImage(bit,ClientRectangle);
        }
    }

6.全屏的窗口

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Size = MaximumSize;
            WindowState = FormWindowState.Maximized;
            FormBorderStyle = FormBorderStyle.None;
            Paint += paint;
            files= Directory.GetFiles(Directory.GetCurrentDirectory(),"*.jpg");
            KeyDown += down;
        }
        void down(object o, KeyEventArgs e)
        {
            index++;
            Invalidate();
        }
        string[] files;
        int index=0;
        void paint(object o, PaintEventArgs e)
        {
            Bitmap bit = new Bitmap(ClientSize.Width,ClientSize.Height);
            Graphics.FromImage(bit).DrawImage(Image.FromFile(files[index%files.Length]), ClientRectangle);
            e.Graphics.DrawImage(bit,0,0);
        }
    }

7.分工演示器

    public partial class Form1 : Form
    {
        SpeechSynthesizer teller = new SpeechSynthesizer();
        void tell(string s)
        {
            teller.Speak(s);
        }
        void crossLine(int from, int to)
        {
            int w = bit.Width / (size + 1);
            int h = bit.Height / (size + 1);
            Graphics g = Graphics.FromImage(bit);
            Pen p = new Pen(new SolidBrush(Color.Gold),4);
            g.DrawLine(p, work[from] * w + w + w / 2, from * h + h + h / 2, work[to] * w + w + w / 2, to * h + h + h / 2);
            g.DrawLine(p, work[to] * w + w + w / 2, from * h + h + h / 2, work[from] * w + w + w / 2, to * h + h + h / 2);
            CreateGraphics().DrawImage(bit,0,0);
        }
        void go()
        {
            int i, j;
            bool changed = true;
            while (changed)
            {
                tell("只要有变化,那就接着调整.调整哇调整,一直到没法再调整了");
                changed = false;
                for (i = 0; i < size; i++)
                {
                    for (j = size - 1; j >= 0; j--)
                    {
                        if (a[i, work[j]] + a[j, work[i]] < a[i, work[i]] + a[j, work[j]])
                        {
                            crossLine(i, j);
                            tell("工人" + man[i] + "干任务" + (char)(work[i] + 'A') + "需要" + a[i, work[i]] + "的时间");
                            tell("工人" + man[j] + "干任务" + (char)(work[j] + 'A') + "需要" + a[j, work[j]] + "的时间");
                            tell("他们交换一下任务,岂不是更快");
                            int temp = work[i];
                            work[i] = work[j];
                            work[j] = temp;
                            draw();
                            changed = true;
                        }
                    }
                }
            }
        }
        int[] work;
        public Form1()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            Text = "分工演示器-written for jiege,made by weidiao";
            var set = teller.GetInstalledVoices();
            teller.SelectVoice(set[0].VoiceInfo.Name);
            init();
            Paint += delegate
            {
                draw();
                go();
                tell("杰哥,你可以百度一下模拟淬火算法,这种方法适用于解决NP问题,但它的缺点就是最后所求的结果不一定对");
                tell("这种方法能够保证得到局部最优解,至于有多么接近最优解,这取决于你所选择的初始值");
                tell("于是,我们可以大量的用很多初始值进行迭代优化,从中选取最好的");
            };
            Resize += delegate
            {
                bit = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
                draw();
            };
        }
        Bitmap bit;
        char[] man = "甲乙丙丁戊己庚辛壬癸".ToCharArray();
        void draw()
        {
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.Black);
            int i, j;
            int w = bit.Width / (size + 1);
            int h = bit.Height / (size + 1);
            g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, w - 2, h - 2));
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), 0, 0, w, h);
            g.DrawString("任务", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.Crimson), new RectangleF(w / 2, 0, w / 2, h / 2));
            g.DrawString("人物", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.DarkOrange), new RectangleF(0, h / 2, w / 2, h / 2));
            for (i = 0; i < size; i++)
            {
                RectangleF rec = new RectangleF(w + w * i, 0, w - 2, h - 2);
                g.FillRectangle(new SolidBrush(Color.Gray), rec);
                g.DrawString((char)(i + 'A') + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Crimson), rec);
            }
            for (i = 0; i < size; i++)
            {
                RectangleF rec = new RectangleF(0, h + i * h, w - 2, h - 2);
                g.FillRectangle(new SolidBrush(Color.Gray), rec);
                g.DrawString(man[i] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.DarkOrange), rec);
            }
            for (i = 0; i < size; i++)
            {
                for (j = 0; j < size; j++)
                {
                    RectangleF rec = new RectangleF(j * w + w, h + i * h, w - 2, h - 2);
                    Color back = work[i] == j ? Color.Gainsboro : Color.Gray;
                    g.FillRectangle(new SolidBrush(back), rec);
                    g.DrawString(a[i, j] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Red), rec);
                }
            }
            CreateGraphics().DrawImage(bit, 0, 0);
        }
        int[,] a;
        Random r = new Random();
        int size;
        void init()
        {
            size = 4;
            a = new int[size, size];
            work = new int[size];
            int i, j;
            for (i = 0; i < size; i++) work[i] = i;
            for (i = 0; i < size; i++)
            {
                for (j = 0; j < size; j++)
                {
                    a[i, j] = r.Next() % 20 + 2;
                }
            }
        }
    }

C# Winform代码片段-大二下学期的垃圾代码的更多相关文章

  1. 此文记录了我从研二下学期到研三上学期的找工历程,包括百度、腾讯、网易、移动、电信、华为、中兴、IBM八家企业的面试总结和心得--转

    感谢电子通讯工程的研究生学长为大家整理了这么全面的求职总结,希望进入通信公司和互联网公司做非技术类岗位的学弟学妹们千万不要错过哦~ ---------------------------原文分割线-- ...

  2. PHP实用代码片段(二)

    1. 转换 URL:从字符串变成超链接 如果你正在开发论坛,博客或者是一个常规的表单提交,很多时候都要用户访问一个网站.使用这个函数,URL 字符串就可以自动的转换为超链接. function mak ...

  3. 直接拿来用 九个超实用的PHP代码片段(二)

    每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ...

  4. python超实用的30 个简短的代码片段(二)

    Python是目前最流行的语言之一,它在数据科学.机器学习.web开发.脚本编写.自动化方面被许多人广泛使用. 它的简单和易用性造就了它如此流行的原因. 如果你正在阅读本文,那么你或多或少已经使用过P ...

  5. 研二下学期做的第一个项目(主要关于datagridview的一些笔记)

    首先是行标题列rowheader dataGridView1.TopLeftHeaderCell.Value = "details"; ______________________ ...

  6. 大二上学期Javaweb阶段性学习总结

    本学期主要学了h5,css3,js,Java,SQL server数据库基本操作等相关知识,学会了简单web系统的制作. 这个学期总的来说学到了很多东西. 前期Java学习因为有了暑期学习及pta上5 ...

  7. Idea Live Template代码片段总结

    目录 Idea Live Template总结 一.演示 二.详细介绍 2.1 类型 2.2设置(win默认快捷键win+alt+s) 2.3 快捷键 2.4 实战 Idea Live Templat ...

  8. 微信小程序代码片段

    微信小程序代码片段是一种可分享的小项目,可用于分享小程序和小游戏的开发经验.展示组件和 API 的使用.复现开发问题等等.分享代码片段会得到一个链接,所有拥有此分享链接的人可以在工具中导入此代码片段. ...

  9. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

随机推荐

  1. java nio系列文章

    java nio系列教程 基于NIO的Client/Server程序实践 (推荐) java nio与并发编程相关电子书籍   (访问密码 48dd) 理解NIO nio学习记录 图解ByteBuff ...

  2. Search and Replace搜寻与替换工具

    一个功能强大的搜寻与替代工具.它可以在同一部硬盘中对所有的文件进行搜寻与替换的功能,也可以对Zip文件中的文件做搜寻,支持特殊字符条件表达式搜寻,或是以脚本文件(Script)做搜寻替换工作,也可以以 ...

  3. list to csv

    import csv # ============================== # list to csv # ============================== a = [1,2, ...

  4. IOS实用功能之截图(来自相册和拍照)

    // //  ViewController.m //  MyImagePicker1.0 // //  Created by Mac on 14-7-14. //  Copyright (c) 201 ...

  5. jsp页面格式化数字或时间

    Tags   fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:setTimeZone fmt:bundle fmt:setBundle fmt:m ...

  6. pandas数据结构练习题(部分)

    更多函数查阅http://pandas.pydata.org/pandas-docs/stable/10min.htmlimport pandas as pd#两种数据结构from pandas im ...

  7. 武侠--生活--java

    一.名词解释 1.向上转型 大白话:村支书通知你爸去大队领过年发的面粉,结果你爸不在家,你装成你爸去了,村支书一看,行,你具有你爸的所有功能,就给了你. 官方解释:子类引用的对象转换为父类类型称为向上 ...

  8. Java面试19&vert;过于深入的问题

    1.synchronized关键字的实现原理 可以参考:http://www.jianshu.com/p/c5058b6fe8e5 2.CAS是由Unsafe类的compareAndSwap()方法实 ...

  9. scrapy 登陆知乎

    参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time import r ...

  10. EOS 权限

    [EOS权限] 1.查看权限 cleos get account $(Account_Name) 2.使用 cleos set account permission 命令来修改权限 可以看到,owne ...