MainForm.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using VideoPlayLib;
  7. namespace Demo
  8. {
  9. public partial class MainForm: Form
  10. {
  11. private VideoPlay _vp;
  12. private Form _big;
  13. public MainForm()
  14. {
  15. InitializeComponent();
  16. }
  17. #region 自定义消息
  18. protected override void WndProc(ref Message m)
  19. {
  20. MsgPostProc(m);
  21. base.WndProc(ref m);
  22. }
  23. private const int WM_USER = 0x0400;
  24. private const int MSG_PLAY = WM_USER + 101;
  25. [DllImport("User32.dll")]
  26. private static extern int PostMessage(
  27. IntPtr hWnd, // handle to destination window
  28. uint msg, // message
  29. uint wParam, // first message parameter
  30. uint lParam // second message parameter
  31. );
  32. private string MsgPostUrl;
  33. private void MsgPostPlay(string url)
  34. {
  35. MsgPostUrl = url;
  36. new Thread(
  37. () => Invoke(
  38. new Action(
  39. () =>
  40. PostMessage(Handle, MSG_PLAY, 0, 0)
  41. )
  42. )
  43. ).Start();
  44. }
  45. private void MsgPostProc(Message p)
  46. {
  47. if (p.Msg != MSG_PLAY)
  48. return;
  49. LoadMediaFile();
  50. MsgPostUrl = null;
  51. }
  52. #endregion 自定义消息
  53. private void MainForm_Shown(object sender, EventArgs e)
  54. {
  55. _big = new Form();
  56. _big.Show();
  57. _big.Location = Screen.AllScreens[0].Bounds.Location;
  58. _big.WindowState = FormWindowState.Maximized;
  59. _big.FormBorderStyle = FormBorderStyle.None;
  60. }
  61. private void btnLoad_DragEnter(object sender, DragEventArgs e)
  62. {
  63. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  64. ? DragDropEffects.Link
  65. : DragDropEffects.None;
  66. }
  67. private void btnLoad_DragDrop(object sender, DragEventArgs e)
  68. {
  69. var filename = ((string[])e.Data.GetData(DataFormats.FileDrop)).First();
  70. MsgPostPlay(filename);
  71. }
  72. private void LoadMediaFile()
  73. {
  74. tmrProgress.Stop();
  75. if (_vp != null)
  76. _vp.Dispose();
  77. _vp = new VideoPlay(MsgPostUrl, pnlVideo, _big);
  78. cboAudioTracks.DataSource = _vp.Audios;
  79. trkProgress.Minimum = 0;
  80. trkProgress.Maximum = (int)(_vp.Duration * 10);
  81. trkProgress.Value = 0;
  82. }
  83. private void btnPlay_Click(object sender, EventArgs e)
  84. {
  85. _vp.Play();
  86. tmrProgress.Start();
  87. }
  88. private void btnPause_Click(object sender, EventArgs e)
  89. {
  90. _vp.Pause();
  91. tmrProgress.Stop();
  92. }
  93. private void cboAudioTracks_SelectedIndexChanged(object sender, EventArgs e)
  94. {
  95. _vp.SetAudio(cboAudioTracks.SelectedIndex);
  96. }
  97. private void tmrProgress_Tick(object sender, EventArgs e)
  98. {
  99. trkProgress.Value = (int)(_vp.CurrentPosition * 10);
  100. }
  101. }
  102. }