2023年6月4日日曜日

仕事を増やす人、仕事を減らす人

 世の中には、仕事を増やす人と仕事を減らす人がいます。


 例えば、新しいシステムを作ったりすると、そのシステムの運用の仕事が増えます。よくあるのは、システムを作ることだけ考えて、その実際の運用を考えていないため、現場が混乱するというパターンです。私が思うに、仕事を増やす人は、往々にして、優秀ではないです。


 私が優秀だと思う人は、既存のシステムを調べて、無駄を取り除く人です。無駄な仕事が減るので、現場の負担が減ります。私は、仕事を終わらせる人ほど、価値が高いと思います。


 成果主義では、「新しいシステムを作りました」みたいなのが評価されがちです。新しいシステムを作って、仕事を増やして、みんなを疲弊させることが評価されていたら、ひどい話です。

 仕事を減らすことにより生まれる価値を高く評価すべきだと思います。

また欧米か

 ニュースなどの識者のコメントで、「欧米では・・・」という言葉を聴くことが多々あります。うんざりします。


 欧米は常に正しいということでしょうか。明治時代の考え方みたいです。思考を停止していると思います。


 世界のトレンドを把握することは重要ですが、諸外国のやり方が常に正しいとは限りません。原発の廃止 or 存続が二転三転しているように、何が正しいかは簡単には判断できません。


 もう少し合理的なコメントを聞かせて欲しいものです。

名字故事

 「毛沢東」という名前は、日本では「モウタクトウ」と読まれていると思います。ですが、Googleで「毛沢東」を読ませると全然違います。気になった方は試してみてください。


 日本の感覚に慣れすぎいて全然気づかなかったのですが、名前を日本語読みにするのは、失礼なのでは?とふと思いました。

2023年1月22日日曜日

C#でピアノを作ろう (2022/1/22版)

  以前、C#でMIDIを遊ぶプログラムを作りました。これを少し更新してみました。


 ボタンで作成していた鍵盤をPictureBoxに変えて、キーイベントを追加しました。そのうち自動演奏機能も付けようとしてますが、いつになるかは気分次第。


 ソースコードのご利用はご自由に。


using System;

using System.Runtime.InteropServices;

using System.Drawing;

using System.Windows.Forms;

using System.Collections.Generic;


class Program

{

[STAThread]


static void Main()

{

Application.Run( new FormPiano());

}

}


class FormPiano : Form

{

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

/* 使用するAPIの宣言 */

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

[DllImport( "Winmm.dll")]

extern static uint midiOutOpen( ref long lphmo, uint uDeviceID, uint dwCallback, uint dwCallbackInstance, uint dwFlags);


[DllImport( "Winmm.dll")]

extern static uint midiOutClose( long hmo);


[DllImport( "Winmm.dll")]

extern static uint midiOutShortMsg( long hmo, uint dwMsg);


private const uint MMSYSERR_NOERROR = 0;

private const uint MIDI_MAPPER = 0xffffffff;


private long hMidi;


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

private MenuStrip ms;

private ToolStripMenuItem[][] tsmi;


private Label lbl;

private ComboBox cbx;


private Panel pnl;

private PictureBox pbx;


private Dictionary<Keys, int> keyAssign;


private bool[] keyFlag;


private int mouseX;

private int mouseY;


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

public FormPiano()

{

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

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

this.Text = "Hello World!";


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

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


this.KeyPreview = true;

this.KeyDown += new KeyEventHandler( this.Form_KeyDown);

this.KeyUp += new KeyEventHandler( this.Form_KeyUp);


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

this.CreateMenu();


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

this.CreateCtrl();

}


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

private void Form_Load( object sender, EventArgs e)

{

Console.WriteLine( "Form_Load");


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

this.pnl.AutoScrollPosition = new Point( 280 * 4, 0);

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

this.keyAssign = new Dictionary<Keys, int>();


this.keyAssign.Add( Keys.A, 60);

this.keyAssign.Add( Keys.W, 61);

this.keyAssign.Add( Keys.S, 62);

this.keyAssign.Add( Keys.E, 63);

this.keyAssign.Add( Keys.D, 64);

this.keyAssign.Add( Keys.F, 65);

this.keyAssign.Add( Keys.T, 66);

this.keyAssign.Add( Keys.G, 67);

this.keyAssign.Add( Keys.Y, 68);

this.keyAssign.Add( Keys.H, 69);

this.keyAssign.Add( Keys.U, 70);

this.keyAssign.Add( Keys.J, 71);

this.keyAssign.Add( Keys.K, 72);

this.keyAssign.Add( Keys.O, 73);

this.keyAssign.Add( Keys.L, 74);


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

if( midiOutOpen( ref this.hMidi, MIDI_MAPPER, 0, 0, 0) != MMSYSERR_NOERROR)

{

MessageBox.Show( "midiOutOpen error");

Application.Exit();

}


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

this.keyFlag = new bool[128];


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

{

this.keyFlag[i] = false;

}


this.mouseX = 0;

this.mouseY = 0;

}

private void Form_Closed( object sender, EventArgs e)

{

Console.WriteLine( "Form_Closed");


midiOutClose( this.hMidi);

}


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

/* Menu */

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

private void CreateMenu()

{

this.ms = new MenuStrip();


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

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


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 (&F)";

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


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.ToString() == "Exit (&X)")

{

Application.Exit();

}

}


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

/* Control */

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

private void CreateCtrl()

{

this.lbl = new Label();

this.lbl.SetBounds( 20, this.ms.Height, 60, 20);

this.lbl.Text = "program";

this.Controls.Add( this.lbl);


this.cbx = new ComboBox();

this.cbx.SetBounds( 80, this.ms.Height, 60, 20);

this.cbx.DropDownStyle = ComboBoxStyle.DropDownList;

this.Controls.Add( this.cbx);


for( int i = 0; i < 128; i++)

{

this.cbx.Items.Add( i);

}


this.cbx.SelectedIndex = 0;

this.cbx.SelectedIndexChanged += new EventHandler( this.cbx_SelectedIndexChanged);

this.pnl = new Panel();

this.pnl.SetBounds( 20, this.ms.Height + 20, 800, 200 - this.ms.Height);

this.pnl.Anchor = ( AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom);

this.pnl.BorderStyle = BorderStyle.FixedSingle;

this.pnl.AutoScroll = true;

this.Controls.Add( this.pnl);


this.pbx = new PictureBox();

this.pbx.SetBounds( 0, 0, 3000, 160);

this.pbx.BorderStyle = BorderStyle.FixedSingle;

this.pbx.Paint += new PaintEventHandler( this.pbx_Paint);

this.pbx.MouseDown += new MouseEventHandler( this.pbx_MouseDown);

this.pbx.MouseMove += new MouseEventHandler( this.pbx_MouseMove);

this.pbx.MouseUp += new MouseEventHandler( this.pbx_MouseUp);

this.pnl.Controls.Add( this.pbx);

}


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

/* PictureBox Paint */

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

private void pbx_Paint( object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;


Rectangle rect = new Rectangle();


Font fnt = new Font( "Arial", 8);

StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Center;

sf.LineAlignment = StringAlignment.Center;


/*----- White Keys -----*/

for( int i = 0; i < 128; i++)

{

int q = i / 12;

int r = i % 12;


if( r == 0 || r == 2 || r == 4 || r == 5 || r == 7 || r == 9 || r == 11)

{

rect.X = 280 * q + 20 * ( r < 5 ? r : r + 1);

rect.Y = 0;

rect.Width = 40;

rect.Height = 160;


Brush b = ( this.keyFlag[i] ? Brushes.Pink : Brushes.White);

Pen p =  Pens.Black;


g.FillRectangle( b, rect);

g.DrawRectangle( p, rect);


rect.Y = 120;

rect.Height = 40;

g.DrawString( i.ToString(), fnt, Brushes.Gray, rect, sf);


foreach( KeyValuePair<Keys, int> kvp in this.keyAssign)

{

if( i == kvp.Value)

{

rect.Y = 80;

g.DrawString( ( (char)kvp.Key).ToString(), fnt, Brushes.Gray, rect, sf);

}

}

}

}


/*----- Black Keys -----*/

for( int i = 0; i < 128; i++)

{

int q = i / 12;

int r = i % 12;


if( r == 1 || r == 3 || r == 6 || r == 8 || r == 10)

{

rect.X = 280 * q + 20 * ( r < 5 ? r : r + 1) + 5;

rect.Y = 0;

rect.Width = 30;

rect.Height = 80;


Brush b = ( this.keyFlag[i] ? Brushes.Pink : Brushes.Black);

Pen p =  Pens.White;


g.FillRectangle( b, rect);

g.DrawRectangle( p, rect);


rect.Y = 40;

rect.Height = 40;

g.DrawString( i.ToString(), fnt, Brushes.Gray, rect, sf);

foreach( KeyValuePair<Keys, int> kvp in this.keyAssign)

{

if( i == kvp.Value)

{

rect.Y = 0;

g.DrawString( ( (char)kvp.Key).ToString(), fnt, Brushes.Gray, rect, sf);

}

}

}

}

}


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

/* Mouse Event */

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

private void pbx_MouseDown( object sender, MouseEventArgs e)

{

Console.WriteLine( "pbx_MouseDown");


byte key = this.PositionToKey( e.X, e.Y);


if( ( key != 255) && !this.keyFlag[key])

{

this.keyFlag[key] = true;

this.NoteOn( key);


this.mouseX = e.X;

this.mouseY = e.Y;

}


this.pbx.Invalidate();

}


private void pbx_MouseMove( object sender, MouseEventArgs e)

{

if( e.Button != MouseButtons.None)

{

byte key1 = this.PositionToKey( this.mouseX, this.mouseY);

byte key2 = this.PositionToKey( e.X, e.Y);


if( key1 != key2)

{

Console.WriteLine( "pbx_MouseMove");


this.keyFlag[key1] = false;

this.NoteOff( key1);


this.keyFlag[key2] = true;

this.NoteOn( key2);


this.mouseX = e.X;

this.mouseY = e.Y;


this.pbx.Invalidate();

}

}

}


private void pbx_MouseUp( object sender, MouseEventArgs e)

{

Console.WriteLine( "pbx_MouseUp");


byte key = this.PositionToKey( e.X, e.Y);


if( ( key != 255) && this.keyFlag[key])

{

this.keyFlag[key] = false;

this.NoteOff( key);

}


this.pbx.Invalidate();

}


private byte PositionToKey( int x, int y)

{

Rectangle rect = new Rectangle();


/*----- Black Keys -----*/

for( int i = 0; i < 128; i++)

{

int q = i / 12;

int r = i % 12;


if( r == 1 || r == 3 || r == 6 || r == 8 || r == 10)

{

rect.X = 280 * q + 20 * ( r < 5 ? r : r + 1) + 5;

rect.Y = 0;

rect.Width = 30;

rect.Height = 80;


if( rect.X <= x && x < rect.X + rect.Width && rect.Y <= y && y <= rect.Y + rect.Height)

{

return (byte) i;

}

}

}


/*----- White Keys -----*/

for( int i = 0; i < 128; i++)

{

int q = i / 12;

int r = i % 12;


if( r == 0 || r == 2 || r == 4 || r == 5 || r == 7 || r == 9 || r == 11)

{

rect.X = 280 * q + 20 * ( r < 5 ? r : r + 1);

rect.Y = 0;

rect.Width = 40;

rect.Height = 160;


if( rect.X <= x && x < rect.X + rect.Width && rect.Y <= y && y <= rect.Y + rect.Height)

{

return (byte) i;

}

}

}


return 255;

}


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

/* Key Event */

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

private void Form_KeyDown( object sender, KeyEventArgs e)

{

Console.WriteLine( "Form1_KeyDown" + "\t" + e.KeyCode.ToString());


foreach( KeyValuePair<Keys, int> kvp in this.keyAssign)

{

if( e.KeyCode == kvp.Key)

{

byte key = (byte)kvp.Value;


if( !this.keyFlag[key])

{

this.keyFlag[key] = true;

this.NoteOn( key);

}


this.pbx.Invalidate();


return;

}

}

}


private void Form_KeyUp( object sender, KeyEventArgs e)

{

Console.WriteLine( "Form1_KeyUp" + "\t" + ( (int) e.KeyCode).ToString());


foreach( KeyValuePair<Keys, int> kvp in this.keyAssign)

{

if( e.KeyCode == kvp.Key)

{

byte key = (byte)kvp.Value;


if( this.keyFlag[key])

{

this.keyFlag[key] = false;

this.NoteOff( key);

}


this.pbx.Invalidate();

}

}

}


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

/* ComboBox */

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

private void cbx_SelectedIndexChanged(object sender, System.EventArgs e)

{

byte prg = (byte) this.cbx.SelectedIndex;

Console.WriteLine( "cbx_SelectedIndexChanged " + prg.ToString());


this.ProgramChange( prg);

}


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

/* Note On/Off */

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

private void NoteOn( byte key)

{

byte ch = 0; //(byte) ( ( this.cmb[0].SelectedIndex == 0) ? 0 : 9);

byte velocity = 0x7f;

uint msg;

msg = (uint)( ( velocity << 16) + ( key << 8) + 0x90 + ch);

midiOutShortMsg( this.hMidi, msg);


DateTime dt = DateTime.Now;

Console.WriteLine( dt.ToString( "hh:mm:ss.fff") + "\t" + "NoteOn" + "\t" + key.ToString());

}

private void NoteOff( byte key)

{

byte ch = 0; //(byte) ( ( this.cmb[0].SelectedIndex == 0) ? 0 : 9);

byte velocity = 0x7f;

uint msg;

msg = (uint)( ( velocity << 16) + ( key << 8) + 0x80 + ch);

midiOutShortMsg( this.hMidi, msg);


DateTime dt = DateTime.Now;

Console.WriteLine( dt.ToString( "hh:mm:ss.fff") + "\t" + "NoteOff" + "\t" + key.ToString());

}


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

/* Program Change */

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

private void ProgramChange( byte prg)

{

byte ch = (byte) 0;


uint msg;

msg = (uint)( ( prg << 8) + 0xc0 + ch);

midiOutShortMsg( this.hMidi, msg);

}

}



C#でMusic Playerを作ろう 3

 以前、C#で簡単なMusic Playerを作成したのですが、ちょっと更新してみました。


 主な変更点は、再生位置の表示をTrackbarからPictureBoxに変えたことです。Trackbarだと、使いにくかった。


 ソースコードのご利用はご自由に。


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 sieStr, IntPtr hwnd);


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

private MenuStrip ms;

private ToolStripMenuItem[][] tsmi;


private Label lblPlayList;

private ListBox lbx;

private PictureBox pbx;

private Label lblPosition;

private Label lblVolume;

private Button btnPlayStop;

private Button btnClose;

private Button btnVolumeDown;

private Button btnVolumeUp;


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

private const string DeviceID = "mysound";


private string filename = System.Environment.CurrentDirectory + @"\list.txt";


private List<string> lst;


private int position;

private int length;

private int volume; //1 to 5


private Timer tmr;


private enum MCIStatus

{

None,

Playing,

Stopped,

}


MCIStatus mst;


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

public MusicPlayer()

{

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

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

this.Text = "Music Player";


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

this.CreateMenu();


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

this.CreateCtrl();


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

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);

}


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

private void Form_Load( object sender, EventArgs e)

{

Console.WriteLine( "Form_Load");


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


this.LoadFileList();


this.position = 0;

this.length = 0;

this.volume = 3;


this.tmr = new Timer();

this.tmr.Interval = 1000;

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


this.mst = MCIStatus.None;


this.timer_Tick( null, null);

}

private void Form_Closed( object sender, EventArgs e)

{

Console.WriteLine( "Form_Closed");


this.SaveFileList();


this.mciStop();

this.mciClose();

}


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

private void LoadFileList()

{

if( !File.Exists( this.filename))

{

using( File.Create( this.filename))

{

}

}


using( StreamReader sr = new StreamReader( this.filename))

{

while( -1 < sr.Peek())

{

string temp = sr.ReadLine();

Console.WriteLine( temp);


this.lst.Add( temp);

this.lbx.Items.Add( Path.GetFileName( temp));

}

}

}


private void SaveFileList()

{

using( StreamWriter sw = new StreamWriter( this.filename, false))

{

for( int i = 0; i < this.lst.Count; i++)

{

sw.WriteLine( this.lst[i]);

}

}

}


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

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);

}


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

/* Menuの作成 */

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

private void CreateMenu()

{

this.ms = new MenuStrip();


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

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


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 = "List (&L)";

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

this.tsmi[0][2].Text = "Add (&A)";

this.tsmi[0][3].Text = "Remove (&R)";

this.tsmi[0][4].Text = "Clear (&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;

}


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

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

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

private void Menu_Click( object sender, EventArgs e)

{

Console.WriteLine( sender.ToString());


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

{

System.Diagnostics.Process.Start( "notepad.exe", this.filename);

}

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

{

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][3])

{

this.lbx_Remove();

}

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

{

this.lbx_Clear();

}

}


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

/* コントロールの作成 */

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

private void CreateCtrl()

{

this.lblPlayList = new Label();

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

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

this.lblPlayList.TextAlign = ContentAlignment.MiddleLeft;

this.lblPlayList.Text = "Play List";

//this.lblPlayList.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add( this.lblPlayList);


this.lbx = new ListBox();

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

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

this.lbx.HorizontalScrollbar = true;

this.lbx.DoubleClick += new EventHandler( this.lbx_DoubleClick);

this.Controls.Add( this.lbx);


this.pbx = new PictureBox();

this.pbx.SetBounds( 20, 240, 240, 20);

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

this.pbx.BorderStyle = BorderStyle.FixedSingle;

this.pbx.BackColor = Color.LightGray;

this.pbx.Paint += new PaintEventHandler( this.pbx_Paint);

this.pbx.MouseClick += new MouseEventHandler( this.pbx_MouseClick);

this.Controls.Add( this.pbx);

this.lblPosition = new Label();

this.lblPosition.SetBounds( 20, 270, 140, 30);

this.lblPosition.Anchor = AnchorStyles.Bottom;

this.lblPosition.TextAlign = ContentAlignment.MiddleLeft;

this.lblPosition.Text = "Position : ";

//this.lblPosition.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add( this.lblPosition);


this.lblVolume = new Label();

this.lblVolume.SetBounds( 160, 270, 100, 30);

this.lblVolume.Anchor = AnchorStyles.Bottom;

this.lblVolume.TextAlign = ContentAlignment.MiddleRight;

this.lblVolume.Text = "Volume : ";

//this.lblVolume.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add( this.lblVolume);


this.btnPlayStop = new Button();

this.btnPlayStop.SetBounds( 20, 300, 80, 40);

this.btnPlayStop.Anchor = AnchorStyles.Bottom;

this.btnPlayStop.Text = "Play";

this.btnPlayStop.Click += new EventHandler( this.btnPlayStop_Click);

this.Controls.Add( this.btnPlayStop);


this.btnClose = new Button();

this.btnClose.SetBounds( 100, 300, 60, 40);

this.btnClose.Anchor = AnchorStyles.Bottom;

this.btnClose.Text = "Close";

this.btnClose.Click += new EventHandler( this.btnClose_Click);

this.Controls.Add( this.btnClose);


this.btnVolumeDown = new Button();

this.btnVolumeDown.SetBounds( 160, 300, 50, 40);

this.btnVolumeDown.Anchor = AnchorStyles.Bottom;

this.btnVolumeDown.Text = "Vol.-";

this.btnVolumeDown.Click += new EventHandler( this.btnVolumeDown_Click);

this.Controls.Add( this.btnVolumeDown);


this.btnVolumeUp = new Button();

this.btnVolumeUp.SetBounds( 210, 300, 50, 40);

this.btnVolumeUp.Anchor = AnchorStyles.Bottom;

this.btnVolumeUp.Text = "Vol.+";

this.btnVolumeUp.Click += new EventHandler( this.btnVolumeUp_Click);

this.Controls.Add( this.btnVolumeUp);

}


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

/* Listの処理 */

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

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]));

}

}


private void lbx_Remove()

{

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

{

this.lst.RemoveAt( this.lbx.SelectedIndex);

this.lbx.Items.RemoveAt( this.lbx.SelectedIndex);

}

}


private void lbx_Clear()

{

this.lst.Clear();

this.lbx.Items.Clear();

}


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

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

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

private void btnPlayStop_Click( object sender, EventArgs e)

{

Console.WriteLine( "btnPlayStop_Click");


if( this.mst == MCIStatus.None)

{

this.OpenPlay();

}

else if( this.mst == MCIStatus.Playing)

{

this.mciStop();


this.mst = MCIStatus.Stopped;

this.btnPlayStop.Text = "Play";


this.tmr.Enabled = false;

this.timer_Tick( null, null);

}

else if( this.mst == MCIStatus.Stopped)

{

this.mciPlay();


this.mst = MCIStatus.Playing;

this.btnPlayStop.Text = "Stop";


this.tmr.Enabled = true;

this.timer_Tick( null, null);

}

}


private void btnClose_Click( object sender, EventArgs e)

{

Console.WriteLine( "btnClose_Click");

this.StopClose();

}


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

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

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

private void lbx_DoubleClick( object sender, EventArgs e)

{

Console.WriteLine( "lbx_DoubleClick");


this.StopClose();


this.OpenPlay();

}


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

/* Notify Event */

/* 再生中に、曲が終了して、次の曲があれば、再生する */

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

protected override void WndProc( ref Message m)

{

base.WndProc( ref m);


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

{

Console.WriteLine( "Notify " + this.mst.ToString());


if( this.mst == MCIStatus.None)

{

return;

}


this.position = this.mciPosition();


if( this.position < this.length)

{

return;

}


this.StopClose();


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

{

return;

}


this.lbx.SelectedIndex++;


this.OpenPlay();

}

}


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

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

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

private void pbx_MouseClick( object sender, MouseEventArgs e)

{

Console.WriteLine( "pbx_MouseClick");


if( this.mst == MCIStatus.None)

{

return;

}


if( 0 < this.pbx.Width)

{

Console.WriteLine( this.position);

this.position = this.length * e.X / this.pbx.Width;

Console.WriteLine( this.position);


this.mciSeek( this.position);


if( this.mst == MCIStatus.Playing)

{

this.mciPlay();

}


this.timer_Tick( null, null);

}

}


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

/* Volume */

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

private void btnVolumeDown_Click( object sender, EventArgs e)

{

Console.WriteLine( "btnVolumeDown_Click");


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


if( this.mst != MCIStatus.None)

{

this.mciSetVolume( this.volume);

}


this.timer_Tick( null, null);

}


private void btnVolumeUp_Click( object sender, EventArgs e)

{

Console.WriteLine( "btnVolumeUp_Click");


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


if( this.mst != MCIStatus.None)

{

this.mciSetVolume( this.volume);

}


this.timer_Tick( null, null);

}


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

/* タイマーイベント (表示の更新) */

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

private void timer_Tick( object sender, EventArgs e)

{

Console.WriteLine( "timer_tick");


if( this.mst != MCIStatus.None)

{

this.position = this.mciPosition();

}


this.pbx.Invalidate();


this.lblPosition.Text = "Pos : " + this.position.ToString() + " / " + this.length.ToString();

this.lblVolume.Text = "Vol : " + ( this.volume * this.volume * 40).ToString();

}


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

/* ピクチャーボックスの描画の更新 */

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

private void pbx_Paint( object sender, PaintEventArgs e)

{

Console.WriteLine( "pbx_Paint");


Graphics g = e.Graphics;


if( 0 < this.length)

{

g.FillRectangle( Brushes.Gray, 0, 0, this.pbx.Width * this.position / this.length, this.pbx.Height);

}

}


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

private void StopClose()

{

this.mciStop();

this.mciClose();


this.mst = MCIStatus.None;

this.btnPlayStop.Text = "Play";


this.tmr.Enabled = false;

this.timer_Tick( null, null);

}


private void OpenPlay()

{

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

{

return;

}


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

try

{

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

}

catch( Exception err)

{

MessageBox.Show( err.ToString());

return;

}


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

this.length = this.mciLength();

this.position = 0;


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

this.mciSetVolume( this.volume);


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

this.mciPlay();


this.mst = MCIStatus.Playing;

this.btnPlayStop.Text = "Stop";


this.tmr.Enabled = true;

this.timer_Tick( null, null);

}


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

/* MCI Functions */

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

private void mciOpen( string filename)

{

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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private void mciPlay()

{

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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private void mciStop()

{

string cmd = "stop " + MusicPlayer.DeviceID;

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private void mciClose()

{

string cmd = "close " + MusicPlayer.DeviceID;

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private void mciSetVolume( int v)

{

string cmd;

int ret;

StringBuilder retStr = new StringBuilder( 256);


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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());


cmd = "setaudio " + MusicPlayer.DeviceID + " volume to " + ( v * v * 40).ToString();

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());


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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private void mciSeek( int position)

{

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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

}


private int mciLength()

{

string cmd;

int ret;

StringBuilder retStr = new StringBuilder( 256);


cmd = "set " + MusicPlayer.DeviceID + " time format milliseconds";

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());


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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());


return Convert.ToInt32( retStr.ToString());

}


private int mciPosition()

{

StringBuilder retStr = new StringBuilder( 256);


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

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

Console.WriteLine( "cmd : " + cmd);

Console.WriteLine( "ret : " + ret.ToString());

return Convert.ToInt32( retStr.ToString());

}

}


私の給料を決めるもの

  昨年はずっと忙しくて、ブログを更新する気力が全然ありませんでした。今年も忙しそうなんですが、少しくらいは更新しようと思っています。


 閑話休題


 最近、給料を上げようみたいな話がされているみたいです。みんなが使えるお金が増えれば、消費が活発化して、景気が良くなる、という思惑です。

 で、ふと考えるのですが、サラリーマンである私の給料は何で決まっているでしょうか?

 普通なら、会社の業績と個人の仕事内容で給料は決まりそうですが、それ以上に影響する事柄がある気がします。それは税制です。

 会社員の税金は、源泉徴収で給料から引かれます。給料から引かれる税金は、給料の額によって段階的に変わるみたいです。源泉徴収票や年末調整の資料を見てみると、しきい値が複雑に決められていることが分かります。給料がしきい値を超えると、税金の計算方法が変わります。会社としては、税金の額が多くならないように給料を決めるでしょう。

 たぶん、複雑な税制は、給料を上がりにくくしている原因の一つだと思います。


 どちらかというと、私は忙しくてお金を使う時間がないので、給料より時間の余裕が欲しいです。