開発環境 Microsoft Visual Studio Express 2013 for Windows Desktop
実行環境 Windows 8.1 (64bit)
プロジェクト Visual C#/Windows フォーム アプリケーション
名前 mcirec

Program.cs
using System;
using System.Windows.Forms;
 
namespace mcirec
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 

Form1.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
 
namespace mcirec
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        private extern static uint mciSendString(
            string lpszCommand, StringBuilder lpszReturnString, uint cchReturn, IntPtr hwndCallback);
 
        private bool rec;
 
        public Form1()
        {
            InitializeComponent();
 
            setRec(false);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (rec)
            {
                closeRec();
                setRec(false);
            }
            else
            {
                openRec();
                setRec(true);
            }
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (rec)
            {
                closeRec();
            }
        }
 
        private void setRec(bool b)
        {
            button1.Text = b ? "■停止" : "●録音";
            rec = b;
        }
 
        private void openRec()
        {
            string set1 = "channels 1 samplespersec 8000 bytespersec 8000 alignment 1 bitspersample 8";
            mciSendString("open new type waveaudio alias Rec", null, 0, IntPtr.Zero);
            mciSendString("set Rec " + set1, null, 0, IntPtr.Zero);
            mciSendString("record Rec", null, 0, IntPtr.Zero);
        }
 
        private void closeRec()
        {
            string fileName = textBox1.Text;
            mciSendString("stop Rec", null, 0, IntPtr.Zero);
            mciSendString("save Rec " + fileName, null, 0, IntPtr.Zero);
            mciSendString("close Rec", null, 0, IntPtr.Zero);
        }
    }
}
 

Form1.Designer.cs
namespace mcirec
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows フォーム デザイナーで生成されたコード
 
        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(57, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "ファイル名:";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(75, 6);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(197, 19);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "sample.wav";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(75, 31);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 61);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "mcirec";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
    }
}
 
最終更新:2014年09月24日 18:06