A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-21 15:02  /  778 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;

  10. namespace MusicPlayer
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Bitmap originImg;
  15.         Image finishImg;
  16.         Graphics g;
  17.         Point StartPoint, EndPoint;
  18.         Pen p = new Pen(Color.Black, 2);
  19.         bool IsDraw;
  20.         bool IsPen;
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  25.             this.UpdateStyles();  
  26.             p.StartCap = LineCap.Round;
  27.             p.EndCap = LineCap.Round;//将线帽样式设为圆线帽
  28.             originImg = new Bitmap(picDraw.Width, picDraw.Height);
  29.             g = Graphics.FromImage(originImg);
  30.             g.Clear(Color.White); //画布背景初始化为白底
  31.             picDraw.Image = originImg;
  32.             finishImg = (Image)originImg.Clone();
  33.         }

  34.         private void button1_Click(object sender, EventArgs e)
  35.         {
  36.             DialogResult pathok;
  37.             pathok = folderBrowserDialog1.ShowDialog();    //显示选择目录对话框
  38.             if (pathok == DialogResult.OK)     //如果单击OK 则获取目录内
  39.             {
  40.                 string[] allfile = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath);   //获取目录内所有文件
  41.                 foreach (string file in allfile)
  42.                 {
  43.                     if (System.IO.Path.GetExtension(file) == ".mp3" || System.IO.Path.GetExtension(file) == ".wma")
  44.                     {
  45.                         listBox1.Items.Add(System.IO.Path.GetFileName(file));    //在listBox1里显示文件
  46.      
  47.                     }
  48.                 }

  49.             }
  50.             pause.Enabled = true;
  51.             previous.Enabled = true;
  52.             next.Enabled = true;
  53.             delete.Enabled = true;
  54.             trackBar1.Value = axWindowsMediaPlayer1.settings.volume;
  55.         }

  56.         private void listBox1_DoubleClick(object sender, EventArgs e)
  57.         {
  58.                 axWindowsMediaPlayer1.URL = folderBrowserDialog1.SelectedPath + @"/" + listBox1.Items[listBox1.SelectedIndex].ToString();
  59.                 timer1.Enabled = true;
  60.         }

  61.         private void play_Click(object sender, EventArgs e)
  62.         {
  63.             axWindowsMediaPlayer1.Ctlcontrols.play();
  64.         }

  65.         private void pause_Click(object sender, EventArgs e)
  66.         {
  67.             axWindowsMediaPlayer1.Ctlcontrols.pause();
  68.             play.Enabled = true;
  69.         }

  70.         private void previous_Click(object sender, EventArgs e)
  71.         {
  72.                 --listBox1.SelectedIndex;
  73.                 axWindowsMediaPlayer1.URL = folderBrowserDialog1.SelectedPath + @"/" + listBox1.Items[listBox1.SelectedIndex].ToString();
  74.         }

  75.         private void next_Click(object sender, EventArgs e)
  76.         {
  77.                 ++listBox1.SelectedIndex;
  78.                 axWindowsMediaPlayer1.URL = folderBrowserDialog1.SelectedPath + @"/" + listBox1.Items[listBox1.SelectedIndex].ToString();
  79.         }

  80.         private void delete_Click(object sender, EventArgs e)
  81.         {
  82.             if (listBox1.Text != null)
  83.             { listBox1.Items.Remove(listBox1.Text); }
  84.             ++listBox1.SelectedIndex;
  85.             axWindowsMediaPlayer1.URL = folderBrowserDialog1.SelectedPath + @"/" + listBox1.Items[listBox1.SelectedIndex].ToString();
  86.         }

  87.         private void trackBar1_Scroll(object sender, EventArgs e)
  88.         {
  89.             axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
  90.         }

  91.         private void timer1_Tick(object sender, EventArgs e)
  92.         {
  93.             if (axWindowsMediaPlayer1.playState == (WMPLib.WMPPlayState)1)
  94.             {
  95.                 ++listBox1.SelectedIndex;
  96.                 axWindowsMediaPlayer1.URL = folderBrowserDialog1.SelectedPath + @"/" + listBox1.Items[listBox1.SelectedIndex].ToString();
  97.             }
  98.      
  99.         }

  100.         private void mypen_Click(object sender, EventArgs e)
  101.         {
  102.             IsPen = true;
  103.         }

  104.         private void myeraser_Click(object sender, EventArgs e)
  105.         {
  106.             IsPen = false;
  107.         }

  108.         private void picDraw_MouseDown(object sender, MouseEventArgs e)
  109.         {
  110.             if (e.Button == MouseButtons.Left)
  111.             {
  112.                 IsDraw = true;
  113.                 StartPoint = e.Location;
  114.                 if (!IsPen)
  115.                     finishImg = (Image)originImg.Clone();
  116.             }
  117.         }

  118.         private void picDraw_MouseMove(object sender, MouseEventArgs e)
  119.         {
  120.             if (IsDraw)
  121.             {
  122.                 g = Graphics.FromImage(finishImg);
  123.                 g.SmoothingMode = SmoothingMode.AntiAlias;
  124.                 EndPoint = e.Location;
  125.                 if (IsPen)
  126.                 {
  127.                     g.DrawLine(p, StartPoint, EndPoint);
  128.                     StartPoint = EndPoint;
  129.                 }
  130.                 else
  131.                 {
  132.                     Pen pen1 = new Pen(Color.White, 20);
  133.                     pen1.StartCap = LineCap.Round;
  134.                     pen1.EndCap = LineCap.Round;  
  135.                     g.DrawLine(pen1, StartPoint, EndPoint);
  136.                     StartPoint = EndPoint;
  137.                     pen1.Dispose();
  138.                 }
  139.                 reDraw();
  140.             }
  141.         }

  142.         private void picDraw_MouseUp(object sender, MouseEventArgs e)
  143.         {
  144.             IsDraw = false;
  145.             originImg = (Bitmap)finishImg;
  146.             picDraw.Image = originImg;
  147.         }
  148.         private void reDraw()
  149.         {
  150.             Graphics graphics = picDraw.CreateGraphics();
  151.             graphics.DrawImage(finishImg, new Point(0, 0));
  152.             graphics.Dispose();
  153.         }

  154.         private void savepic_Click(object sender, EventArgs e)
  155.         {
  156.             SaveFileDialog sfd = new SaveFileDialog();
  157.             sfd.Filter = "jpg|*.jpg|png|*.png|bmp|*.bmp";
  158.             if (sfd.ShowDialog() == DialogResult.OK)
  159.             {
  160.                 finishImg.Save(sfd.FileName);
  161.             }
  162.         }

  163.         private void clearpic_Click(object sender, EventArgs e)
  164.         {
  165.             g.Clear(Color.White);
  166.             reDraw();
  167.         }  
  168.     }
  169. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马