关于C# 开发Winfrom事后总结

avatar
作者
筋斗云
阅读量:4

一:要求能读取文件夹中视频及图片并判断

 private void Form1_Load(object sender, EventArgs e)  {   string foldPath = "路径";//获取该目录下的文件                         string[] files = Directory.GetFiles(@foldPath + "\\", "*.*",    System.IO.SearchOption.TopDirectoryOnly);             var th = new Thread(delegate ()             {                 //遍历每个文件                 for (int i = 0; i < files.Length; i++)                 {                     //判断当前组文件中第i个文件                     if (IsPicture(files[i]))  //图片                     {                         Bitmap picture = new Bitmap(files[i]);                         ChangeBringToFrontImage(picture);                     }                       if (IsVideo(files[i]))    //视频                     {                         ChangeBringToFrontVideo(files[i]);                      }                       if (i == files.Length - 1)                     {                         //从0开始重新播放                         i = 0;                     }                 }             }); //开辟新线程 //th.IsBackground = true; //th.Start();  } //定义判断是否为图片 public bool IsPicture(string filePath) {      var extension = Path.GetExtension(filePath).ToLowerInvariant();     return extension == ".jpg" ||            extension == ".jpeg" ||            extension == ".png" ||            extension == ".gif" ||            extension == ".bmp" ||            extension == ".tiff" ||            extension == ".ico";   }  //定义判断是否为视频 public bool IsVideo(string filePath) {     var extension = Path.GetExtension(filePath).ToLowerInvariant();     return extension == ".wmv" ||            extension == ".asf" ||            extension == ".asx" ||            extension == ".avi" ||            extension == ".mp4" ||            extension == ".mov" ||            extension == ".mkv"; }

二:视频跟图片要等比例放大缩小

pictureBox1.Dock = DockStyle.Fill; axWindowsMediaPlayer1.Dock = DockStyle.Fill;

三:更换窗体运行图标

// 移除窗体边框 this.FormBorderStyle = FormBorderStyle.None;  // 允许用户调整窗体大小 this.SizeGripStyle = SizeGripStyle.Show;  //设置窗体的最大大小和最小大小 this.MaximumSize = new Size(1920, 1080); this.MinimumSize = new Size(400, 300);   //设置窗体的开始位置(CenterScreen将窗体居中) this.StartPosition = FormStartPosition.CenterScreen;  // 设置窗体的举头文本 this.Text = "Trs宣传播报";  // 设置窗体的背景色 //this.BackColor = Color.Lime;  // 设置窗体50%的透明度 //this.Opacity = 0.5;  //更改窗体图标 this.Icon = Resources.Trs;

四:要求可配置文件路径及更换时间

//config    <appSettings>     <add key="Time" value="4000" /><!--循环的时间-->     <add key="FromPath" value="C:\Users\N01465\Desktop\1" /><!--播放路径-->   </appSettings>  //From1_cs  // 强制重新加载app.config  ConfigurationManager.RefreshSection("appSettings");   // 读取所有设置  Time     = ConfigurationManager.AppSettings["Time"];  FromPath = ConfigurationManager.AppSettings["FromPath"];

五:运行渲染的过程中不运行卡顿

//如一 开启新线程不使用主线程去渲染 private void ChangeBringToFrontImage(Image img) {     if (pictureBox1.InvokeRequired)     {         // 使用Invoke安全地从其他线程调用         pictureBox1.Invoke(new Action(() =>         {             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;             axWindowsMediaPlayer1.Visible = false;             pictureBox1.Visible = true;             pictureBox1.Image = img;             Delay(Convert.ToInt32(Time));         }));     }     else     {         // 如果已经在创建控件的线程上,直接设置值         pictureBox1.Visible = true;         axWindowsMediaPlayer1.Visible = false;         pictureBox1.Image = img;         Thread.Sleep(Convert.ToInt32(Time));     } }  private void ChangeBringToFrontVideo(string filePath) {     if (axWindowsMediaPlayer1.InvokeRequired)     {         // 使用Invoke安全地从其他线程调用         axWindowsMediaPlayer1.Invoke(new Action(() =>         {             axWindowsMediaPlayer1.Visible = true;             pictureBox1.Visible = false;              #region 解决报错的方式你可以先不加这个你试试会不会报错             ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();//axWindowsMediaPlayer2为第三方控件名             axWindowsMediaPlayer1.uiMode = "None"; //隐藏视频播放器布局状态栏             this.Controls.Add(axWindowsMediaPlayer1);             ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();             #endregion             this.axWindowsMediaPlayer1.URL = filePath;              //1 的时候是关闭,结束了             try             {                 while (!((int)axWindowsMediaPlayer1.playState == 1))                 {                     Delay(500);                 }             }             catch             {              }          }));     }     else     {         axWindowsMediaPlayer1.Visible = true;         pictureBox1.Visible = false;          #region 解决报错的方式你可以先不加这个你试试会不会报错         ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();//axWindowsMediaPlayer2为第三方控件名         axWindowsMediaPlayer1.uiMode = "None"; //隐藏视频播放器布局状态栏         this.Controls.Add(axWindowsMediaPlayer1);         ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();         #endregion         this.axWindowsMediaPlayer1.URL = filePath;         //1 的时候是关闭,结束了         while (!((int)axWindowsMediaPlayer1.playState == 1))         {             Delay(500);         }      } }

六:隐藏标题栏后需要可拖拽缩放

可通过游标去实现我这边就不粘出来了

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!