そうしのVC#2015日記

おっちゃんがC#始めてみたよ

toolStripにnumericUpDownを追加

VC#20005のツールストリップにはnumericUpDownがない。

たよりのdobon.netを探す

http://dobon.net/vb/dotnet/control/tstoolstripcontrolhost.html

これこれ、とりあえず言われた通りに書くと表示された。

なんだ簡単じゃんと思ったけど

private void Form1_Load{   }

内では見れるがそれ以外だと見えなくなってしまう。おまけに設定がビジュアルにできない。

http://dobon.net/vb/dotnet/control/tstoolstripcontrolhost.html

の下のほうを見ると

/一行でも書ける
toolStrip1.Items.Add(new ToolStripControlHost(new CheckBox));

とかいてある。

試行錯誤した結果

デザイン画面の適当な場所にnumericUpDownとかを張り付ける。(どうせツールストリップに移動してしまうのでどこでもよい)新しく張り付いたのがnumericUpDown7とすると

private void Form1_Load(object sender, EventArgs e)
{

  toolStrip5.Items.Add(new ToolStripControlHost(this.numericUpDown7));
  numericUpDown7.Width = 30;

 

とかするとできちゃった。addすると幅が変わっちゃうようで

numericUpDown7.Width = 30;

という行が入ってます。プログラムとしては2行、高級言語は便利ですね。

 

これでいいのかな? 

録音

VC#2015 デスクトップで音声録音をやろうと思ったのですが、簡単にできると思っていたのに意外と時間がかかったので覚え書き (^◇^)

基本的には

http://csharp-tricks-en.blogspot.jp/2010/09/record-sound-from-microphone.html

のサイトのパクリです。

起動引数に録音時間、ファイル名、フォルダーを指定

長いパスとかスペースの入ったフルパスファイル名は

mciSendString("save recsound " + ファイル名

で失敗するみたいです。細かいことは詳しい人に任せてカレントパスにファイル書き込み後ファイル移動にて対応(後ろ向き・・・)

最初は外部プログラムにしたけどCPU負荷が低かったので本体プログラムに取り込んでしまい不用品となりました。

実は音声→テキスト変換をやろうと思ってた(^◇^)

 

以下プログラム

public partial class Form1 : Form
{
[DllImport("winmm.dll")]
private extern static int mciSendString(string s1, StringBuilder s2, int i1, int i2);

public Form1()
{
InitializeComponent();
string[] cmds = System.Environment.GetCommandLineArgs();
int i = 0;
//コマンドライン引数を列挙する
foreach (string cmd in cmds)
{
Console.WriteLine(cmd);
if (i == 1) numericUpDown1.Value = int.Parse(cmds[1]);
if (i == 2) textBox1.Text = cmds[2];
if (i == 3) textBox2.Text = cmds[3];
i++;
}

if (textBox1.Text == "")
{
textBox1.Text = "test_Rec.wav";

if (System.IO.Directory.Exists(@"c:\sound") == false) System.IO.Directory.CreateDirectory(@"c:\sound");
}
if (textBox2.Text == "")textBox2.Text = @"c:\sound";

if (System.IO.Directory.Exists(textBox2.Text) == false) System.IO.Directory.CreateDirectory(textBox2.Text);

// タイマー生成
timer1.Tick += new EventHandler(this.OnTick_FormsTimer1);
timer1.Interval = 1000 * (int)numericUpDown1.Value;

rec_start();

// タイマーを開始
timer1.Start();

}

public void OnTick_FormsTimer1(object sender, EventArgs e)
{
button2_Click(sender, e);
timer1.Stop();// タイマーを停止
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
rec_start();
}

private void rec_start()
{ // 録音開始
button1.Enabled = false;
button1.Text = "録音中";
button1.BackColor = Color.Red;
mciSendString("open new Type waveaudio Alias recsound", null, 0, 0);
mciSendString("record recsound", null, 0, 0);
}

private void button2_Click(object sender, EventArgs e)
{ // 録音停止

string fileName;// = @"C:\Users\maki\Desktop\test_Rec.wav";
fileName = textBox1.Text;// フルパスで記載できるが長い名前、スペースがあると失敗するみたい。パスがないとカレントフォルダーに作る
//fileName= "test_Rec.wav";
mciSendString("save recsound " + fileName , null, 0, 0);
mciSendString("close recsound", null, 0, 0);
System.Threading.Thread.Sleep(500);
if(System.IO.File.Exists(textBox2.Text + "\\" + fileName) == false)System.IO.File.Move(fileName, textBox2.Text+"\\"+ fileName);

}

VC#2015覚え書き

VC#でプログラム作っている時の覚え書き(初心者マーク付き)

DOUBLEを配列におとして表示 配列をDOUBLE形式に戻す

USHORTを配列におとして表示 配列をUSHORT形式に戻す

 

画面はこんな感じ 

f:id:ksssd008:20170713175359p:plain

プログラムはこんな感じ(追加したのは赤文字)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public byte barr = new byte[8];


public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
double d = double.Parse(textBox1.Text);
barr = System.BitConverter.GetBytes(d);
string sarr = (from a in barr select a.ToString("X2")).Reverse().ToArray();
textBox2.Text = string.Join("", sarr);
}

private void button2_Click(object sender, EventArgs e)
{
double d = BitConverter.ToDouble(barr, 0);
textBox3.Text = d.ToString();
}

private void button3_Click(object sender, EventArgs e)
{
ushort ui = ushort.Parse(textBox1.Text);
barr = System.BitConverter.GetBytes(ui);
string[] sarr = (from a in barr select a.ToString("X2")).Reverse().ToArray();
textBox2.Text = string.Join("", sarr);
}

private void button4_Click(object sender, EventArgs e)
{
ushort ui = BitConverter.ToUInt16(barr, 0);
textBox3.Text = ui.ToString();
}
}

}

用途 RS-232C通信でDOUBLE型を8バイトにUSHORT型を2バイトに保存して呼び出して元に戻すために使用

対象機器

ロジカルプロダクト社 ワイヤレス8chロガー用

DOUBLE 外部機器固有設定(8バイトx8ch=64バイト)

ushort 補正値キー(2バイトx8ch=16バイト)

 

いろいろ試してるが 外部機器固有設定は使用できないみたい*1

*1:+_+