2021年8月7日土曜日

C#でMusic Playerを作ろう 2

 前回はmciSendString関数を色々試したので、今回はそれを使って簡単なMusic Playerを作ります。ソースコードは530行だけなので簡単です。


 今回のソフトを作るときに一番苦労したのはUIのデザインです。

 最初は、曲を移動するための"Next"、"Prev"ボタンとか、リストから1曲削除するボタンとか、"Pause/Resume"機能とかも作ったのですが、結局、やめました。とにかく単純にしたかったから。

 ちなみに、音量操作をトラックバーではなく、ボタンにしたのは、見た目が理由です。トラックバーだと、再生位置用のトラックバーとかぶるんで。

 リストの保存とか、ファイルの拡張子の制限とか、ほかにも機能は色々追加できるんですが。まぁ、なくてもいいかなぁ、という感じです。

 結果、コンパイルしたら12KBでした。こういう軽いソフトが好きなんです。


 ソースコードはご自由にご利用ください。ただし、趣味のプログラムなので保証はありません。



using System;

using System.IO;

using System.Text;

using System.Drawing;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Collections.Generic;


/*--------------------------------------------------*/

class Program

{

[STAThread]


static void Main()

{

Application.Run( new MusicPlayer());

}

}


/*--------------------------------------------------*/

class MusicPlayer : Form

{

/*--------------------------------------------------*/

[DllImport( "Winmm.dll")]

extern static int mciSendString( string cmd, System.Text.StringBuilder retStr, uint ret, IntPtr hwnd);


/*--------------------------------------------------*/

private MenuStrip ms;

private ToolStripMenuItem[][] tsmi;


private Label lbl;

private ListBox lbx;

private List<string> lst;


private TextBox tbx;

private TrackBar tbr;


private Button[] btn;


private const string DeviceID = "mysound";


private int volume;


private Timer tmr;


/*--------------------------------------------------*/

public MusicPlayer()

{

/*--------------------------------------------------*/

this.ClientSize = new Size( 280, 360);

this.Text = "Music Player";


this.Load += new EventHandler( this.Form_Load);

this.Closed += new EventHandler( this.Form_Closed);


this.AllowDrop = true;

this.DragEnter += new DragEventHandler( this.Form_DragEnter);

this.DragDrop += new DragEventHandler( this.Form_DragDrop);


/*--------------------------------------------------*/

this.CreateMenu();


/*--------------------------------------------------*/

this.lbl = new Label();

this.lbl.SetBounds( 20, 30, 240, 30);

this.lbl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

this.lbl.TextAlign = ContentAlignment.MiddleLeft;

this.lbl.Text = "Play List";

this.Controls.Add( this.lbl);


/*--------------------------------------------------*/

this.lbx = new ListBox();

this.lbx.SetBounds( 20, 60, 240, 160);

this.lbx.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

this.lbx.HorizontalScrollbar = true;

this.Controls.Add( this.lbx);


/*--------------------------------------------------*/

this.tbx = new TextBox();

this.tbx.SetBounds( 20, 230, 240, 20);

this.tbx.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;

this.tbx.BorderStyle = BorderStyle.None;

this.tbx.ReadOnly = true;

this.Controls.Add( this.tbx);

this.tbr = new TrackBar();

this.tbr.SetBounds( 20, 250, 240, 50);

this.tbr.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;

this.tbr.MouseUp += new MouseEventHandler( this.tbr_MouseUp);

this.Controls.Add( this.tbr);


/*--------------------------------------------------*/

this.btn = new Button[4];


this.btn[0] = new Button();

this.btn[0].SetBounds( 20, 300, 60, 40);

this.btn[0].Anchor = AnchorStyles.Bottom;

this.btn[0].Text = ">";

this.btn[0].Click += new EventHandler( this.btn_Play_Click);

this.Controls.Add( this.btn[0]);


this.btn[1] = new Button();

this.btn[1].SetBounds( 80, 300, 60, 40);

this.btn[1].Anchor = AnchorStyles.Bottom;

this.btn[1].Text = "||";

this.btn[1].Click += new EventHandler( this.btn_Stop_Click);

this.Controls.Add( this.btn[1]);


this.btn[2] = new Button();

this.btn[2].SetBounds( 140, 300, 60, 40);

this.btn[2].Anchor = AnchorStyles.Bottom;

this.btn[2].Text = "Vol.-";

this.btn[2].Click += new EventHandler( this.btn_VolumeDown_Click);

this.Controls.Add( this.btn[2]);


this.btn[3] = new Button();

this.btn[3].SetBounds( 200, 300, 60, 40);

this.btn[3].Anchor = AnchorStyles.Bottom;

this.btn[3].Text = "Vol.+";

this.btn[3].Click += new EventHandler( this.btn_VolumeUp_Click);

this.Controls.Add( this.btn[3]);

}


/*--------------------------------------------------*/

private void Form_Load( object sender, EventArgs e)

{

Console.WriteLine( "Form_Load");


this.lst = new List<string>();


this.volume = 10;


this.tmr = new Timer();

this.tmr.Interval = 1000;

this.tmr.Tick += new EventHandler( this.timer_Tick);

}

private void Form_Closed( object sender, EventArgs e)

{

Console.WriteLine( "Form_Closed");


this.tmr.Enabled = false;


string cmd = "stop " + MusicPlayer.DeviceID;

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


cmd = "close " + MusicPlayer.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}


/*--------------------------------------------------*/

private void Form_DragEnter( object sender, DragEventArgs e)

{

if( e.Data.GetDataPresent( DataFormats.FileDrop))

{

e.Effect = DragDropEffects.Move;

}

}

private void Form_DragDrop( object sender, DragEventArgs e)

{

string[] filenames = (string[]) e.Data.GetData( DataFormats.FileDrop, false);


this.lbx_Add( filenames);

}


/*--------------------------------------------------*/

private void CreateMenu()

{

this.ms = new MenuStrip();


this.tsmi = new ToolStripMenuItem[2][];

this.tsmi[0] = new ToolStripMenuItem[4];

this.tsmi[1] = new ToolStripMenuItem[3];


for( int i = 0; i < this.tsmi.Length; i++)

{

for( int j = 0; j < this.tsmi[i].Length; j++)

{

this.tsmi[i][j] = new ToolStripMenuItem();

}

}


this.tsmi[0][0].Text = "File";

this.tsmi[0][1].Text = "Select";

this.tsmi[0][2].Text = "Clear";

this.tsmi[0][3].Text = "Exit (&X)";

this.tsmi[1][0].Text = "MCI";

this.tsmi[1][1].Text = "Play";

this.tsmi[1][2].Text = "Stop";


for( int i = 0; i < this.tsmi.Length; i++)

{

this.ms.Items.Add( this.tsmi[i][0]);


for( int j = 1; j < this.tsmi[i].Length; j++)

{

this.tsmi[i][0].DropDownItems.Add( this.tsmi[i][j]);

this.tsmi[i][j].Click += new EventHandler( this.Menu_Click);

}

}


this.Controls.Add( this.ms);

this.MainMenuStrip = ms;

}


/*--------------------------------------------------*/

/* Menuのイベント処理 */

/*--------------------------------------------------*/

private void Menu_Click( object sender, EventArgs e)

{

Console.WriteLine( sender.ToString());


if( sender == this.tsmi[0][1])

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "All files | *.*";

ofd.Multiselect = true;


if( ofd.ShowDialog() == DialogResult.OK)

{

this.lbx_Add( ofd.FileNames);

}

}

else if( sender == this.tsmi[0][2])

{

this.lbx_Clear();

}

else if( sender == this.tsmi[0][3])

{

this.Close();

}

else if( sender == this.tsmi[1][1])

{

this.btn_Play_Click( null, null);

}

else if( sender == this.tsmi[1][2])

{

this.btn_Stop_Click( null, null);

}

}


/*--------------------------------------------------*/

/* リストの処理 */

/*--------------------------------------------------*/

private void lbx_Add( string[] filenames)

{

for( int i = 0; i < filenames.Length; i++)

{

Console.WriteLine( filenames[i]);


this.lst.Add( filenames[i]);

this.lbx.Items.Add( Path.GetFileName( filenames[i]));

this.lbx.SelectedIndex = this.lbx.Items.Count - 1;

}

}


private void lbx_Clear()

{

this.lst.Clear();

this.lbx.Items.Clear();

}


/*--------------------------------------------------*/

/* Buttonのイベント処理 */

/*--------------------------------------------------*/

private void btn_Play_Click( object sender, EventArgs e)

{

Console.WriteLine( "btn_Play_Click");


string status = this.mciGetStatus();


if( status == "")

{

if( this.lbx.SelectedIndex == -1)

{

return;

}


this.mciOpen( this.lst[this.lbx.SelectedIndex]);

this.mciPlay();

}

else if( status  == "playing")

{

return;

}

else if( status  == "stopped")

{

this.mciPlay();

}

}


private void btn_Stop_Click( object sender, EventArgs e)

{

Console.WriteLine( "btn_Stop_Click");

string status = this.mciGetStatus();


if( status == "")

{

return;

}

else if( status  == "playing")

{

this.mciStop();

}

else if( status  == "stopped")

{

this.mciClose();

}

}


private void btn_VolumeDown_Click( object sender, EventArgs e)

{

Console.WriteLine( "btn_VolumeDown_Click");


string status = this.mciGetStatus();


if( status != "")

{

this.mciVolumeDown();

}

}


private void btn_VolumeUp_Click( object sender, EventArgs e)

{

Console.WriteLine( "btn_VolumeUp_Click");


string status = this.mciGetStatus();


if( status != "")

{

this.mciVolumeUp();

}

}


/*--------------------------------------------------*/

/* TrackBarのイベント処理 */

/*--------------------------------------------------*/

private void tbr_MouseUp( object sender, MouseEventArgs e)

{

Console.WriteLine( "tbr_MouseUp");


string status = this.mciGetStatus();


if( status != "")

{

this.tmr.Enabled = false;

this.mciSeek( this.tbr.Value);

}

}


/*--------------------------------------------------*/

/* 再生中のタイマーイベント */

/*--------------------------------------------------*/

private void timer_Tick( object sender, EventArgs e)

{

Console.WriteLine( "timer tick");


/*--------------------------------------------------*/

string str = this.mciGetStatus();


/*--------------------------------------------------*/

StringBuilder retStr = new StringBuilder( 256);

string cmd = "status " + MusicPlayer.DeviceID + " position";

int ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);


str += ", pos.:" + retStr.ToString();


this.tbr.Value = Convert.ToInt32( retStr.ToString());


cmd = "status " + MusicPlayer.DeviceID + " length";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);


str += " / " + retStr.ToString();


/*--------------------------------------------------*/

cmd = "status " + MusicPlayer.DeviceID + " volume";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);


str += ", vol.:" + retStr.ToString();


/*--------------------------------------------------*/

this.tbx.Text = str;

}


/*--------------------------------------------------*/

/* Notifyイベントの処理 */

/* 曲が終わったら、リストの次の曲を再生する (Stop or Seekから呼ばれた場合はreturn)

/*--------------------------------------------------*/

protected override void WndProc( ref Message m)

{

base.WndProc( ref m);


if( m.Msg == 0x03B9) //MM_MCINOTIFY message

{

Console.WriteLine( "Notify");


if( this.tmr.Enabled == false)

{

return;

}


this.mciStop();

this.mciClose();


if( ! ( 0 <= this.lbx.SelectedIndex && this.lbx.SelectedIndex < this.lbx.Items.Count - 1))

{

return;

}


this.lbx.SelectedIndex++;


this.mciOpen( this.lst[this.lbx.SelectedIndex]);

this.mciPlay();

}

}


/*--------------------------------------------------*/

/* MCIの状態を返す ("", "playing", "stopped"のいずれかになるはず) */

/*--------------------------------------------------*/

private string mciGetStatus()

{

StringBuilder retStr = new StringBuilder( 256);


string cmd = "status " + MusicPlayer.DeviceID + " mode";

int ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);


Console.WriteLine( "mode : " + retStr);


return retStr.ToString();

}


/*--------------------------------------------------*/

/* MCI Open, Play, Stop, Close */

/*--------------------------------------------------*/

private void mciOpen( string filename)

{

/*--------------------------------------------------*/

string cmd = "open " + "\"" + filename + "\"" + " alias " + MusicPlayer.DeviceID;

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


/*--------------------------------------------------*/

cmd = "setaudio " + MusicPlayer.DeviceID + " volume to " + ( this.volume * 100).ToString();

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


/*--------------------------------------------------*/

StringBuilder retStr = new StringBuilder( 256);


cmd = "status " + MusicPlayer.DeviceID + " length";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);


this.tbr.Minimum = 0;

this.tbr.Maximum = Convert.ToInt32( retStr.ToString());

}


private void mciPlay()

{

string cmd = "play " + MusicPlayer.DeviceID + " notify";

int ret = mciSendString( cmd, null, 0, this.Handle);

Console.WriteLine( cmd);


this.tmr.Enabled = true;


this.timer_Tick( null, null);

}


private void mciStop()

{

this.tmr.Enabled = false;


string cmd = "stop " + MusicPlayer.DeviceID;

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


this.timer_Tick( null, null);

}


private void mciClose()

{

string cmd = "close " + MusicPlayer.DeviceID;

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


this.tbx.Text = "";

}


/*--------------------------------------------------*/

/* MCI Volume Down/Up */

/*--------------------------------------------------*/

private void mciVolumeDown()

{

this.volume = Math.Max( 0, this.volume - 1);

string cmd = "setaudio " + MusicPlayer.DeviceID + " volume to " + ( this.volume * 100).ToString();

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


this.timer_Tick( null, null);

}


private void mciVolumeUp()

{

this.volume = Math.Min( this.volume + 1, 10);

string cmd = "setaudio " + MusicPlayer.DeviceID + " volume to " + ( this.volume * 100).ToString();

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


this.timer_Tick( null, null);

}


/*--------------------------------------------------*/

/* MCI Seek */

/*--------------------------------------------------*/

private void mciSeek( int position)

{

string cmd = "seek " + MusicPlayer.DeviceID + " to " + position.ToString();

int ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


this.timer_Tick( null, null);

}

}


C#でMusic Playerを作ろう 1

 以前から思っていたのですが、パソコンで音楽を再生するソフトは重たいです。Windows Media Playerとか、Quick Timeとかですね。ただ、BGMが欲しいだけなのに。変な視覚エフェクトとか、ネットワークから情報を集めてきたりとか、そういうのはいらないんですが。。。

 というわけで、音楽再生のソフトを作ろうと思います。まぁ、Win32APIのmciSendString関数を使うだけの簡単なソフトです。


 手始めに、mciSendString関数を使って、色々試してみました。

 Statusの変化するタイミングとか、PauseとStopの違いとか、使ってみないと分からないこともあるもんです。面白かったのが、Volumeの設定です。Volumeは0-1000で変化するみたいですが、例えば800に設定しようとしても803になったりしました。ハードウェアの都合なのかもしれません。へぇー。


 ソースコードはご自由にご利用ください。ただし、趣味のプログラムなので保証はありません。

 (ちなみに、メニューを作っているのは、ウィルス対策ソフトと戦ったからです。)


using System;

using System.IO;

using System.Text;

using System.Drawing;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Collections.Generic;


/*--------------------------------------------------*/

class Program

{

[STAThread]


static void Main()

{

Application.Run( new MciTest());

}

}


/*--------------------------------------------------*/

class MciTest: Form

{

/*--------------------------------------------------*/

[DllImport( "Winmm.dll")]

extern static int mciSendString( string cmd, System.Text.StringBuilder retStr, uint ret, IntPtr hwnd);


/*--------------------------------------------------*/

private MenuStrip ms;

private ToolStripMenuItem[][] tsmi;


private Button[] btn;


private const string DeviceID = "mysound";


private string filename = @"C:\Windows\Media\Alarm02.wav";


/*--------------------------------------------------*/

public MciTest()

{

/*--------------------------------------------------*/

this.ClientSize = new Size( 360, 240);

this.Text = "MCITest";


this.Load += new EventHandler( this.Form_Load);

this.Closed += new EventHandler( this.Form_Closed);


/*--------------------------------------------------*/

this.CreateMenu();


/*--------------------------------------------------*/

this.btn = new Button[16];


for( int i = 0; i < this.btn.Length; i++)

{

this.btn[i] = new Button();

this.btn[i].SetBounds( 20 + 80 * ( i % 4), 40 + 40 * ( i / 4), 80, 40);

this.btn[i].Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

this.btn[i].Text = i.ToString();

this.btn[i].Click += new EventHandler( this.btn_Click);

this.Controls.Add( this.btn[i]);

}


this.btn[0].Text = "open";

this.btn[1].Text = "close";

this.btn[2].Text = "play";

this.btn[3].Text = "stop";


this.btn[4].Text = "pause";

this.btn[5].Text = "resume";

this.btn[6].Text = "seek";

this.btn[7].Text = "position";


this.btn[8].Text = "volume quiet";

this.btn[9].Text = "volume loud";

this.btn[10].Text = "audio on";

this.btn[11].Text = "audio off";


this.btn[12].Text = "info";

this.btn[13].Text = "mode";

}


/*--------------------------------------------------*/

private void Form_Load( object sender, EventArgs e)

{

Console.WriteLine( "Form_Load");

}

private void Form_Closed( object sender, EventArgs e)

{

Console.WriteLine( "Form_Closed");


int ret;

string cmd;


cmd = "stop " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);


cmd = "close " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

}


/*--------------------------------------------------*/

private void CreateMenu()

{

this.ms = new MenuStrip();


this.tsmi = new ToolStripMenuItem[1][];

this.tsmi[0] = new ToolStripMenuItem[3];


for( int i = 0; i < this.tsmi.Length; i++)

{

for( int j = 0; j < this.tsmi[i].Length; j++)

{

this.tsmi[i][j] = new ToolStripMenuItem();

}

}


this.tsmi[0][0].Text = "Menu (&M)";

this.tsmi[0][1].Text = "File (&F)";

this.tsmi[0][2].Text = "Close (&C)";


for( int i = 0; i < this.tsmi.Length; i++)

{

this.ms.Items.Add( this.tsmi[i][0]);


for( int j = 1; j < this.tsmi[i].Length; j++)

{

this.tsmi[i][0].DropDownItems.Add( this.tsmi[i][j]);

this.tsmi[i][j].Click += new EventHandler( this.Menu_Click);

}

}


this.Controls.Add( this.ms);

this.MainMenuStrip = ms;

}


/*--------------------------------------------------*/

private void Menu_Click( object sender, EventArgs e)

{

Console.WriteLine( sender.ToString());


if( sender == this.tsmi[0][1])

{

MessageBox.Show( Application.ExecutablePath);


OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "All files | *.*";

ofd.Multiselect = true;


if( ofd.ShowDialog() == DialogResult.OK)

{

MessageBox.Show( ofd.FileNames[0]);

this.filename = ofd.FileNames[0];

}

}

else if( sender == this.tsmi[0][2])

{

this.Close();

}

}


/*--------------------------------------------------*/

private void btn_Click( object sender, EventArgs e)

{

Console.WriteLine( "btn_Click" + "\t" + ( (Button)sender).Text);


int ret;

string cmd;

StringBuilder retStr = new StringBuilder( 256);


if( ( (Button)sender).Text == "open")

{

cmd = "open " + "\"" + this.filename + "\"" + " alias " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "close")

{

cmd = "close " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "play")

{

cmd = "play " + MciTest.DeviceID + " notify";

ret = mciSendString( cmd, null, 0, this.Handle);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "stop")

{

cmd = "stop " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "pause")

{

cmd = "pause " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


cmd = "status " + MciTest.DeviceID + " pause timeout";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "pause timeout : " + retStr);

}

else if( ( (Button)sender).Text == "resume")

{

cmd = "resume " + MciTest.DeviceID;

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "seek")

{

cmd = "seek " + MciTest.DeviceID + " to start";

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "position")

{

cmd = "status " + MciTest.DeviceID + " position";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.Write( "position : " + retStr);


cmd = "status " + MciTest.DeviceID + " length";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( " / " + retStr);

}

else if( ( (Button)sender).Text == "volume quiet")

{

cmd = "status " + MciTest.DeviceID + " volume";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "volume : " + retStr);


cmd = "setaudio " + MciTest.DeviceID + " volume to 200";

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


cmd = "status " + MciTest.DeviceID + " volume";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "volume : " + retStr);

}

else if( ( (Button)sender).Text == "volume loud")

{

cmd = "status " + MciTest.DeviceID + " volume";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "volume : " + retStr);


cmd = "setaudio " + MciTest.DeviceID + " volume to 800";

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);


cmd = "status " + MciTest.DeviceID + " volume";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "volume : " + retStr);

}

else if( ( (Button)sender).Text == "audio on")

{

cmd = "set " + MciTest.DeviceID + " audio all on";

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "audio off")

{

cmd = "set " + MciTest.DeviceID + " audio all off";

ret = mciSendString( cmd, null, 0, IntPtr.Zero);

Console.WriteLine( cmd);

}

else if( ( (Button)sender).Text == "info")

{

cmd = "info " + MciTest.DeviceID + " file";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "info : " + retStr);

}

else if( ( (Button)sender).Text == "mode")

{

cmd = "status " + MciTest.DeviceID + " mode";

ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);

Console.WriteLine( "mode : " + retStr);

}

}


/*--------------------------------------------------*/

protected override void WndProc( ref Message m)

{

base.WndProc( ref m);


if( m.Msg == 0x03B9) //MM_MCINOTIFY message

{

Console.WriteLine( "Notify");


StringBuilder retStr = new StringBuilder( 256);


string cmd = "status " + MciTest.DeviceID + " mode";;

int ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);;


Console.WriteLine( "mode : " + retStr);

}

}

}


ウィルス対策ソフトと戦う

 趣味でプログラムを書いていると、ウィルス対策ソフトと戦うことがあります。

 先日、C#で書いたプログラムが、なぜか有害なソフトとして認識されました。フォームを1個作るだけのプログラムなのに、コンパイルした実行ファイルは、ウィルス対策ソフトによって即削除されました。うぅっ(泣)。

 ウィルススキャンが働いた後、"ウィルスを駆除しました。心配いりません。"とかメッセージが出てきます。

・・・このポンコツ感・・・


 あるソフトウェアが有害か否かを判定するのは難しいと思います。有害と判定されない方法がわかったら、当然、悪意のあるソフトは、それになりすますでしょう。というわけで、判定方法は秘密なはずです。


 仕方ないとは言え、もどかしい話です。


 ちなみにソースコードは以下。

 ウィルススキャンの対象フォルダを設定すればいいんですが、めんどくさい。ていうか、ウィルス対策ソフトのご機嫌をうかがっているみたいで、なんか嫌です。


using System;

using System.Windows.Forms;


class Program

{

[STAThread]


static void Main()

{

Application.Run( new Form());

}

}