使用COM控件
1,使用Windows Media Player播放视频。
效果
如果VS工具箱中没有这个控件,那么右击工具箱,添加选项卡,命名为COM,然后再COM选项栏,右击选择“选择项”,打开的对话框中选择“COM组件”,找到Windows Media Player,勾选之后,点击确定就添加到工具箱了。 然后直接从工具箱拉到窗体上就可以使用了。
如下图
下图窗体中,放了ToolStrip,ToolStrip选择了一个Button命名为“浏览”,一个TextBox,显示选择的文件完全名。然后还放了Windows Media Player。
代码如下:
//======================================================================
// 《31天学会CRM项目开发》机械工业出版社 版权所有 (C) 2015-2016
// 文件名:Form1.cs
// 随书附赠源代码,若转发需保留版权信息。
// 社区支持:http://www.huiyaosoft.com/
//======================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MediaPlayerDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string appPath = System.Windows.Forms.Application.StartupPath + "\\";
//设置视频链接,可以是网络,也可以是本地
//网络视频链接
this.axWindowsMediaPlayer1.URL = @"http://2449.vod.myqcloud.com/2449_43b6f696980311e59ed467f22794e792.f20.mp4";
//本地视频链接
//this.axWindowsMediaPlayer1.URL = appPath + "";
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
//设置对话框标题
dlg.Title = "选择媒体文件";
//按照扩展名过滤文件
dlg.Filter = "所有文件|*.*|媒体文件|*.mp4*;*.avi;*.wmv";
//是否支持多选
dlg.Multiselect = false;
//弹出对话框判断用户是否选择了文件
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
//重新选定播放文件路径
this.axWindowsMediaPlayer1.URL = this.tstbMediaFile.Text = dlg.FileName;
}
}
}
2,使用Adobe PDF Reader阅读PDF文件
添加这个组件步骤参照上面的。
下图,放了Adobe PDF Reader,和ToolStrip。
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AdobePDFReader2019
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string appPath = Application.StartupPath + "\\";
//使用webBrowser1阅读PDF
//this.webBrowser1.Navigate(appPath + "31天学会CRM项目开发.pdf");
axAcroPDF1.LoadFile(appPath + "31天学会CRM项目开发.pdf");
#region 不显示工具栏
//axAcroPDF1.setPageMode("thumbs");
axAcroPDF1.setPageMode("none");
#endregion
}
/// <summary>
/// 选择PDF文档路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
//设置对话框标题
dlg.Title = "选择pdf文件";
//按照扩展名过滤文件
dlg.Filter = "所有文件|*.*|PDF文件|*.pdf*";
//是否支持多选
dlg.Multiselect = false;
//弹出对话框判断用户是否选择了文件
if (dlg.ShowDialog() == DialogResult.OK)
{
tstbPdfDir.Text = dlg.FileName;
axAcroPDF1.LoadFile(dlg.FileName);
#region 不显示工具栏
//axAcroPDF1.setPageMode("thumbs");
axAcroPDF1.setPageMode("none");
#endregion
}
}
}