|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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; using System.Data.OleDb; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { // DataTableはテーブルキャッシュで、そのコレクションがDataSet。 // DataReader/Adapter // DataReaderはループで取得し、Read()されるまで読み込まれないため軽い。 // DataAdapterはFill()で取得してDataSetに全て入る。(da -> ds) // DataAdapterへの更新はUpdate()で処理。 // 実験 // create table tb([ID] autoincrement, [age] int); OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + @"\db.mdb" + ";"); //con.Open(); //ここのOpen()は不要。Open()するならClose()必要。 //Open()すると接続型となる。 //データアダプタの作成/データセットの作成 OleDbDataAdapter da = new OleDbDataAdapter("select * from tb", con); DataSet ds = new DataSet(); //これがないとUpdate()でエラー new OleDbCommandBuilder(da); da.Fill(ds); //新しい行の作成 DataRow dr = ds.Tables[0].NewRow(); dr[1] = 10; ds.Tables[0].Rows.Add(dr); //データグリッドビューを作成し、データソースを設定 DataGridView d = new DataGridView(); d.DataSource = ds.Tables[0]; Controls.Add(d); //DataAdapterのUpdate()を実行。 Button btn = new Button(); btn.Top = Height - 100; Controls.Add(btn); btn.Click += new EventHandler((object sender, EventArgs e) => { da.Update(ds.Tables[0]);//新しい行が追加されているのでUpdate()ではInsertが実行される。 }); //con.Close(); //InitializeComponent(); } } } |
C# 在庫管理
以前にも数回同じようなソフトを作っているが、今回は
表を並べて表示するタイプ。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
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; using System.Data.OleDb; using System.IO; namespace 預かり在庫管理ver2 { public partial class Form1 : Form { public void dgv1_setup() { string[] header = new string[] { "ID", "登録日", "伝票番号", "得意先", "仕入先", "品目名", "PO", "INV", "数量", "備考" }; foreach (string h in header) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = h; dataGridView1.Columns.Add(col); } for (int i = 1; i <= 2; i++) { DataGridViewButtonColumn col_btn = new DataGridViewButtonColumn(); col_btn.HeaderText = " "; dataGridView1.Columns.Add(col_btn); } dataGridView1.Columns[0].Visible = false; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.ReadOnly = true; dataGridView1.AllowUserToAddRows = false; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.RowTemplate.Height = 28; dataGridView1.Font = new Font("Meiryo UI", 9); dataGridView1.MultiSelect = false; dataGridView1.CellClick += new DataGridViewCellEventHandler(cell_click); } public void dgv2_setup() { string[] header = new string[] { "ID", "出庫日", "出庫数" }; foreach (string h in header) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.Name = h; col.HeaderText = h; dataGridView2.Columns.Add(col); } DataGridViewButtonColumn col_btn = new DataGridViewButtonColumn(); col_btn.HeaderText = " "; dataGridView2.Columns.Add(col_btn); dataGridView2.Columns[0].Visible = false; dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView2.ReadOnly = true; dataGridView2.AllowUserToAddRows = false; dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView2.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView2.RowTemplate.Height = 28; dataGridView2.Font = new Font("Meiryo UI", 9); dataGridView2.MultiSelect = false; dataGridView2.CellClick += new DataGridViewCellEventHandler(sub_cell_click); } public Form1() { InitializeComponent(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd1 = new OleDbCommand("select distinct customer_name from orders;", con); using (OleDbDataReader dr = cmd1.ExecuteReader()) { while (dr.Read()) { comboBox1.Items.Add(dr["customer_name"].ToString()); } } OleDbCommand cmd2 = new OleDbCommand("select distinct supplier_name from orders;", con); using (OleDbDataReader dr = cmd2.ExecuteReader()) { while (dr.Read()) { comboBox2.Items.Add(dr["supplier_name"].ToString()); } } OleDbCommand cmd3 = new OleDbCommand("select distinct item_name from orders;", con); using (OleDbDataReader dr = cmd3.ExecuteReader()) { while (dr.Read()) { comboBox3.Items.Add(dr["item_name"].ToString()); } } } catch (OleDbException e) { foreach (Control c in this.Controls) { c.Enabled = false; } MessageBox.Show(e.Message); return; } finally { con.Close(); } } textBox1.TextChanged += new EventHandler((object sender, EventArgs e) => { button1.Text = textBox1.Text == "" ? "登録" : "更新"; }); dgv1_setup(); dgv2_setup(); dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); } private string rp(string s) { s = s.Replace("\'", "’"); s = s.Replace("\"", "”"); return s; } private void sub_cell_click(object sender, DataGridViewCellEventArgs e) { int btn_ci = 3; int ri = e.RowIndex; int ci = e.ColumnIndex; if (ri == -1 || ci == -1) return; string id = dataGridView2.Rows[ri].Cells[0].Value.ToString(); if (ci == btn_ci) { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from shipments where shipments_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } sub_dgv_load(textBox1.Text); } } private void cell_click(object sender, DataGridViewCellEventArgs e) { int btn1_ci = 10; int btn2_ci = 11; int btn3_ci = 6; int ri = e.RowIndex; int ci = e.ColumnIndex; if (ri == -1 || ci == -1) return; string id = dataGridView1.Rows[ri].Cells[0].Value.ToString(); string po = dataGridView1.Rows[ri].Cells[6].Value.ToString(); string btn_cap = dataGridView1.Rows[ri].Cells[btn1_ci].Value.ToString(); if (ci == btn1_ci) { rows_change(id, btn_cap); dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); return; } else if (ci == btn2_ci) { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from orders where orders_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); return; } else if (ci == btn3_ci) { open_Folder(po); return; } textBox1.Text = dataGridView1.Rows[ri].Cells[0].Value.ToString(); textBox8.Text = dataGridView1.Rows[ri].Cells[1].Value.ToString(); textBox2.Text = dataGridView1.Rows[ri].Cells[2].Value.ToString(); comboBox1.Text = dataGridView1.Rows[ri].Cells[3].Value.ToString(); comboBox2.Text = dataGridView1.Rows[ri].Cells[4].Value.ToString(); comboBox3.Text = dataGridView1.Rows[ri].Cells[5].Value.ToString(); textBox5.Text = dataGridView1.Rows[ri].Cells[6].Value.ToString(); textBox6.Text = dataGridView1.Rows[ri].Cells[7].Value.ToString(); textBox9.Text = dataGridView1.Rows[ri].Cells[8].Value.ToString(); textBox7.Text = dataGridView1.Rows[ri].Cells[9].Value.ToString(); sub_dgv_load(id); } private void sub_dgv_load(string id) { dataGridView2.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("select * from shipments where orders_id = " + id + ";", con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { dataGridView2.Rows.Add( dr["shipments_id"].ToString(), DateTime.Parse(dr["ship_date"].ToString()).ToString("yyyy/MM/dd"), dr["quantity"].ToString(), "削除"); } } } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } int stock_count = 0; for (int r = 0; r < dataGridView2.Rows.Count; r++) { stock_count += int.Parse(dataGridView2.Rows[r].Cells[2].Value.ToString()); } textBox12.Text = stock_count.ToString(); } private void rows_change(string id,string btn_cap) { string tmp = (btn_cap == "完了") ? "hide" : "show"; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("update orders set visible = '" + tmp + "' where orders_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } } private void form_clear() { textBox1.Text = ""; textBox8.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox2.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox9.Text = ""; textBox7.Text = ""; textBox14.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox12.Text = ""; textBox10.Text = ""; dataGridView2.Rows.Clear(); } private bool form_validate() { if (textBox8.Text == "" | textBox2.Text == "" | comboBox1.Text == "" | comboBox2.Text == "" | comboBox3.Text == "" | textBox5.Text == "" | textBox6.Text == "" | textBox9.Text == "") { MessageBox.Show("入力が不足しています。"); return false; } try { DateTime.Parse(textBox8.Text); } catch { MessageBox.Show("日付が正しくありません。"); return false; } try { int.Parse(textBox9.Text); } catch { MessageBox.Show("数量が正しくありません。"); return false; } textBox2.Text = rp(textBox2.Text); comboBox1.Text = rp(comboBox1.Text); comboBox2.Text = rp(comboBox2.Text); comboBox3.Text = rp(comboBox3.Text); textBox5.Text = rp(textBox5.Text); textBox6.Text = rp(textBox6.Text); textBox7.Text = rp(textBox7.Text); return true; } private bool change_tbl() { string query; if (button1.Text == "登録") { query = "insert into orders (register_date,order_no,customer_name,supplier_name,item_name,po_no,inv_no,quantity,notes,visible) values (" + "'" + textBox8.Text + "'," + "'" + textBox2.Text + "'," + "'" + comboBox1.Text + "'," + "'" + comboBox2.Text + "'," + "'" + comboBox3.Text + "'," + "'" + textBox5.Text + "'," + "'" + textBox6.Text + "'," + "" + textBox9.Text + "," + "'" + textBox7.Text + "','show');"; } else { query = "update orders set " + "register_date='" + textBox8.Text + "'," + "order_no='" + textBox2.Text + "'," + "customer_name='" + comboBox1.Text + "'," + "supplier_name='" + comboBox2.Text + "'," + "item_name='" + comboBox3.Text + "'," + "po_no='" + textBox5.Text + "'," + "inv_no='" + textBox6.Text + "'," + "quantity=" + textBox9.Text + "," + "notes='" + textBox7.Text + "'" + "where orders_id = " + textBox1.Text + ";"; } using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query, con); cmd.ExecuteNonQuery(); } catch (OleDbException e) { MessageBox.Show(e.Message); return false; } finally { con.Close(); } } return true; } private void dgv_load(string query,string btn_cap) { dataGridView1.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query,con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { dataGridView1.Rows.Add( dr["orders_id"].ToString(), DateTime.Parse( dr["register_date"].ToString()).ToString("yyyy/MM/dd"), dr["order_no"].ToString(), dr["customer_name"].ToString(), dr["supplier_name"].ToString(), dr["item_name"].ToString(), dr["po_no"].ToString(), dr["inv_no"].ToString(), dr["quantity"].ToString(), dr["notes"].ToString(), btn_cap,"削除"); } } } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } if (btn_cap == "完了") { button6.Enabled = false; button5.Enabled = true; } if (btn_cap == "戻入") { button6.Enabled = true; button5.Enabled = false; } } private void button1_Click(object sender, EventArgs e) { if (!form_validate()) return; if (!change_tbl()) return; if (button1.Text == "登録") { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (button1.Text == "更新" && button5.Enabled == true) { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (button1.Text == "更新" && button5.Enabled == false) { dgv_load("select * from orders where visible = 'hide';", "戻入"); } form_clear(); MessageBox.Show("完了しました。"); } private void button3_Click(object sender, EventArgs e) { form_clear(); } private bool sub_form_validate() { if (textBox14.Text == "" | textBox13.Text == "") { MessageBox.Show("入力が不足しています。"); return false; } try { DateTime.Parse(textBox14.Text); } catch { MessageBox.Show("日付が正しくありません。"); return false; } try { int.Parse(textBox13.Text); } catch { MessageBox.Show("数量が正しくありません。"); return false; } return true; } private void button4_Click(object sender, EventArgs e) { if (!form_validate()) return; if (!sub_form_validate()) return; if (textBox1.Text == "") return; string query = "insert into shipments (orders_id,ship_date,quantity) values " + "(" + textBox1.Text + ",'" + textBox14.Text + "'," + textBox13.Text + ");"; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query, con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } sub_dgv_load(textBox1.Text); textBox14.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox13.Text = ""; } private void button6_Click(object sender, EventArgs e) { dgv_load("select * from orders where visible <> 'hide';","完了"); form_clear(); } private void button5_Click_1(object sender, EventArgs e) { dgv_load("select * from orders where visible = 'hide';","戻入"); form_clear(); } private void button2_Click(object sender, EventArgs e) { string q = ""; textBox10.Text = rp(textBox10.Text); if (textBox10.Text == "" && button5.Enabled == true) { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (textBox10.Text == "" && button5.Enabled == false) { dgv_load("select * from orders where visible = 'hide';", "戻入"); } else if (textBox10.Text != "" && button5.Enabled == true) { q = "select * from orders where visible <> 'hide' and " + "(order_no like '%" + textBox10.Text + "%' or " + "customer_name like '%" + textBox10.Text + "%' or " + "supplier_name like '%" + textBox10.Text + "%' or " + "item_name like '%" + textBox10.Text + "%' or " + "po_no like '%" + textBox10.Text + "%' or " + "inv_no like '%" + textBox10.Text + "%' or " + "notes like '%" + textBox10.Text + "%')"; dgv_load(q, "完了"); } else if (textBox10.Text != "" && button5.Enabled == false) { q = "select * from orders where visible = 'hide' and " + "(order_no like '%" + textBox10.Text + "%' or " + "customer_name like '%" + textBox10.Text + "%' or " + "supplier_name like '%" + textBox10.Text + "%' or " + "item_name like '%" + textBox10.Text + "%' or " + "po_no like '%" + textBox10.Text + "%' or " + "inv_no like '%" + textBox10.Text + "%' or " + "notes like '%" + textBox10.Text + "%')"; dgv_load(q, "戻入"); } } void open_Folder(string po) { string path = ""; try { using (StreamReader sr = new StreamReader(Application.StartupPath + @"\setting.txt", Encoding.GetEncoding("shift_jis"))) { path = sr.ReadLine() + @"\" + po; if (Directory.Exists(path)) { System.Diagnostics.Process.Start(path); } else { DialogResult yn = MessageBox.Show("フォルダを作成します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; Directory.CreateDirectory(path); System.Diagnostics.Process.Start(path); } } } catch (Exception e) { MessageBox.Show(e.Message); } } } } |
Windows SMB1.0無効化
古いNASが繋がらなくなる場合あるので注意。
・現在の状態を確認
sc.exe qc lanmanworkstation
にて確認できる。
DEPENDENCIES
に
MRxSmb10
があるとSMB1.0が有効な状態
・SMB1.0無効化
コントロール>プログラムと機能>Windowsの機能の有効化または無効化
SMB1.0/CIFSファイル共有のサポート
のチェックを外す。
(SMB 1.0/CIFSクライアントだけチェックがあればアクセスすることはできる)
・どのバージョンで接続しているか
管理者のPowerShellから、
get-smbconnection
を実行。
C# 指定秒数でサムネイル作成②
作ってみたら意外と使えそうだったので、少々機能追加。
読み込みの処理を非同期にして固まらないように。
タブに進捗を表示。
タブに読み込んだパスを記入。
タブのダブルクリックでタブ削除。
サムネイル左ダブルクリックで標準プログラムを起動。
サムネイル右ダブルクリックでエクスプローラ起動。
検索機能追加。
ファイルの親フォルダの名前を記入。
指定秒数より短いファイルもエラーではなく透明の画像とした。
サムネイルの取得にffmpegを使っているので、実行ファイルと同じ場所にffmpeg.exeが必要。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
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 themenail { public partial class Form1 : Form { public Form1() { InitializeComponent(); tabControl1.Font = new Font("Meiryo UI", 11); tabControl1.MouseDoubleClick += new MouseEventHandler(tpDbClick); this.FormClosed += new FormClosedEventHandler((object sender, FormClosedEventArgs e) => { foreach (System.Diagnostics.Process p in ProcessList) { try { if (!p.HasExited) p.Kill(); } catch { } } }); } private List<System.Diagnostics.Process> ProcessList = new List<System.Diagnostics.Process>(); private async void BtnClick() { List<string> path = new List<string>(); foreach (string f in System.IO.Directory.GetFiles(textBox2.Text)) { path.Add(f); } foreach (string d in System.IO.Directory.GetDirectories(textBox2.Text,"*",System.IO.SearchOption.AllDirectories)) { foreach (string f in System.IO.Directory.GetFiles(d,"*.mp4")) { path.Add(f); } } string ss = textBox1.Text; string[] files = path.ToArray(); TabPage tp = new TabPage(); tp.Text = "開始中..."; tabControl1.Controls.Add(tp); tabControl1.SelectedTab = tp; Label lb = new Label(); lb.Font = new Font("Meiryo UI", 11); lb.Text = textBox2.Text; ImageList il = new ImageList(); il.ImageSize = new Size(255, 255); il.ColorDepth = ColorDepth.Depth24Bit; ListView lv = new ListView(); System.Diagnostics.Process p = new System.Diagnostics.Process(); ProcessList.Add(p); await Task.Run(() => { for (int i = 0; i < files.Length; i++) { try { Invoke((Action)(() => { tp.Text = i.ToString() + "/" + files.Length.ToString(); })); } catch (System.ObjectDisposedException) { return; } System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo( Application.StartupPath + @"\ffmpeg.exe", "-ss " + ss + " -i \"" + files[i] + "\" -vframes 1 -f image2 pipe:1"); ps.RedirectStandardOutput = true; ps.CreateNoWindow = true; ps.UseShellExecute = false; p.StartInfo = ps; try { p.Start(); } catch (System.ComponentModel.Win32Exception e) { MessageBox.Show(e.Message); return; } Image img; try { img = Image.FromStream(p.StandardOutput.BaseStream); } catch (System.ArgumentException) { img = new Bitmap(255, 255); } Image theme = img.GetThumbnailImage(255, 255, null, IntPtr.Zero); il.Images.Add(theme); string parent = System.IO.Directory.GetParent(files[i]).Name; ListViewItem lvi = lv.Items.Add("(" + parent + ")" + System.IO.Path.GetFileName(files[i]), i); lvi.SubItems.Add(files[i]); } }); lv.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; lv.Size = new Size(tp.Size.Width - 2, tp.Height - 1 - 25); lv.Top = 25; lv.LargeImageList = il; lv.MouseDoubleClick += new MouseEventHandler(lvDbClick); lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; lb.Top = 2; lb.Left = 2; lb.Width = lv.Width; tp.Controls.Add(lb); tp.Controls.Add(lv); tp.Text = "完了"; } private void lvDbClick(object sender, MouseEventArgs e) { string path = ((ListView)sender).SelectedItems[0].SubItems[1].Text; string file = ((ListView)sender).SelectedItems[0].Text; if (!System.IO.File.Exists(path)) return; if (e.Button == MouseButtons.Right) { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "explorer"; p.StartInfo.Arguments = "/select," + path; p.Start(); } if (e.Button == MouseButtons.Left) { System.Diagnostics.Process p = System.Diagnostics.Process.Start(path); } } private void tpDbClick(object sender, MouseEventArgs e) { tabControl1.TabPages.Remove(((TabControl)sender).SelectedTab); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "") return; if (!System.IO.Directory.Exists(textBox2.Text)) return; BtnClick(); } List<ListView> TempListView = new List<ListView>(); private void button2_Click(object sender, EventArgs e) { if (tabControl1.TabCount == 0) return; if (tabControl1.SelectedTab.Text != "完了") return; if (tabControl1.SelectedTab.Name == "") { string guid = System.Guid.NewGuid().ToString(); tabControl1.SelectedTab.Name = guid; ListView lv = new ListView(); lv.Name = guid; int r = 0; foreach (ListViewItem i in ((ListView)tabControl1.SelectedTab.Controls[1]).Items) { lv.Items.Add(""); lv.Items[r] = (ListViewItem)i.Clone(); r++; } TempListView.Add(lv); } if (tabControl1.SelectedTab.Name != "") { foreach (ListView i in TempListView) { if (i.Name == tabControl1.SelectedTab.Name) { ((ListView)tabControl1.SelectedTab.Controls[1]).Items.Clear(); int r = 0; foreach (ListViewItem k in i.Items) { ((ListView)tabControl1.SelectedTab.Controls[1]).Items.Add(""); ((ListView)tabControl1.SelectedTab.Controls[1]).Items[r] = (ListViewItem)k.Clone(); r++; } } } } if (textBox3.Text != "") { foreach (ListViewItem i in ((ListView)tabControl1.SelectedTab.Controls[1]).Items) { if (i.Text.IndexOf(textBox3.Text,StringComparison.OrdinalIgnoreCase) <= 0) i.Remove(); } } } private void button3_Click(object sender, EventArgs e) { foreach (TabPage t in tabControl1.TabPages) { tabControl1.SelectedTab = t; button2.PerformClick(); } } } } |
C# 指定秒数でサムネイル作成①
特定フォルダ以下のmp4のサムネイルを指定秒数で取得したくなった。取得をするたびに新しいタブページを追加する。ダブルクリックすると、explorerで開く。
実行ファイルと同じ場所にffmpeg.exeが必要。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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 themenail { public partial class Form1 : Form { public Form1() { InitializeComponent(); toolStripProgressBar1.Visible = false; } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "") return; toolStripProgressBar1.Visible = true; string path = textBox2.Text; string[] files; TabPage tp = new TabPage(); ListView lv = new ListView(); ImageList il = new ImageList(); il.ImageSize = new Size(255, 255); il.ColorDepth = ColorDepth.Depth32Bit; lv.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; lv.Size = new Size(tp.Size.Width-2,tp.Height-3); lv.Top = 2; lv.LargeImageList = il; lv.MouseDoubleClick += new MouseEventHandler(DbClick); tp.Controls.Add(lv); tabControl1.Controls.Add(tp); tabControl1.SelectedTab = tp; try { files = System.IO.Directory.GetFiles(path, "*.mp4",System.IO.SearchOption.AllDirectories); toolStripProgressBar1.Minimum = 0; toolStripProgressBar1.Maximum = files.Length; for (int i = 0; i < files.Length; i++) { Application.DoEvents(); toolStripProgressBar1.Value = i; System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo( Application.StartupPath + @"\ffmpeg.exe", "-ss " + textBox1.Text + " -i \"" + files[i] + "\" -vframes 1 -f image2 pipe:1"); ps.RedirectStandardOutput = true; ps.CreateNoWindow = true; ps.UseShellExecute = false; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = ps; p.Start(); Image img = Image.FromStream(p.StandardOutput.BaseStream); Image theme = img.GetThumbnailImage(255, 255, null, IntPtr.Zero); il.Images.Add(theme); lv.Items.Add(files[i], i); } } catch (Exception ex) { MessageBox.Show(ex.Message); } toolStripProgressBar1.Visible = false; MessageBox.Show("完了"); } private void DbClick(object sender, MouseEventArgs e) { string path = ((ListView)sender).SelectedItems[0].Text; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "explorer"; p.StartInfo.Arguments = "/select," + path; p.Start(); } } } |
C# 画像サムネイルを取得
標準サムネイルをストリーム経由で取得してListViewに表示。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
imageList1.ImageSize = new Size(256, 256); imageList1.ColorDepth = ColorDepth.Depth24Bit; listView1.LargeImageList = imageList1; string path = textBox2.Text; string[] files; files = System.IO.Directory.GetFiles(path,"*.jpg"); for(int i = 0; i < files.Length; i++) { using (System.IO.FileStream fs = System.IO.File.OpenRead(files[i])) { Image img = Image.FromStream(fs,false,false); Image theme = img.GetThumbnailImage(200, 200, delegate { return false; }, IntPtr.Zero); imageList1.Images.Add(theme); listView1.Items.Add(files[i],i); } } |
ffmpegでサムネイルを取得するタイプ。画像にせず標準出力経由。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//ffmpegから直接画像として保存 string path = ""; string[] files = System.IO.Directory.GetFiles(path, "*.mp4"); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = Application.StartupPath + @"\ffmpeg.exe"; p.StartInfo.Arguments = "-ss 50 -i \"" + files[0] + "\" -vframes 1 -f image2 tmp1.jpg"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.Start(); //一旦Imageオブジェクトにしてから画像として保存 string path = ""; string[] files = System.IO.Directory.GetFiles(path, "*.mp4"); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = Application.StartupPath + @"\ffmpeg.exe"; p.StartInfo.Arguments = "-ss 50 -i \"" + files[0] + "\" -vframes 1 -f image2 pipe:1"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.Start(); Image img = Image.FromStream(p.StandardOutput.BaseStream); img.Save(Application.StartupPath + @"\tmp2.jpg"); |
C# フォーム並べて追随させる
あまり使い道はないが、たまにこういう挙動を見かけることもある。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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 Test { public partial class Form1 : Form { Form f = null; public Form1() { InitializeComponent(); f = new Form2(); f.FormClosed += new FormClosedEventHandler((object sender,FormClosedEventArgs e)=> { this.Close(); }); f.Shown += new EventHandler((object sender, EventArgs e) => { f.Left = this.Left + this.Width; f.Top = this.Top; f.Height = this.Height; f.Width = this.Width; }); f.Show(); } private void Form1_ResizeEnd(object sender, EventArgs e) { f.Left = this.Left + this.Width; f.Top = this.Top; f.Height = this.Height; f.Width = this.Width; } private void button1_Click(object sender, EventArgs e) { ((Button)f.Controls["button1"]).Text = "hello world"; } } } |
C# テキストファイル集計
テキストファイルを正規表現で集計する。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
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; using System.IO; using System.Text.RegularExpressions; namespace LineRegex { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.AllowDrop = true; } private void listBox1_DragDrop(object sender, DragEventArgs e) { listBox1.Items.AddRange((string[])e.Data.GetData(DataFormats.FileDrop, false)); } private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; } private void button1_Click(object sender, EventArgs e) { dataGridView1.Rows.Clear(); DataTable dt = new DataTable("tbl"); dt.Columns.Add("日付"); dt.Columns.Add("番号"); dt.Columns.Add("数量"); dt.Columns.Add("金額"); dt.Columns.Add("名称"); DataSet ds = new DataSet(); ds.Tables.Add(dt); foreach (string s in listBox1.Items) { using (StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("shift_jis"))) { string r_d = ""; string r_j = ""; List<string[]> r_j_list = new List<string[]>(); string r_q = ""; string r_a = ""; string r_n = ""; while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (new Regex("--------------------------------").IsMatch(ln)) { r_d = ""; r_j = ""; r_j_list.Clear(); r_q = ""; r_a = ""; r_n = ""; } else if (new Regex(@"^\d{4}年\d{2}月\d{2}日").IsMatch(ln)) { Match m = new Regex(@"^\d{4}年\d{2}月\d{2}日").Match(ln); r_d = m.Groups[0].Value; } else if (new Regex(@"^\d+\s{1}JAN").IsMatch(ln)) { Match m = new Regex(@"^(\d+)\s{1}JAN").Match(ln); r_j = m.Groups[1].Value; } else if (new Regex(@"^\s+[0-9]+コ").IsMatch(ln)) { Match m = new Regex(@"^\s+([0-9]+)コ").Match(ln); r_q = m.Groups[1].Value; } else if (new Regex(@"^\s{1}[1-9]+.+\\[0-9,]+$").IsMatch(ln)) { Match m = new Regex(@"^\s{1}[1-9]+.+\\([0-9,]+)$").Match(ln); r_a = m.Groups[1].Value; if (r_q == "") r_q = "1"; if (r_j != "") { r_j_list.Add(new string[] { r_j, r_q, r_a }); r_j = ""; r_q = ""; r_a = ""; } } else if (new Regex(@"^\s+\S+\s+様").IsMatch(ln)) { Match m = new Regex(@"^\s+(\S+)\s+様").Match(ln); r_n = m.Groups[1].Value; foreach (string[] j in r_j_list) { DataRow dr = ds.Tables["tbl"].NewRow(); dr["日付"] = r_d; dr["番号"] = j[0]; dr["数量"] = j[1]; dr["金額"] = j[2]; dr["名称"] = r_n; ds.Tables["tbl"].Rows.Add(dr); } } } } } dataGridView1.DataSource = ds.Tables["tbl"]; } } } |
VBA 条件付き書式
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Sub CellsCondition() ' 条件付書式クリア Set w = Sheets("11月") 'w.Cells.FormatConditions.Delete w.Columns("J").FormatConditions.Delete ' 最終行取得 'b = w.UsedRange.Rows.Count b = 324 For r = 2 To b For c = 10 To 10 ' J列のみ ' A列の値(Weekday)が1なら赤 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=Weekday(A" & r & ")=1") 'f.Interior.Color = RGB(255, 200, 200) 'f.StopIfTrue = False ' A列の値(Weekday)が7なら青(複数条件) 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=AND(B" & r & "=0,Weekday(A" & r & ")=7)") 'f.Interior.Color = RGB(200, 200, 255) 'f.StopIfTrue = False ' 重複チェック+特定の単語を除外 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=AND(COUNTIF(J2:J324,J" & r & ")>1,J" & r & "<>""シコミ"")") 'f.Interior.Color = RGB(255, 0, 0) 'f.StopIfTrue = False ' A列の値が1なら書式設定 'Set f = w.Cells(r, 1).FormatConditions.Add(xlExpression, xlEqual, "=Day(A" & r & ")=1") 'f.NumberFormat = "mm/dd(aaa)" 'f.StopIfTrue = False Next c Next r End Sub |
C# FFmpegにコマンド投げる②
ちょっと動画の長さを調整したいとき用。
実行ファイルと同じ場所にffmpeg.exeを置く。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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; using System.Runtime.InteropServices; using QuartzTypeLib; namespace FFMPEG_UI { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.AllowDrop = true; textBox1.Text = "00"; textBox2.Text = "00"; textBox3.Text = "00"; textBox4.Text = "000"; textBox5.Text = "00"; textBox6.Text = "00"; textBox7.Text = "00"; textBox8.Text = "000"; } private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; } private void listBox1_DragDrop(object sender, DragEventArgs e) { listBox1.Items.AddRange((string[])e.Data.GetData(DataFormats.FileDrop, false)); } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); textBox1.Text = "00"; textBox2.Text = "00"; textBox3.Text = "00"; textBox4.Text = "000"; textBox5.Text = "00"; textBox6.Text = "00"; textBox7.Text = "00"; textBox8.Text = "000"; } private void button1_Click(object sender, EventArgs e) { string s_h = textBox1.Text; string s_m = textBox2.Text; string s_s = textBox3.Text; string s_ms = textBox4.Text; string e_h = textBox5.Text; string e_m = textBox6.Text; string e_s = textBox7.Text; string e_ms = textBox8.Text; TimeSpan start_position = new TimeSpan(0, int.Parse(s_h), int.Parse(s_m), int.Parse(s_s), int.Parse(s_ms)); TimeSpan end_position = new TimeSpan(0, int.Parse(e_h), int.Parse(e_m), int.Parse(e_s), int.Parse(e_ms)); string interval = (end_position - start_position).TotalSeconds.ToString(); foreach (string s in listBox1.Items) { string filePath = System.IO.Path.GetDirectoryName(s) + @"\out_" + System.IO.Path.GetFileName(s); System.Diagnostics.Process p = System.Diagnostics.Process.Start( Application.StartupPath + @"\ffmpeg.exe" , "-ss " + start_position.TotalSeconds.ToString() + " -i \"" + s.ToString() + "\" -t " + interval + " \"" + filePath + "\""); p.WaitForExit(); } } } } |