搞定!!通过PictureBox播放视频,为实现诺言,加贴,给出本人的代码与大家交流,并给正解者zoujiaming两百分!!!

szxbluestar 2003-10-15 08:39:21
搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!
使用的是mciSendString API函数
主要参考了zoujiaming 给我的邮件(再次感谢啊!!)
以及http://www.csdn.net/Develop/Read_Article.asp?Id=16269
和http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mcisendstring.asp

下面是我的一个简单实验代码,通过双击PictureBox播放我机子上的一个视频文件。
有什么问题可以跟贴:)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace APImciTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public class LibWrap
{
[DllImport(("winmm.dll"), EntryPoint="mciSendString", CharSet=CharSet.Auto )]
public static extern int mciSendString
( string lpszCommand, string lpszReturnString, uint cchReturn, int hwndCallback);
} //此处为API函数声明部分
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlText;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(292, 273);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void pictureBox1_DoubleClick(object sender, System.EventArgs e) //此处为双击播放应用部分,mciCommand中注意空格
{
PictureBox PlayScreen = new PictureBox();
PlayScreen = this.pictureBox1;
string mciCommand;
mciCommand = "open " + "H:\\12.asf" + " alias MyAVI";
mciCommand = mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child";
LibWrap.mciSendString(mciCommand, null, 0,0);
Rectangle r = PlayScreen.ClientRectangle;
mciCommand = "put MyAVI window at 0 0 "+r.Width +" "+r.Height ;
LibWrap.mciSendString( mciCommand, null, 0, 0);
LibWrap.mciSendString ("play MyAVI", null, 0, 0);

}
}
}

谢谢各位支持啊:)
...全文
670 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiayadong 2003-10-20
  • 打赏
  • 举报
回复
我要捕获我的摄像头又要怎么做呢如果是播放视频文件可以调用Windows Media Player的呀
xfwshaver 2003-10-20
  • 打赏
  • 举报
回复
请问那位老大做过相册的啊???
xfwshaver 2003-10-20
  • 打赏
  • 举报
回复
好!!!
szxbluestar 2003-10-17
  • 打赏
  • 举报
回复
将函数第二个参数的类型改成StringBuilder (通过System.text引用) 是因为在返回多媒体设备状态的时候,返回值是要指定长度的,必需和mci第三个参数相一致才可。给个例子给大家:
string mciCommand;
StringBuilder result = new StringBuilder(64);//此处指定string长度为64
mciCommand = "status MyAVI length";
LibWrap.mciSendString(mciCommand,result,64,0);//与此处第三个参数64相配合
MessageBox.Show(result.ToString());
只有这样程序才能正常运行得到result,否则会造成程序停止响应的状态
szxbluestar 2003-10-17
  • 打赏
  • 举报
回复
不好意思啊各位,本贴存在一个自己未注意的BUG
今天调试有反回值的mci程序发现了问题,原API引用声明存在问题,现改正如下:
[DllImport(("winmm.dll"), EntryPoint="mciSendString", CharSet=CharSet.Auto )]
public static extern int mciSendString
( string lpszCommand,
StringBuilder lpszReturnString,
uint cchReturn, int hwndCallback);
szxbluestar 2003-10-16
  • 打赏
  • 举报
回复
呵呵,这个创意不变态啊,很实用的啊
大家再去多看看MSDN上关于mciSendString
的字符串命令的帮助,一定可以做出一个符合自己个性和使用习惯的的媒体播放器啊
feigehao 2003-10-16
  • 打赏
  • 举报
回复
zhichi .xuexi
slag 2003-10-16
  • 打赏
  • 举报
回复
好!UP
tfming 2003-10-16
  • 打赏
  • 举报
回复
up
CSTerry 2003-10-16
  • 打赏
  • 举报
回复
厉害……呵呵
这么变态的需求是谁提出来的?
duibudui 2003-10-16
  • 打赏
  • 举报
回复
很好的创意啊
by92419 2003-10-16
  • 打赏
  • 举报
回复
h
LA003 2003-10-16
  • 打赏
  • 举报
回复
感谢!学习,收藏
zag 2003-10-16
  • 打赏
  • 举报
回复
学习。
sunyou 2003-10-16
  • 打赏
  • 举报
回复
学习
acewang 2003-10-16
  • 打赏
  • 举报
回复
up
zoujiaming 2003-10-16
  • 打赏
  • 举报
回复
呵呵,不错呀,共同学习,一起进步。
yoobj 2003-10-16
  • 打赏
  • 举报
回复
收藏
sharplee82 2003-10-16
  • 打赏
  • 举报
回复
我没有想到还有这样的绝招。
维她奶 2003-10-16
  • 打赏
  • 举报
回复
学习&收藏!
加载更多回复(8)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧