1

2

3
//2005-01-214
//http://blog.csdn.net/apcsherry/archive/2006/01/04/570202.aspx5
//使用DirectX技术实现播放,需要引用系统的 C:\WINNT\system32\quartz.dll 6
//是从CodeProject上看来的。前一阵看到有人需要这样的代码,所以贴出来共享一下。 7

8
using System; 9
using System.Collections; 10
using System.ComponentModel; 11
using System.Drawing; 12
using System.Data; 13
using System.Windows.Forms; 14
using QuartzTypeLib; 15
using EnvDTE;16
namespace HHOA 17


{ 18

/**//// 〈summary〉 19
/// FrmTV 的摘要说明。 2005-01-21 tommy test20
/// 〈/summary〉 21
public class FrmTV : System.Windows.Forms.UserControl 22

{ 23
private System.Windows.Forms.ToolBar toolBar1; 24
private System.Windows.Forms.ToolBarButton toolBarButton1; 25
private System.Windows.Forms.ToolBarButton toolBarButton2; 26
private System.Windows.Forms.ToolBarButton toolBarButton3; 27
private System.Windows.Forms.Panel panel1; 28
private System.Windows.Forms.ImageList imageList1; 29
private System.Windows.Forms.Timer timer1; 30
private System.Windows.Forms.ToolBarButton toolBarButton4; 31
private System.ComponentModel.IContainer components; 32

33
34

35
private const int WM_APP = 0x8000; 36
private const int WM_GRAPHNOTIFY = WM_APP + 1; 37
private const int EC_COMPLETE = 0x01; 38
private const int WS_CHILD = 0x40000000; 39
private const int WS_CLIPCHILDREN = 0x2000000; 40

41
private FilgraphManager m_objFilterGraph = null; 42
private IBasicAudio m_objBasicAudio = null; 43
private IVideoWindow m_objVideoWindow = null; 44
private IMediaEvent m_objMediaEvent = null; 45
private IMediaEventEx m_objMediaEventEx = null; 46
private IMediaPosition m_objMediaPosition = null; 47
private IMediaControl m_objMediaControl = null; 48
private System.Windows.Forms.StatusBar statusBar1; 49
private System.Windows.Forms.StatusBarPanel statusBarPanel1; 50
private System.Windows.Forms.StatusBarPanel statusBarPanel2; 51
private System.Windows.Forms.StatusBarPanel statusBarPanel3; 52

53

enum MediaStatus
{ None, Stopped, Paused, Running }; 54

55
private MediaStatus m_CurrentStatus = MediaStatus.None; 56

57
public FrmTV() 58

{ 59
// 该调用是 Windows.Forms 窗体设计器所必需的。 60
InitializeComponent(); 61
// TODO: 在 InitializeComponent 调用后添加任何初始化 62

63
} 64
public FrmTV(EnvDTE._DTE dte,string workpath) 65

{ 66
InitializeComponent(); 67
this.mDTE=dte; 68
this.mWorkPath=workpath; 69
} 70
private _DTE mDTE=null; 71
public _DTE DTE 72

{ 73
set 74

{ 75
mDTE=value; 76
} 77

78
get 79

{ 80
return mDTE; 81
} 82
} 83

84
//工作目录 85
private string mWorkPath=null; 86
public string WorkPath 87

{ 88
set 89

{ 90
this.mWorkPath=value; 91
} 92
} 93

/**//// 〈summary〉 94
/// 清理所有正在使用的资源。 95
/// 〈/summary〉 96
protected override void Dispose( bool disposing ) 97

{ 98
if( disposing ) 99

{ 100
if(components != null) 101

{ 102
components.Dispose(); 103
} 104
} 105
base.Dispose( disposing );106
} 107

108

组件设计器生成的代码#region 组件设计器生成的代码 109

/**//// 〈summary〉 110
/// 设计器支持所需的方法 - 不要使用代码编辑器 111
/// 修改此方法的内容。 112
/// 〈/summary〉 113
private void InitializeComponent() 114

{ 115
this.components = new System.ComponentModel.Container();116
this.toolBar1 = new System.Windows.Forms.ToolBar();117
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();118
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();119
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();120
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();121
this.imageList1 = new System.Windows.Forms.ImageList(this.components);122
this.panel1 = new System.Windows.Forms.Panel();123
this.timer1 = new System.Windows.Forms.Timer(this.components);124
this.statusBar1 = new System.Windows.Forms.StatusBar();125
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();126
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();127
this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel();128
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();129
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();130
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit();131
this.SuspendLayout();132
// 133
// toolBar1134
// 135
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;136

this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[]
{137
this.toolBarButton4,138
this.toolBarButton1,139
this.toolBarButton2,140
this.toolBarButton3});141
this.toolBar1.ButtonSize = new System.Drawing.Size(37, 28);142
this.toolBar1.DropDownArrows = true;143
this.toolBar1.ImageList = this.imageList1;144
this.toolBar1.Location = new System.Drawing.Point(0, 0);145
this.toolBar1.Name = "toolBar1";146
this.toolBar1.ShowToolTips = true;147
this.toolBar1.Size = new System.Drawing.Size(288, 41);148
this.toolBar1.TabIndex = 0;149
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);150
// 151
// toolBarButton4152
// 153
this.toolBarButton4.Text = "Open";154
// 155
// toolBarButton1156
// 157
this.toolBarButton1.Enabled = false;158
this.toolBarButton1.Text = "Play";159
// 160
// toolBarButton2161
// 162
this.toolBarButton2.Enabled = false;163
this.toolBarButton2.Text = "Pause";164
// 165
// toolBarButton3166
// 167
this.toolBarButton3.Enabled = false;168
this.toolBarButton3.Text = "Stop";169
// 170
// imageList1171
// 172
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;173
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);174
this.imageList1.TransparentColor = System.Drawing.Color.Red;175
// 176
// panel1177
// 178
this.panel1.BackColor = System.Drawing.Color.Gainsboro;179
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;180
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;181
this.panel1.Location = new System.Drawing.Point(0, 41);182
this.panel1.Name = "panel1";183
this.panel1.Size = new System.Drawing.Size(288, 239);184
this.panel1.TabIndex = 4;185
// 186
// timer1187
// 188
this.timer1.Enabled = true;189
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);190
// 191
// statusBar1192
// 193
this.statusBar1.Location = new System.Drawing.Point(0, 260);194
this.statusBar1.Name = "statusBar1";195

this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[]
{196
this.statusBarPanel1,197
this.statusBarPanel2,198
this.statusBarPanel3});199
this.statusBar1.ShowPanels = true;200
this.statusBar1.Size = new System.Drawing.Size(288, 20);201
this.statusBar1.TabIndex = 5;202
// 203
// statusBarPanel1204
// 205
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;206
this.statusBarPanel1.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;207
this.statusBarPanel1.Text = "准备";208
this.statusBarPanel1.Width = 144;209
// 210
// statusBarPanel2211
// 212
this.statusBarPanel2.Alignment = System.Windows.Forms.HorizontalAlignment.Center;213
this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;214
this.statusBarPanel2.Text = "00:00:00";215
this.statusBarPanel2.Width = 64;216
// 217
// statusBarPanel3218
// 219
this.statusBarPanel3.Alignment = System.Windows.Forms.HorizontalAlignment.Center;220
this.statusBarPanel3.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;221
this.statusBarPanel3.Text = "00:00:00";222
this.statusBarPanel3.Width = 64;223
// 224
// FrmTV225
// 226
this.BackColor = System.Drawing.SystemColors.Control;227
this.Controls.Add(this.statusBar1);228
this.Controls.Add(this.panel1);229
this.Controls.Add(this.toolBar1);230
this.Name = "FrmTV";231
this.Size = new System.Drawing.Size(288, 280);232
this.SizeChanged += new System.EventHandler(this.FrmTV_SizeChanged);233
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();234
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();235
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).EndInit();236
this.ResumeLayout(false);237

238
} 239
#endregion 240

241
private void CleanUp() 242

{ 243
if (m_objMediaControl != null) 244
m_objMediaControl.Stop(); 245

246
m_CurrentStatus = MediaStatus.Stopped; 247

248
if (m_objMediaEventEx != null) 249
m_objMediaEventEx.SetNotifyWindow(0, 0, 0); 250

251
if (m_objVideoWindow != null) 252

{ 253
254
m_objVideoWindow.Visible = 0; 255
m_objVideoWindow.Owner = 0; 256
} 257

258
if (m_objMediaControl != null) m_objMediaControl = null; 259
if (m_objMediaPosition != null) m_objMediaPosition = null; 260
if (m_objMediaEventEx != null) m_objMediaEventEx = null; 261
if (m_objMediaEvent != null) m_objMediaEvent = null; 262
if (m_objVideoWindow != null) m_objVideoWindow = null; 263
if (m_objBasicAudio != null) m_objBasicAudio = null; 264
if (m_objFilterGraph != null) m_objFilterGraph = null; 265
} 266

267
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) 268

{ 269
switch(toolBar1.Buttons.IndexOf(e.Button)) 270

{ 271
case 0: 272

{ 273

打开文件#region 打开文件 274
OpenFileDialog openFileDialog = new OpenFileDialog();275
openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*"; 276
if (DialogResult.OK == openFileDialog.ShowDialog()) 277

{278
CleanUp();279
m_objFilterGraph = new FilgraphManager();280
m_objFilterGraph.RenderFile(openFileDialog.FileName);281
m_objBasicAudio = m_objFilterGraph as IBasicAudio; 282
try 283

{ 284
m_objVideoWindow = m_objFilterGraph as IVideoWindow; 285
m_objVideoWindow.Owner = (int) panel1.Handle;286
m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN; 287
m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left, 288
panel1.ClientRectangle.Top, panel1.ClientRectangle.Width,panel1.ClientRectangle.Height); 289
} 290
catch (Exception) 291

{ 292
m_objVideoWindow = null; 293
} 294
m_objMediaEvent = m_objFilterGraph as IMediaEvent; 295
m_objMediaEventEx = m_objFilterGraph as IMediaEventEx; 296
m_objMediaEventEx.SetNotifyWindow((int) this.Handle,WM_GRAPHNOTIFY, 0); 297
m_objMediaPosition = m_objFilterGraph as IMediaPosition;298
m_objMediaControl = m_objFilterGraph as IMediaControl; 299
this.Text = "DirectShow - [" + openFileDialog.FileName + "]"; 300
m_objMediaControl.Run(); 301
m_CurrentStatus = MediaStatus.Running;302
} 303
break; 304
#endregion 305

306
} 307
case 1: 308

{ 309
m_objMediaControl.Run(); 310
m_CurrentStatus = MediaStatus.Running;311
break; 312
} 313

314
case 2: 315

{ 316
m_objMediaControl.Pause(); 317
m_CurrentStatus = MediaStatus.Paused; 318
break; 319
} 320

321
case 3: 322

{ 323
m_objMediaControl.Stop(); 324
m_objMediaPosition.CurrentPosition = 0; 325
m_CurrentStatus = MediaStatus.Stopped; 326
break; 327
} 328
} 329

330
UpdateStatusBar(); 331
UpdateToolBar(); 332
} 333

334
private void FrmTV_SizeChanged(object sender, System.EventArgs e) 335

{ 336
if (m_objVideoWindow != null) 337

{ 338
m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left, 339
panel1.ClientRectangle.Top, 340
panel1.ClientRectangle.Width, 341
panel1.ClientRectangle.Height); 342
} 343
} 344

345
private void timer1_Tick(object sender, System.EventArgs e) 346

{ 347

348
if (m_CurrentStatus == MediaStatus.Running) 349

{ 350
UpdateStatusBar(); 351
} 352
} 353
protected override void WndProc( ref Message m) 354

{ 355
if (m.Msg == WM_GRAPHNOTIFY) 356

{ 357
int lEventCode; 358
int lParam1, lParam2; 359
while (true) 360

{ 361
try 362

{ 363
m_objMediaEventEx.GetEvent(out lEventCode,out lParam1, out lParam2, 0); 364
m_objMediaEventEx.FreeEventParams(lEventCode, lParam1, lParam2); 365
if (lEventCode == EC_COMPLETE) 366

{ 367
m_objMediaControl.Stop(); 368
m_objMediaPosition.CurrentPosition = 0;369
m_CurrentStatus = MediaStatus.Stopped; 370
UpdateStatusBar(); 371
UpdateToolBar(); 372
} 373
} 374
catch (Exception) 375

{ 376
break; 377
} 378
} 379
} 380
base.WndProc(ref m); 381
} 382

383
384
private void UpdateStatusBar() 385

{ 386
switch (m_CurrentStatus) 387

{ 388
case MediaStatus.None : statusBarPanel1.Text = "停止"; break; 389
case MediaStatus.Paused : statusBarPanel1.Text = "暂停 "; break; 390
case MediaStatus.Running: statusBarPanel1.Text = "播放"; break; 391
case MediaStatus.Stopped: statusBarPanel1.Text = "停止"; break; 392
} 393

394
if (m_objMediaPosition != null) 395

{ 396
int s = (int) m_objMediaPosition.Duration; 397
int h = s / 3600; 398
int m = (s - (h * 3600)) / 60; 399
s = s - (h * 3600 + m * 60); 400

401
statusBarPanel2.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s); 402

403
s = (int) m_objMediaPosition.CurrentPosition; 404
h = s / 3600; 405
m = (s - (h * 3600)) / 60; 406
s = s - (h * 3600 + m * 60); 407

408
statusBarPanel3.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s); 409
} 410
else 411

{ 412
statusBarPanel2.Text = "00:00:00"; 413
statusBarPanel3.Text = "00:00:00"; 414
} 415
} 416

417
private void UpdateToolBar() 418

{ 419
switch (m_CurrentStatus) 420

{ 421
case MediaStatus.None : toolBarButton1.Enabled = false; 422
toolBarButton2.Enabled = false; 423
toolBarButton3.Enabled = false; 424
break; 425

426
case MediaStatus.Paused : toolBarButton1.Enabled = true; 427
toolBarButton2.Enabled = false; 428
toolBarButton3.Enabled = true; 429
break; 430

431
case MediaStatus.Running: toolBarButton1.Enabled = false; 432
toolBarButton2.Enabled = true; 433
toolBarButton3.Enabled = true; 434
break; 435

436
case MediaStatus.Stopped: toolBarButton1.Enabled = true; 437
toolBarButton2.Enabled = false; 438
toolBarButton3.Enabled = false; 439
break; 440
} 441
} 442
} 443
}
浙公网安备 33010602011771号