using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
/* 工具箱 --> 右键单击选择项 --> COM 组件 --> Windows Media Player
添加引用 --> COM --> Windows Media Player wmp.dll */
namespace WindowsMedia
{
public partial class FormWMP : Form
{
#region
private Timer timerInfo;
private NotifyIcon notifyInfo;
private ChineseLunisolarCalendar lunarCalendar;
#endregion
public FormWMP()
{
#region
InitializeComponent();
lunarCalendar = new ChineseLunisolarCalendar();
this.AllowDrop = true; // 启用拖放。
this.DesktopBounds = Screen.GetWorkingArea(this);
using (RegistryKey userKey = Application.UserAppDataRegistry)
{
string dirPath = userKey.GetValue("URL") as string;
if (Directory.Exists(dirPath))
Environment.CurrentDirectory = dirPath;
LoadFiles();
}
TimerStyle();
WMPlayerStyle();
DrawListView();
DrawComboBox();
NotifyIconStyle();
#endregion
}
#region NotifyIcon
private void NotifyIconStyle()
{
notifyInfo = new NotifyIcon();
notifyInfo.Visible = true;
notifyInfo.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
notifyInfo.Text = AppDomain.CurrentDomain.FriendlyName;
notifyInfo.BalloonTipClosed += new EventHandler(notifyInfo_BalloonTipClosed);
notifyInfo.MouseClick += new MouseEventHandler(notifyInfo_MouseClick);
}
private void notifyInfo_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
if (this.Visible = !this.Visible)
this.Activate();
break;
case MouseButtons.Right:
notifyInfo.ShowBalloonTip(10, notifyInfo.Text, " 在系统托盘中运行", ToolTipIcon.Info);
break;
}
}
private void notifyInfo_BalloonTipClosed(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
#region Timer
private void TimerStyle()
{
timerInfo = new Timer();
timerInfo.Enabled = true;
timerInfo.Interval = 1000;
timerInfo.Tick += new EventHandler(this.timerWMP_Tick);
}
private void timerWMP_Tick(object sender, EventArgs e)
{
switch (axWMPlayer.playState)
{
case WMPLib.WMPPlayState.wmppsTransitioning:
case WMPLib.WMPPlayState.wmppsPlaying:
if (!toolComboBoxColor.DroppedDown)
listViewWMP.Focus();
break;
case WMPLib.WMPPlayState.wmppsReady:
axWMPlayer.Ctlcontrols.play();
break;
}
Application.CurrentCulture.ClearCachedData();
DateTime solar = DateTime.Now;
int month = lunarCalendar.GetMonth(solar);
int leapMonth = lunarCalendar.GetLeapMonth(lunarCalendar.GetYear(solar));
if (0 < leapMonth && leapMonth <= month)
--month;
statusLabelTime.Text = string.Format("{0:F} [{1} {2:00}]", solar, DateTimeFormatInfo.CurrentInfo.MonthNames[month - 1], lunarCalendar.GetDayOfMonth(solar));
}
#endregion
#region WMPlayerStyle
private void WMPlayerStyle()
{
axWMPlayer.enableContextMenu = true; // 启用上下文菜单。
axWMPlayer.stretchToFit = true; // 自动缩放。
axWMPlayer.settings.volume = 100; // 音量调最大。
axWMPlayer.settings.setMode("loop", true); // 循环播放。
axWMPlayer.CurrentItemChange += new AxWMPLib._WMPOCXEvents_CurrentItemChangeEventHandler(axWMPlayer_CurrentItemChange);
}
private void axWMPlayer_CurrentItemChange(object sender, AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent e)
{
if (listViewWMP.Items.Count != axWMPlayer.currentPlaylist.count)
{
listViewWMP.Items.Clear();
return;
}
for (int index = 0; index < axWMPlayer.currentPlaylist.count; ++index)
{
if (axWMPlayer.currentMedia.get_isIdentical(axWMPlayer.currentPlaylist.get_Item(index)))
{
ListViewItem item = listViewWMP.Items[index];
this.Text = item.Text;
item.Focused = true;
item.EnsureVisible();
break;
}
}
}
private void toolButtonBalance_Click(object sender, EventArgs e)
{
switch ((sender as ToolStripButton).Name)
{
case "toolButtonLeft":
axWMPlayer.settings.balance = -100; // 左声道。
break;
case "toolButtonStereo":
axWMPlayer.settings.balance = 0; // 立体声。
break;
case "toolButtonRight":
axWMPlayer.settings.balance = 100; // 右声道。
break;
}
}
#endregion
#region DrawComboBox
private void DrawComboBox()
{
toolComboBoxColor.BeginUpdate();
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
{
Color bc = Color.FromKnownColor(kc);
if (!bc.IsSystemColor && bc != Color.Transparent)
toolComboBoxColor.Items.Add(bc);
}
toolComboBoxColor.EndUpdate();
toolComboBoxColor.SelectedItem = Color.BlueViolet;
toolComboBoxColor.DropDownStyle = ComboBoxStyle.DropDownList;
toolComboBoxColor.ComboBox.DrawMode = DrawMode.OwnerDrawFixed; // 用代码绘制控件中的所有元素,并且元素大小都相等。
toolComboBoxColor.ComboBox.DrawItem += new DrawItemEventHandler(toolComboBoxColor_DrawItem);
}
private void toolComboBoxColor_DrawItem(object sender, DrawItemEventArgs e)
{
using (Graphics g = e.Graphics)
using (SolidBrush b = new SolidBrush(e.BackColor))
{
if ((e.State & DrawItemState.Selected) != 0) // 取交集。
b.Color = Color.BlueViolet;
Rectangle r = e.Bounds;
g.FillRectangle(b, r);
r.Offset(1, 1);
r.Width = 52;
r.Height -= 2;
b.Color = Color.Black;
g.FillRectangle(b, r);
r.Offset(1, 1);
r.Width -= 2;
r.Height -= 2;
b.Color = (Color)toolComboBoxColor.Items[e.Index];
g.FillRectangle(b, r);
string colorName = b.Color.Name;
b.Color = e.ForeColor;
g.DrawString(colorName, e.Font, b, r.X + 72, r.Y);
}
}
#endregion
#region DrawListView
private void DrawListView()
{
listViewWMP.GridLines = true; // 显示网格线。
listViewWMP.OwnerDraw = true; // 代码绘制。
listViewWMP.MultiSelect = false; // 仅选择单项。
listViewWMP.ShowItemToolTips = true; // 显示工具提示。
listViewWMP.Sorting = SortOrder.None; // 禁止排序。
listViewWMP.View = View.Details; // 详细信息。
listViewWMP.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listViewWMP_DrawColumnHeader);
listViewWMP.DrawItem += new DrawListViewItemEventHandler(listViewWMP_DrawItem);
listViewWMP.ClientSizeChanged += new EventHandler(listViewWMP_ClientSizeChanged);
listViewWMP.Click += new EventHandler(listViewWMP_Click);
listViewWMP.KeyUp += new KeyEventHandler(listViewWMP_KeyUp);
}
private void listViewWMP_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(SystemBrushes.InactiveCaptionText, e.Bounds);
e.DrawText(TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
private void listViewWMP_DrawItem(object sender, DrawListViewItemEventArgs e)
{
if ((e.State & ListViewItemStates.Focused) != 0)
using (SolidBrush sb = new SolidBrush((Color)toolComboBoxColor.SelectedItem))
{
e.Graphics.FillRectangle(sb, e.Bounds);
}
e.DrawText(TextFormatFlags.Default);
}
private void listViewWMP_ClientSizeChanged(object sender, EventArgs e)
{
columnHeaderWMP.Width = listViewWMP.ClientSize.Width;
}
private void listViewWMP_Click(object sender, EventArgs e)
{
if (listViewWMP.FocusedItem != null)
axWMPlayer.Ctlcontrols.playItem(axWMPlayer.currentPlaylist.get_Item(listViewWMP.FocusedItem.Index));
}
private void listViewWMP_KeyUp(object sender, KeyEventArgs e)
{
if (listViewWMP.FocusedItem == null)
return;
switch (e.KeyData)
{
case Keys.Enter:
axWMPlayer.Ctlcontrols.playItem(axWMPlayer.currentPlaylist.get_Item(listViewWMP.FocusedItem.Index));
break;
case Keys.Escape:
axWMPlayer.Ctlcontrols.stop();
break;
case Keys.Space:
switch (axWMPlayer.playState)
{
case WMPLib.WMPPlayState.wmppsPlaying:
axWMPlayer.Ctlcontrols.pause();
break;
case WMPLib.WMPPlayState.wmppsPaused:
axWMPlayer.Ctlcontrols.play();
break;
}
break;
case Keys.Left:
axWMPlayer.Ctlcontrols.previous();
break;
case Keys.Right:
axWMPlayer.Ctlcontrols.next();
break;
case Keys.Control | Keys.Left:
axWMPlayer.Ctlcontrols.currentPosition -= 5;
break;
case Keys.Control | Keys.Right:
axWMPlayer.Ctlcontrols.currentPosition += 5;
break;
}
}
#endregion
#region LoadFiles
private void toolButtonOpen_Click(object sender, EventArgs e)
{
if (folderBrowserWMP.ShowDialog(this) == DialogResult.OK)
{
Environment.CurrentDirectory = folderBrowserWMP.SelectedPath;
LoadFiles();
}
}
private void LoadFiles()
{
axWMPlayer.currentPlaylist.clear();
listViewWMP.Items.Clear();
listViewWMP.BeginUpdate();
folderBrowserWMP.Description = Environment.CurrentDirectory;
DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory);
foreach (FileInfo info in dir.GetFiles())
{
switch (info.Extension.ToLowerInvariant())
{
case ".wma":
case ".mp3":
case ".swf":
case ".wmv":
case ".avi":
WMPLib.IWMPMedia wmp = axWMPlayer.newMedia(info.FullName);
axWMPlayer.currentPlaylist.appendItem(wmp);
ListViewItem item = listViewWMP.Items.Add(Path.GetFileNameWithoutExtension(info.Name));
item.ToolTipText = string.Format("艺术家: {0}/n时长: {1}/n大小: {2:#,##0.00} MB", wmp.getItemInfo("Author"), wmp.durationString, info.Length / 1048576M);
break;
}
}
listViewWMP.EndUpdate();
columnHeaderWMP.Width = listViewWMP.ClientSize.Width;
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
DataObject data = e.Data as DataObject;
if (data.ContainsFileDropList())
{
string dirPath = data.GetFileDropList()[0];
if ((File.GetAttributes(dirPath) & FileAttributes.Directory) != 0)
{
Environment.CurrentDirectory = dirPath;
LoadFiles();
this.Activate();
}
}
}
#endregion
#region FormClosing
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
switch (e.CloseReason)
{
case CloseReason.ApplicationExitCall:
case CloseReason.TaskManagerClosing:
axWMPlayer.close();
notifyInfo.Visible = false;
using (RegistryKey subKey = Application.UserAppDataRegistry)
{
subKey.SetValue("URL", Environment.CurrentDirectory);
}
break;
case CloseReason.UserClosing:
this.Visible = false;
e.Cancel = true;
break;
}
}
#endregion
#region ToolStripButton
private void toolButtonSplit_Click(object sender, EventArgs e)
{
splitContainerWMP.Panel1Collapsed = !splitContainerWMP.Panel1Collapsed;
}
private void toolButtonTopMost_Click(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
toolButtonTopMost.Checked = this.TopMost;
}
private void toolButtonProperty_Click(object sender, EventArgs e)
{
if (axWMPlayer.HasPropertyPages())
axWMPlayer.ShowPropertyPages();
}
private void toolButtonVideo_Click(object sender, EventArgs e)
{
splitContainerWMP.Panel1Collapsed = true;
using (FormXML xml = new FormXML())
{
xml.Tag = axWMPlayer;
xml.ShowDialog(this);
}
}
#endregion
}
}