2013年3月16日土曜日
Pictures Slide Viewer + Un Zip を公開
指定したフォルダ内の画像ファイルを閲覧、スライドショー表示するソフトです。
Zip ファイルを解凍する機能を追加しました。
使い方
追加 : フォルダーを指定すると、指定したフォルダーとそのサブフォルダー内の
画像ファイル(JPG,GIF,BMP,PNG)が再生リストに追加せれます。
全削除 : 再生リストから全項目が削除されます。ファイル自体は削除されません。
再生リストの項目クリック : クリックした項目が選択され、画像が表示されます。
再生リストの項目ダブルクリック : 選択された項目を再生リストから削除します。
Un Zip : フォルダーを指定すると、指定したフォルダーとそのサブフォルダー内の
Zip ファイルをファイル名と同じフォルダーを作成しそこに解凍
解凍した画像ファイルを再生リストに追加します。
再生 : 再生リストの項目をエンドレスに、スライドショーで表示します。
表示間隔を、500ミリ秒間隔で指定できます。
停止 : スライドショーを停止します。
次へ : 再生リストの次の項目を表示します。
前へ : 再生リストの前の項目を表示します。
画像表示領域をクリック : 全画面表示に切り替えます。
もう一度クリックで通常表示に戻ります。
全画面表示時にマウスカーソル(非表示になっています)を下まで持っていくと
再生ボタンなどが表示されます。
終了 : Pictures Slide Viewer + Un Zip を終了します。
再生リストとスライドショー表示間隔は保存され
次回起動時に読み込まれます。
Pictures Slide Viewer + Un Zip (PSVPUZ.exe)
Windows8 Desktop Application ( C# )
SkyDrive を使って公開します。
このリンクから >>> http://sdrv.ms/RZaKds
SkyDrive の公開フォルダが開くので
PSVPUZ.zip を適当なフォルダにダウンロード後、ファイルを展開、setup.exe を実行し
インストーラの指示に従って、インストールしてください。
インストーラは、ClickOnce を使用しています。
[Windows によって PC が保護されました] が表示された場合
[詳細情報] をクリック、つづいて [実行] をクリックしてください。
インストールが成功すれば、Win8 の場合 スタート の画面に Pictures Slide Viewer
のタイルがピン留めされますので、そこから起動できます。
デスクトップにショートカットを置きたい場合は、スタート の画面で右クリック
すべてのアプリ をクリックし、Pictures Slide Viewer を右クリックで選択
ファイルの場所を開く をクリック、エクスプローラーが開くので、Pictures Slide Viewer
を右クリック、ショートカットメニューから、送る > デスクトップ(ショートカット作成)を
選択してください。
アンインストールは、コントロールパネルのプログラムのアンインストールから
Pictures Slide Viewer をアンインストールしてください。
ラベル:
C#,
WinDesktop,
プログラム,
公開
2013年3月14日木曜日
Visual Basic PowerPacks 10.0 が ClickOnce でインストールできない件
以前公開した Copy text to clipboard ( CopyTtCb.exe ) は
インストーラーとして Visual Studio の 発行 ( ClickOnce ) を使用しました。
ところが、これをインストールしようとすると、Microsoft Visual Basic PowerPacks 10.0 が
予めインストールされていないと、途中で VB PowerPacks をインストールしようとし
そこでエラーが発生し、インストールに失敗してしまうことが分かりました。
MS Visual Studio がインストールされていると、一緒に VB PowerPacks もインストール
されているので、気が付きませんでした。
エラーは VB PowerPacks を、必須コンポーネントとしてインストールするのではなく
アプリケーション ファイル で Microsoft.VisualBasic.PowerPacks.VS.dll の
発行の状況 を 含む にしてやれば回避できるようです。・・・たぶん。
インストラーを修正したものを、SkyDrive を使って公開します。中身は以前のままです。
このリンクから >>> http://sdrv.ms/RZaKds
SkyDrive の公開フォルダが開くので
CopyTtCb.zip を適当なフォルダにダウンロード後、ファイルを展開、setup.exe を実行し
インストーラーの指示に従って、インストールしてください。
[Windows によって PC が保護されました] が表示された場合
[詳細情報] をクリック、つづいて [実行] をクリックしてください。
インストールが成功すれば、Win8 の スタート の画面に CopyTtCb.exe のタイルが
ピン留めされているので、右クリックし、タスクバーにもピン留めし、それを利用してください。
アンインストールは、コントロールパネルのプログラムのアンインストールから
CopyTtCb.exe をアンインストールしてください。
ラベル:
C#,
WinDesktop,
プログラム
2013年3月8日金曜日
Pictures Slide Viewer に機能追加
前回作成した Pictures Slide Viewer に
リストの項目ごとの削除機能
リスト項目とスライド時間を終了時保存し、次回起動時に呼び出す機能
を追加しました。
リスト項目の保存、呼び出しは Properties.Settings を利用し
リスト項目をカンマ区切りのベタテキストで保存し、
読み込み時にカンマで分割し直しています。
PicturesSlideViewer.exe C# WinDesktop
前回から、終了方法を変更
リストから項目ごとに削除、設定読み込み、設定保存、を追加
Form1.cs
=====================================================================
// 終了
private void button2_Click(object sender, EventArgs e)
{
// Environment.Exit(0);
this.Close();
}
// リストから項目ごとに削除
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if ((listBox1.Items.Count > 0) && (listBox1.SelectedIndex >= 0))
{
DialogResult result = MessageBox.Show(listBox1.Text, "リストから削除します", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
int selInd = listBox1.SelectedIndex;
// リストの中から削除します
listBox1.Items.RemoveAt(selInd);
// リスト選択項目指定
if (listBox1.Items.Count > 0)
{
if (listBox1.Items.Count > selInd)
{
listBox1.SetSelected(selInd, true);
}
else
{
listBox1.SetSelected(selInd -1 , true);
}
}
}
}
}
// 設定読み込み
private void Form1_Load(object sender, EventArgs e)
{
numericUpDown1.Value = Properties.Settings.Default.SetNum;
String StrSett = Properties.Settings.Default.Setting;
// 分割して配列に格納する
string[] stArrayData = StrSett.Split(',');
// データを追加する
foreach (string stData in stArrayData)
{
if (stData != "")
{
listBox1.Items.Add(stData);
}
}
// リストの1番を選択
if (listBox1.Items.Count > 0)
{
// ListSet();
listBox1.SetSelected(0, true);
}
}
// 設定保存
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// スライド時間設定保存
int Setnum = Convert.ToInt32(numericUpDown1.Value);
Properties.Settings.Default.SetNum = Setnum;
// リスト保存
if (listBox1.Items.Count > 0)
{
string StrSett = string.Empty;
for (int i = 0; i < listBox1.Items.Count; ++i)
{
listBox1.SetSelected(i, true);
StrSett = StrSett + listBox1.Text + ",";
}
Properties.Settings.Default.Setting = StrSett;
}
else
{
Properties.Settings.Default.Setting = null;
}
Properties.Settings.Default.Save();
}
=====================================================================
リストの項目ごとの削除機能
リスト項目とスライド時間を終了時保存し、次回起動時に呼び出す機能
を追加しました。
リスト項目の保存、呼び出しは Properties.Settings を利用し
リスト項目をカンマ区切りのベタテキストで保存し、
読み込み時にカンマで分割し直しています。
PicturesSlideViewer.exe C# WinDesktop
前回から、終了方法を変更
リストから項目ごとに削除、設定読み込み、設定保存、を追加
Form1.cs
=====================================================================
// 終了
private void button2_Click(object sender, EventArgs e)
{
// Environment.Exit(0);
this.Close();
}
// リストから項目ごとに削除
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if ((listBox1.Items.Count > 0) && (listBox1.SelectedIndex >= 0))
{
DialogResult result = MessageBox.Show(listBox1.Text, "リストから削除します", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
int selInd = listBox1.SelectedIndex;
// リストの中から削除します
listBox1.Items.RemoveAt(selInd);
// リスト選択項目指定
if (listBox1.Items.Count > 0)
{
if (listBox1.Items.Count > selInd)
{
listBox1.SetSelected(selInd, true);
}
else
{
listBox1.SetSelected(selInd -1 , true);
}
}
}
}
}
// 設定読み込み
private void Form1_Load(object sender, EventArgs e)
{
numericUpDown1.Value = Properties.Settings.Default.SetNum;
String StrSett = Properties.Settings.Default.Setting;
// 分割して配列に格納する
string[] stArrayData = StrSett.Split(',');
// データを追加する
foreach (string stData in stArrayData)
{
if (stData != "")
{
listBox1.Items.Add(stData);
}
}
// リストの1番を選択
if (listBox1.Items.Count > 0)
{
// ListSet();
listBox1.SetSelected(0, true);
}
}
// 設定保存
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// スライド時間設定保存
int Setnum = Convert.ToInt32(numericUpDown1.Value);
Properties.Settings.Default.SetNum = Setnum;
// リスト保存
if (listBox1.Items.Count > 0)
{
string StrSett = string.Empty;
for (int i = 0; i < listBox1.Items.Count; ++i)
{
listBox1.SetSelected(i, true);
StrSett = StrSett + listBox1.Text + ",";
}
Properties.Settings.Default.Setting = StrSett;
}
else
{
Properties.Settings.Default.Setting = null;
}
Properties.Settings.Default.Save();
}
=====================================================================
ラベル:
C#,
WinDesktop,
プログラム
2013年3月3日日曜日
フォルダ内の画像を表示スライドショーするプログラム
Pictures Slide Viewer
選択したフォルダー内(サブフォルダー内を含む)の画像ファイルをリストにし
スライドショー表示(全画面表示対応)するプログラムです。
スライドショーの再生間隔を、0.5秒から1時間まで、0.5秒単位で変更できます。
PicturesSlideViewer.exe C# WinDesktop
Form1 に button1 ~ 6 、 label1 ~ 3 、pictureBox1、listBox1、numericUpDown1 を配置
コンポーネント timer1、 ダイアログ folderBrowserDialog1 を追加
Form1.cs
=====================================================================
今回は追加したコントロールが多いのでこちらも掲載
Form1.Designer.cs
=====================================================================
namespace PicturesSlideViewer
{
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.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.button6 = new System.Windows.Forms.Button();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(823, 567);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 570);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(57, 12);
this.label1.TabIndex = 1;
this.label1.Text = "ファイル名:";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(829, 596);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "追加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button2.Location = new System.Drawing.Point(12, 596);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "終了";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button3.Location = new System.Drawing.Point(373, 596);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "次へ >>";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button4.Location = new System.Drawing.Point(183, 596);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 5;
this.button4.Text = "<< 前へ";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button5.Location = new System.Drawing.Point(277, 596);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 6;
this.button5.Text = "再生 >";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(829, 26);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 556);
this.listBox1.TabIndex = 7;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(840, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 8;
this.label2.Text = "リスト";
//
// button6
//
this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button6.Location = new System.Drawing.Point(914, 596);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 9;
this.button6.Text = "全削除";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// numericUpDown1
//
this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.numericUpDown1.Increment = new decimal(new int[] {
500,
0,
0,
0});
this.numericUpDown1.Location = new System.Drawing.Point(534, 596);
this.numericUpDown1.Maximum = new decimal(new int[] {
360000,
0,
0,
0});
this.numericUpDown1.Minimum = new decimal(new int[] {
500,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.ReadOnly = true;
this.numericUpDown1.Size = new System.Drawing.Size(81, 19);
this.numericUpDown1.TabIndex = 10;
this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.numericUpDown1.ThousandsSeparator = true;
this.numericUpDown1.Value = new decimal(new int[] {
2000,
0,
0,
0});
this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
//
// label3
//
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(621, 598);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(31, 12);
this.label3.TabIndex = 11;
this.label3.Text = "ミリ秒";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// folderBrowserDialog1
//
this.folderBrowserDialog1.Description = "フォルダを選択";
this.folderBrowserDialog1.ShowNewFolderButton = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(994, 631);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.label3);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.button6);
this.Controls.Add(this.label2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Pictures Slide Viewer";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
}
}
=====================================================================
選択したフォルダー内(サブフォルダー内を含む)の画像ファイルをリストにし
スライドショー表示(全画面表示対応)するプログラムです。
スライドショーの再生間隔を、0.5秒から1時間まで、0.5秒単位で変更できます。
PicturesSlideViewer.exe C# WinDesktop
Form1 に button1 ~ 6 、 label1 ~ 3 、pictureBox1、listBox1、numericUpDown1 を配置
コンポーネント timer1、 ダイアログ folderBrowserDialog1 を追加
Form1.cs
=====================================================================
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 PicturesSlideViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// ListBox1に追加する
private void button1_Click(object sender, EventArgs e)
{
// 初期選択するパスを設定する
// RootFolder以下にあるフォルダである必要がある
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// ダイアログを表示し、戻り値が [OK] の場合は、選択したディレクトリを表示する
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
try
{
// 選択したフォルダ以下のファイルをすべて取得、ListBox1に結果を追加する
string[] filesjpg = System.IO.Directory.GetFiles(
folderBrowserDialog1.SelectedPath, "*.jp*g" , System.IO.SearchOption.AllDirectories);
listBox1.Items.AddRange(filesjpg);
string[] filesgif = System.IO.Directory.GetFiles(
folderBrowserDialog1.SelectedPath, "*.gif", System.IO.SearchOption.AllDirectories);
listBox1.Items.AddRange(filesgif);
string[] filesbmp = System.IO.Directory.GetFiles(
folderBrowserDialog1.SelectedPath, "*.bmp", System.IO.SearchOption.AllDirectories);
listBox1.Items.AddRange(filesbmp);
string[] filespng = System.IO.Directory.GetFiles(
folderBrowserDialog1.SelectedPath, "*.png", System.IO.SearchOption.AllDirectories);
listBox1.Items.AddRange(filespng);
// リストの1番を選択
listBox1.SetSelected(0, true);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
// 終了
private void button2_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.ImageLocation = listBox1.Text;
label1.Text = "ファイル名:" + listBox1.Text;
}
// 次へ
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
{
listBox1.SetSelected(0, true);
}
else
{
int i = listBox1.SelectedIndex + 1;
listBox1.SetSelected(i, true);
}
}
}
// 前へ
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (listBox1.SelectedIndex == 0)
{
listBox1.SetSelected(listBox1.Items.Count - 1, true);
}
else
{
int i = listBox1.SelectedIndex - 1;
listBox1.SetSelected(i, true);
}
}
}
// ListBox1をクリアにする
private void button6_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
label1.Text = "ファイル名:";
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
}
// スライドショー再生・停止
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text == "再生 >")
{
button5.Text = "停止 ■";
// button3.PerformClick();
timer1.Interval = Convert.ToInt32(numericUpDown1.Value);
timer1.Start();
}
else
{
button5.Text = "再生 >";
timer1.Stop();
}
}
// スライドショーの切り替え間隔変更
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(numericUpDown1.Value);
}
// タイマーイベント
private void timer1_Tick(object sender, EventArgs e)
{
button3.PerformClick();
}
// フルスクリーンモード
// フルスクリーン・モードかどうかのフラグ
private bool _bScreenMode;
// フルスクリーン表示前のウィンドウの状態を保存する
private FormWindowState prevFormState;
// 通常表示時のフォームの境界線スタイルを保存する
private FormBorderStyle prevFormStyle;
// 通常表示時のウィンドウのサイズを保存する
private Size prevFormSize;
// 通常表示時のピクチャーボックスのサイズを保存する
private Size prevPictureBoxSize;
// フルスクリーンに切り替え
private void pictureBox1_Click(object sender, EventArgs e)
{
if (_bScreenMode == false)
{
// <フルスクリーン表示への切り替え処理>
// ウィンドウの状態を保存する
prevFormState = this.WindowState;
// 境界線スタイルを保存する
prevFormStyle = this.FormBorderStyle;
// ピクチャーボックスのサイズを保存
prevPictureBoxSize = pictureBox1.Size;
// 「最大化表示」→「フルスクリーン表示」では
// タスク・バーが消えないので、いったん「通常表示」を行う
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
// フォームのサイズを保存する
prevFormSize = this.Size;
// フォームの境界線スタイルを「None」にする
this.FormBorderStyle = FormBorderStyle.None;
// フォームのウィンドウ状態を「最大化」する
this.WindowState = FormWindowState.Maximized;
// ピクチャーボックスを最大化
pictureBox1.Size = this.ClientSize;
// フルスクリーン表示フラグをONにする
_bScreenMode = true;
}
else
{
// 既にフルスクリーンの場合は、次へ
button3.PerformClick();
}
}
// フルスクリーン解除
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (_bScreenMode == true)
{
// 次に進んでしまうので前に戻す
button4.PerformClick();
// <通常表示/最大化表示への切り替え処理>
// フォームのウィンドウのサイズを元に戻す
this.ClientSize = prevFormSize;
// 最大化表示に戻す場合にはいったん通常表示を行う
// (フルスクリーン表示の処理とのバランスと取るため)
if (prevFormState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
// フォームの境界線スタイルを元に戻す
this.FormBorderStyle = prevFormStyle;
// フォームのウィンドウ状態を元に戻す
this.WindowState = prevFormState;
// ピクチャーボックスを戻す
pictureBox1.Size = prevPictureBoxSize;
// フルスクリーン表示フラグをOFFにする
_bScreenMode = false;
}
}
}
}
=====================================================================
今回は追加したコントロールが多いのでこちらも掲載
Form1.Designer.cs
=====================================================================
namespace PicturesSlideViewer
{
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.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.button6 = new System.Windows.Forms.Button();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(823, 567);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 570);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(57, 12);
this.label1.TabIndex = 1;
this.label1.Text = "ファイル名:";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(829, 596);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "追加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button2.Location = new System.Drawing.Point(12, 596);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "終了";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button3.Location = new System.Drawing.Point(373, 596);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "次へ >>";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button4.Location = new System.Drawing.Point(183, 596);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 5;
this.button4.Text = "<< 前へ";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button5.Location = new System.Drawing.Point(277, 596);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 6;
this.button5.Text = "再生 >";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(829, 26);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 556);
this.listBox1.TabIndex = 7;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(840, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 8;
this.label2.Text = "リスト";
//
// button6
//
this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button6.Location = new System.Drawing.Point(914, 596);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 9;
this.button6.Text = "全削除";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// numericUpDown1
//
this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.numericUpDown1.Increment = new decimal(new int[] {
500,
0,
0,
0});
this.numericUpDown1.Location = new System.Drawing.Point(534, 596);
this.numericUpDown1.Maximum = new decimal(new int[] {
360000,
0,
0,
0});
this.numericUpDown1.Minimum = new decimal(new int[] {
500,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.ReadOnly = true;
this.numericUpDown1.Size = new System.Drawing.Size(81, 19);
this.numericUpDown1.TabIndex = 10;
this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.numericUpDown1.ThousandsSeparator = true;
this.numericUpDown1.Value = new decimal(new int[] {
2000,
0,
0,
0});
this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
//
// label3
//
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(621, 598);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(31, 12);
this.label3.TabIndex = 11;
this.label3.Text = "ミリ秒";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// folderBrowserDialog1
//
this.folderBrowserDialog1.Description = "フォルダを選択";
this.folderBrowserDialog1.ShowNewFolderButton = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(994, 631);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.label3);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.button6);
this.Controls.Add(this.label2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Pictures Slide Viewer";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
}
}
=====================================================================
ラベル:
C#,
WinDesktop,
プログラム
2013年2月28日木曜日
Zip を一括で解凍するプログラム
指定したフォルダ内の Zip ファイルをすべて解凍するプログラムです。
解凍するファイルと同じ名前のフォルダを、ファイルがあるフォルダー内に作成し、
そこへ解凍します。
Zip の解凍には、DotNetZip を使用しました。
MS Visual Studio に Ionic.Zip.dll を 参照の追加 しています。
DotNetZip - Zip and Unzip in C#, VB, any .NET language
http://dotnetzip.codeplex.com/
DotNetZip の Ver.1.9 を利用したのですが、日本語対応の部分で、
Ver.1.8 のサンプルがそのまま使えませんでした。
オプションの指定を、予め ReadOptions 変数(options)に用意しておいてから
指定することでうまくいきました。(プログラムの青字部分)
AllUnZip.exe C# WinDesktop
Form1 に button1 button2 と label1 を配置
Form1.cs
=====================================================================
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 Ionic.Zip;
namespace AllUnZip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string selectPath = null;
try
{
// string selectPath = string.Empty;
selectPath = string.Empty;
label1.Text = "フォルダを選択 解凍実行";
label1.Update();
// FolderBrowserDialog の新しいインスタンスを生成する (デザイナから追加している場合は必要ない)
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
// ダイアログの説明を設定する
folderBrowserDialog1.Description = "フォルダを選択";
// ルートになる特殊フォルダを設定する (初期値 SpecialFolder.Desktop)
// folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyDocuments;
// 初期選択するパスを設定する
// RootFolder以下にあるフォルダである必要がある
// folderBrowserDialog1.SelectedPath = @"C:\Program Files\";
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// [新しいフォルダ] ボタンを表示する (初期値 true)
folderBrowserDialog1.ShowNewFolderButton = false;
// ダイアログを表示し、戻り値が [OK] の場合は、選択したディレクトリをする
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
if (Directory.Exists(folderBrowserDialog1.SelectedPath))
{
selectPath = folderBrowserDialog1.SelectedPath;
}
else
{
label1.Text = "フォルダが実在しません";
return;
}
}
else
{
label1.Text = "解凍中止";
return;
}
if (!string.IsNullOrEmpty(selectPath))
{
// 提出モードの処理を実行する
this.UnCompress(selectPath);
}
// 不要になった時点で破棄する
folderBrowserDialog1.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
label1.Text = "解凍失敗!";
}
}
private void button2_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
/// <summary>
/// 解凍処理を実行する
/// </summary>
/// <param name="path">ユーザが選択したフォルダまでのパス</param>
/// <param name="extention">解凍対象の拡張子(例:"*.gz")。デフォルトは"*.zip"</param>
///
private void UnCompress(string path, string extention = "*.zip")
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
string savePath = directoryInfo.FullName;
// ディレクトリ内にある解凍対象を解凍する
foreach (FileInfo fileInfo in directoryInfo.GetFiles(extention))
{
using (FileStream inFile = fileInfo.OpenRead())
{
string currentFile = fileInfo.FullName;
string stBaseName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name);
var options = new ReadOptions
{
StatusMessageWriter = System.Console.Out,
Encoding = System.Text.Encoding.GetEncoding("shift_jis")
};
using (ZipFile zip = ZipFile.Read(currentFile, options))
{
if (System.IO.Directory.Exists(savePath + @"\" + stBaseName))
{
label1.Text = "同名のフォルダが存在する";
DialogResult result = MessageBox.Show
("同名のフォルダが存在するためスキップします。
\r\n処理を終了する場合はキャンセル"
, "エラー"
, MessageBoxButtons.OKCancel
, MessageBoxIcon.Hand);
if (result == DialogResult.Cancel)
{
label1.Text = "解凍中断";
return;
}
}
else
{
// 全て解凍する
// zip.ExtractAll(savePath + @"\" + stBaseName);
// 個別に解凍する
foreach (ZipEntry entry in zip)
{
label1.Text = entry.FileName;
label1.Update();
// 解凍処理
entry.Extract(savePath + @"\" + stBaseName);
}
}
}
}
}
label1.Text = "解凍終了";
}
}
}
=====================================================================
解凍するファイルと同じ名前のフォルダを、ファイルがあるフォルダー内に作成し、
そこへ解凍します。
Zip の解凍には、DotNetZip を使用しました。
MS Visual Studio に Ionic.Zip.dll を 参照の追加 しています。
DotNetZip - Zip and Unzip in C#, VB, any .NET language
http://dotnetzip.codeplex.com/
DotNetZip の Ver.1.9 を利用したのですが、日本語対応の部分で、
Ver.1.8 のサンプルがそのまま使えませんでした。
オプションの指定を、予め ReadOptions 変数(options)に用意しておいてから
指定することでうまくいきました。(プログラムの青字部分)
AllUnZip.exe C# WinDesktop
Form1 に button1 button2 と label1 を配置
Form1.cs
=====================================================================
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 Ionic.Zip;
namespace AllUnZip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string selectPath = null;
try
{
// string selectPath = string.Empty;
selectPath = string.Empty;
label1.Text = "フォルダを選択 解凍実行";
label1.Update();
// FolderBrowserDialog の新しいインスタンスを生成する (デザイナから追加している場合は必要ない)
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
// ダイアログの説明を設定する
folderBrowserDialog1.Description = "フォルダを選択";
// ルートになる特殊フォルダを設定する (初期値 SpecialFolder.Desktop)
// folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyDocuments;
// 初期選択するパスを設定する
// RootFolder以下にあるフォルダである必要がある
// folderBrowserDialog1.SelectedPath = @"C:\Program Files\";
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// [新しいフォルダ] ボタンを表示する (初期値 true)
folderBrowserDialog1.ShowNewFolderButton = false;
// ダイアログを表示し、戻り値が [OK] の場合は、選択したディレクトリをする
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
if (Directory.Exists(folderBrowserDialog1.SelectedPath))
{
selectPath = folderBrowserDialog1.SelectedPath;
}
else
{
label1.Text = "フォルダが実在しません";
return;
}
}
else
{
label1.Text = "解凍中止";
return;
}
if (!string.IsNullOrEmpty(selectPath))
{
// 提出モードの処理を実行する
this.UnCompress(selectPath);
}
// 不要になった時点で破棄する
folderBrowserDialog1.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
label1.Text = "解凍失敗!";
}
}
private void button2_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
/// <summary>
/// 解凍処理を実行する
/// </summary>
/// <param name="path">ユーザが選択したフォルダまでのパス</param>
/// <param name="extention">解凍対象の拡張子(例:"*.gz")。デフォルトは"*.zip"</param>
///
private void UnCompress(string path, string extention = "*.zip")
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
string savePath = directoryInfo.FullName;
// ディレクトリ内にある解凍対象を解凍する
foreach (FileInfo fileInfo in directoryInfo.GetFiles(extention))
{
using (FileStream inFile = fileInfo.OpenRead())
{
string currentFile = fileInfo.FullName;
string stBaseName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name);
var options = new ReadOptions
{
StatusMessageWriter = System.Console.Out,
Encoding = System.Text.Encoding.GetEncoding("shift_jis")
};
using (ZipFile zip = ZipFile.Read(currentFile, options))
{
if (System.IO.Directory.Exists(savePath + @"\" + stBaseName))
{
label1.Text = "同名のフォルダが存在する";
DialogResult result = MessageBox.Show
("同名のフォルダが存在するためスキップします。
\r\n処理を終了する場合はキャンセル"
, "エラー"
, MessageBoxButtons.OKCancel
, MessageBoxIcon.Hand);
if (result == DialogResult.Cancel)
{
label1.Text = "解凍中断";
return;
}
}
else
{
// 全て解凍する
// zip.ExtractAll(savePath + @"\" + stBaseName);
// 個別に解凍する
foreach (ZipEntry entry in zip)
{
label1.Text = entry.FileName;
label1.Update();
// 解凍処理
entry.Extract(savePath + @"\" + stBaseName);
}
}
}
}
}
label1.Text = "解凍終了";
}
}
}
=====================================================================
ラベル:
C#,
WinDesktop,
プログラム
2013年2月27日水曜日
フォルダを参照するプログラム
フォルダ参照ダイアログボックスを表示し、選択したフォルダにある
サブフォルダをリストに表示するプログラムです。
なぜか、マイ ドキュメントのフォルダを選択すると
サブフォルダのリストを表示するところ( GetDirectories )で
My Music フォルダへのアクセス権が無いとエラーが出ます。
ちなみに OS は Win8です。
下のプログラムではエラーが出た時点で処理を中止していますが
エラーをスキップしてリスト表示を続ける、簡単な方法はないものだろうか。
FolderBrowser.exe C# WinDesktop
Form1 に button1 と listBox1 を配置
Form1.cs
=====================================================================
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 FolderBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// FolderBrowserDialog の新しいインスタンスを生成する
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
// ダイアログの説明を設定する
folderBrowserDialog1.Description = "フォルダを選択";
// ルートになる特殊フォルダを設定する (初期値 SpecialFolder.Desktop)
// folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyDocuments;
// 初期選択するパスを設定する
// RootFolder以下にあるフォルダである必要がある
folderBrowserDialog1.SelectedPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// [新しいフォルダ] ボタンを表示する (初期値 true)
folderBrowserDialog1.ShowNewFolderButton = false;
// ダイアログを表示し、戻り値が [OK] の場合は、選択したディレクトリを表示する
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(folderBrowserDialog1.SelectedPath);
// 選択したフォルダ以下のサブフォルダをすべて取得する
// ワイルドカード"*"は、すべてのフォルダを意味する
try
{
string[] subFolders = System.IO.Directory.GetDirectories(
folderBrowserDialog1.SelectedPath, "*",
System.IO.SearchOption.AllDirectories);
// ListBox1をクリアにする
listBox1.Items.Clear();
listBox1.Items.AddRange(subFolders);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// 不要になった時点で破棄する
folderBrowserDialog1.Dispose();
}
}
}
=======================================================================
ラベル:
C#,
WinDesktop,
プログラム
2012年11月30日金曜日
HDDの一覧を表示するプログラム
前回に引き続き、Windows8 ストアーアプリで、ドライブの空き容量を一覧で表示するアプリ
を作ろうとしているのだけれど、問題は全然解決していません。
デバイスの一覧を表示するプログラムのサンプルが有ったので
これを基に、ハードディスクドライブ(HDD) の一覧を表示させることはできたのですが
これだと、あくまで、ハードとしての HDD の一覧なので、
必要としている、ドライブの空き容量の情報を得られませんでした。
とりあえず、今回作った、HDDの一覧を表示するプログラムを載せておきます。
ボタンをクリックすると、HDDの一覧が表示されます。
Disk drive information Windows8 Store App C#
MainPage.xaml
====================================
<Page
x:Class="Disk_drive_information.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Disk_drive_information"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="ContentRoot" Background="Black" Margin="100,20,100,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Header -->
<StackPanel x:Name="Header" Grid.Row="0">
<StackPanel Orientation="Horizontal"/>
</StackPanel>
<!-- Content -->
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="1" ZoomMode="Disabled">
<StackPanel x:Name="ContentPanel">
<Button x:Name="EnumerateInterfacesButton" Content="Enumerate" Margin="50,20,10,0" Click="EnumerateDeviceInterfaces" RenderTransformOrigin="0.821,0.921" HorizontalAlignment="Left" />
<TextBlock Text="Output" Margin="0,25,0,20" />
<StackPanel x:Name="Output" HorizontalAlignment="Left">
<TextBlock x:Name="OutputText" />
<!-- Device Interfaces-->
<ListBox x:Name="DeviceInterfacesOutputList" IsEnabled="False" BorderThickness="0" />
</StackPanel>
</StackPanel>
</ScrollViewer>
</Grid>
</Page>
====================================
MainPage.xaml.cs
====================================
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Devices.Enumeration;
// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください
namespace Disk_drive_information
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// このページがフレームに表示されるときに呼び出されます。
/// </summary>
/// <param name="e">このページにどのように到達したかを説明するイベント データ。Parameter
/// プロパティは、通常、ページを構成するために使用します。</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
async void EnumerateDeviceInterfaces(object sender, RoutedEventArgs eventArgs)
{
EnumerateInterfacesButton.IsEnabled = false;
DeviceInterfacesOutputList.Items.Clear();
try
{
var selector = "System.Devices.InterfaceClassGuid:=
{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
+ " AND
System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
var interfaces = await DeviceInformation.FindAllAsync(selector, null);
OutputText.Text = interfaces.Count + " device interface(s) found\n\n";
foreach (DeviceInformation deviceInterface in interfaces)
{
DisplayDeviceInterface(deviceInterface);
}
}
catch (ArgumentException)
{
OutputText.Text = "Not";
}
EnumerateInterfacesButton.IsEnabled = true;
}
async void DisplayDeviceInterface(DeviceInformation deviceInterface)
{
var id = "Id: " + deviceInterface.Id;
var name = "name: "+ deviceInterface.Name;
var isEnabled = "IsEnabled: " + deviceInterface.IsEnabled;
var item = id + "\n" + name + "\n" + isEnabled + "\n" ;
DeviceInterfacesOutputList.Items.Add(item);
}
}
}
====================================
を作ろうとしているのだけれど、問題は全然解決していません。
デバイスの一覧を表示するプログラムのサンプルが有ったので
これを基に、ハードディスクドライブ(HDD) の一覧を表示させることはできたのですが
これだと、あくまで、ハードとしての HDD の一覧なので、
必要としている、ドライブの空き容量の情報を得られませんでした。
とりあえず、今回作った、HDDの一覧を表示するプログラムを載せておきます。
ボタンをクリックすると、HDDの一覧が表示されます。
Disk drive information Windows8 Store App C#
MainPage.xaml
====================================
<Page
x:Class="Disk_drive_information.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Disk_drive_information"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="ContentRoot" Background="Black" Margin="100,20,100,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Header -->
<StackPanel x:Name="Header" Grid.Row="0">
<StackPanel Orientation="Horizontal"/>
</StackPanel>
<!-- Content -->
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="1" ZoomMode="Disabled">
<StackPanel x:Name="ContentPanel">
<Button x:Name="EnumerateInterfacesButton" Content="Enumerate" Margin="50,20,10,0" Click="EnumerateDeviceInterfaces" RenderTransformOrigin="0.821,0.921" HorizontalAlignment="Left" />
<TextBlock Text="Output" Margin="0,25,0,20" />
<StackPanel x:Name="Output" HorizontalAlignment="Left">
<TextBlock x:Name="OutputText" />
<!-- Device Interfaces-->
<ListBox x:Name="DeviceInterfacesOutputList" IsEnabled="False" BorderThickness="0" />
</StackPanel>
</StackPanel>
</ScrollViewer>
</Grid>
</Page>
====================================
MainPage.xaml.cs
====================================
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Devices.Enumeration;
// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください
namespace Disk_drive_information
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// このページがフレームに表示されるときに呼び出されます。
/// </summary>
/// <param name="e">このページにどのように到達したかを説明するイベント データ。Parameter
/// プロパティは、通常、ページを構成するために使用します。</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
async void EnumerateDeviceInterfaces(object sender, RoutedEventArgs eventArgs)
{
EnumerateInterfacesButton.IsEnabled = false;
DeviceInterfacesOutputList.Items.Clear();
try
{
var selector = "System.Devices.InterfaceClassGuid:=
{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
+ " AND
System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
var interfaces = await DeviceInformation.FindAllAsync(selector, null);
OutputText.Text = interfaces.Count + " device interface(s) found\n\n";
foreach (DeviceInformation deviceInterface in interfaces)
{
DisplayDeviceInterface(deviceInterface);
}
}
catch (ArgumentException)
{
OutputText.Text = "Not";
}
EnumerateInterfacesButton.IsEnabled = true;
}
async void DisplayDeviceInterface(DeviceInformation deviceInterface)
{
var id = "Id: " + deviceInterface.Id;
var name = "name: "+ deviceInterface.Name;
var isEnabled = "IsEnabled: " + deviceInterface.IsEnabled;
var item = id + "\n" + name + "\n" + isEnabled + "\n" ;
DeviceInterfacesOutputList.Items.Add(item);
}
}
}
====================================
2012年11月29日木曜日
Win8 ストアアプリのプログラミングでDriveInfo が使えない
Windows8 ストアーアプリで、ドライブの空き容量を一覧で表示するアプリ
(感じ的には explorer で、コンピュータ を一覧表示させた、写真のような感じ)
を作ろうと思ったのですが、なかなかうまくいきません。
Desktop アプリなら DriveInfo で、簡単に取れる、ドライブの空き容量などの情報が
Windows8 ストアーアプリでは、取得できません。
というか、 DriveInfo クラス自体がないみたいです。
ネットで調べているのですが、なかなか上手い情報が見つかりません。
ドライブの空き領域を取得する方法なんて、すぐ見つかりそうなものなのに・・・。
とりあえず、Desktop アプリ で、ドライブ情報を取得するプログラムを載せておきます。
フォーム上の、ボタンをクリックすとドライブ情報が表示されます。
Windows8 Desktop C#
Form に Button と Label を配置
Form1.cs
====================================
using System;
using System.IO;
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 Disk_Drive_Info
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
label1.Text = "";
foreach (DriveInfo Dd in allDrives)
{
label1.Text += "Drive " + Dd.Name;
if (Dd.IsReady == true)
{
long FreeP = Dd.TotalFreeSpace * 100 / Dd.TotalSize;
label1.Text += " : " + FreeP.ToString() + "% Free ";
label1.Text += Dd.TotalFreeSpace / 1000000000 + "GB / ";
label1.Text += Dd.TotalSize / 1000000000 + "GB " + "\n\r";
// label1.Text += " Volume label: " + Dd.VolumeLabel + "\n\r";
// label1.Text += " File system: " + Dd.DriveFormat + "\n\r";
// label1.Text += " DriveType: " + Dd.DriveType + "\n\r";
}
else
{
label1.Text += " : " + "\n\r";
}
}
}
}
}
====================================
(感じ的には explorer で、コンピュータ を一覧表示させた、写真のような感じ)
を作ろうと思ったのですが、なかなかうまくいきません。
Desktop アプリなら DriveInfo で、簡単に取れる、ドライブの空き容量などの情報が
Windows8 ストアーアプリでは、取得できません。
というか、 DriveInfo クラス自体がないみたいです。
ネットで調べているのですが、なかなか上手い情報が見つかりません。
ドライブの空き領域を取得する方法なんて、すぐ見つかりそうなものなのに・・・。
とりあえず、Desktop アプリ で、ドライブ情報を取得するプログラムを載せておきます。
フォーム上の、ボタンをクリックすとドライブ情報が表示されます。
Windows8 Desktop C#
Form に Button と Label を配置
Form1.cs
====================================
using System;
using System.IO;
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 Disk_Drive_Info
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
label1.Text = "";
foreach (DriveInfo Dd in allDrives)
{
label1.Text += "Drive " + Dd.Name;
if (Dd.IsReady == true)
{
long FreeP = Dd.TotalFreeSpace * 100 / Dd.TotalSize;
label1.Text += " : " + FreeP.ToString() + "% Free ";
label1.Text += Dd.TotalFreeSpace / 1000000000 + "GB / ";
label1.Text += Dd.TotalSize / 1000000000 + "GB " + "\n\r";
// label1.Text += " Volume label: " + Dd.VolumeLabel + "\n\r";
// label1.Text += " File system: " + Dd.DriveFormat + "\n\r";
// label1.Text += " DriveType: " + Dd.DriveType + "\n\r";
}
else
{
label1.Text += " : " + "\n\r";
}
}
}
}
}
====================================
ラベル:
C#,
Win8,
Win8アプリ,
WinDesktop,
プログラム
2012年11月24日土曜日
Win8 ジャンプリスト対応のコピペアプリ公開
Copy text to clipboard ( CopyTtCb.exe )
Windows8 Desktop Application ( Jump List support)
予め設定したテキストを、クリックでクリップボードにコピーする
Win8 デスクトップアプリです。
設定後、アプリ本体のボタンをクリックするか
タスクバーにピン留めしておけば、右クリック(ジャンプリスト)で使えます。
SkyDrive を使って公開します。
このリンクから >>> http://sdrv.ms/RZaKds
SkyDrive の公開フォルダが開くので
CopyTtCb.zip を適当なフォルダにダウンロード後、ファイルを展開、setup.exe を実行し
インストーラーの指示に従って、インストールしてください。
[Windows によって PC が保護されました] が表示された場合
[詳細情報] をクリック、つづいて [実行] をクリックしてください。(詳しくはこちら)
インストールが成功すれば、Win8 の スタート の画面に CopyTtCb.exe のタイルが
ピン留めされているので、右クリックし、タスクバーにもピン留めし、それを利用してください。
アンインストールは、コントロールパネルのプログラムのアンインストールから
CopyTtCb.exe をアンインストールしてください。
ラベル:
C#,
Win8,
WinDesktop,
プログラム,
公開
2012年11月22日木曜日
ジャンプリストを実装するプログラム
Windows8 の ジャンプリスト を使うための、C# のプログラムです。
今回は、PresentationFramework を利用してみました。
それでは手順です。
Jump List の名前で、Windows フォームアプリケーションを作成
(既存のアプリに組み込む場合は、プロジェクトを開く)
メニューバーから、プロジェクト 参照の追加
参照マネージャー の画面で、アセンブリ フレイムワーク の一覧の中の
PresentationFramework に チェック OK ボタンをクリック。
Form1.cs のコード画面で
using System.Windows.Shell;
を追加
これで、PresentationFramework が利用可能になります。
ついでに、using System.IO; も追加
JumpTask を用意し、これをJumpItem として JumpList に登録します。
最後に JumpList を表示させます。
今回のプログラムでは、フォームが表示されますが、フォームには何も無いです。
ジャンプリストに、電卓とメモ帳とコマンドプロンプトの3つが追加表示されます。
コマンドプロンプトは、dir を表示し、自動的に閉じます。
Form1.cs
====================================
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.Windows.Shell;
using System.IO;
namespace Jump_List
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// JumpTask 作成
JumpTask jumpTask1 = new JumpTask();
// JumpTask のプロパティ設定
jumpTask1.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
jumpTask1.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
jumpTask1.Arguments = "";
jumpTask1.Title = "Calculator";
jumpTask1.Description = "Open Calculator.";
jumpTask1.CustomCategory = "User Added Tasks";
// JumpTask 作成
JumpTask jumpTask2 = new JumpTask();
// JumpTask のプロパティ設定
jumpTask2.ApplicationPath = Path.Combine(Environment.GetEnvironmentVariable("windir") , "notepad.exe");
jumpTask2.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "notepad.exe");
jumpTask2.Arguments = "";
jumpTask2.Title = "NotePad";
jumpTask2.Description = "Open NotePad";
jumpTask2.CustomCategory = "User Added Tasks";
// JumpTask 作成
JumpTask jumpTask3 = new JumpTask();
// JumpTask のプロパティ設定
// string str = "test";
jumpTask3.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
jumpTask3.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
jumpTask3.Arguments = "/c dir";
jumpTask3.Title = "cmd";
jumpTask3.Description = "cmd";
jumpTask3.CustomCategory = "User Added Tasks";
// JumpList に JumpTask 登録
JumpList jumpList1 = new JumpList(new JumpItem[] { jumpTask1, jumpTask2 , jumpTask3 }, true, true);
// 表示
jumpList1.Apply();
}
}
}
====================================
ラベル:
C#,
Win8,
WinDesktop,
プログラム
2012年11月20日火曜日
アプリのユーザー設定を保存するプログラム
アプリケーションのユーザー設定を保存する方法はいくつかありますが、
今回は、Visual Studio C# で、[設定] ページ (プロジェクト デザイナー)を利用する方法を紹介
[プロジェクト] メニューの [プロパティ] をクリックし、[設定] タブをクリックする。
ここで、ユーザー設定の保存先の[名前] 等を予め決めておく。
次に、プログラムでは、
Properties.Settings.Default.[名前] が、ユーザー設定の格納場所となる。
設定を呼び出すときは
this.label1.Text = Properties.Settings.Default.[名前];
設定を格納するときは
Properties.Settings.Default.[名前] = this.textBox1.Text;
となる。
格納した設定をファイルとして保存するには
Properties.Settings.Default.Save();
ファイルとして保存して置かないと、アプリ終了時に設定が消えてしまうので
終了前には必ず実行すること。
アプリ起動時のファイルからの読み込みは、自動的に行なわれる。
設定を変更後、保存した設定ファイルの値に戻したい場合
Properties.Settings.Default.Reload();
設定を既定値([設定] の[値]欄の値 )に戻したい場合
Properties.Settings.Default.Reset();
この場合、設定ファイルの値もリセットされるので注意
また、Save()、Reload()、Reset() は、設定[名前] ごとに行われるのではなく
すべての設定[名前] に対して行われる。
---------------------------------------
動作確認用にプログラムを作ってみた。
Settings Management WinDesktop C#
[設定] ページ (プロジェクト デザイナー)で、下記を入力
[名前] TextSetting1
[型] string
[スコープ] ユーザー
[値] 初期値
form に label と textBox それと button を3つ配置
button1 が Save
button2 が Reload
button3 が Reset
Form1.cs
====================================================================
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 Settings_Management
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 起動時に保存してある設定を呼び出す
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
private void button1_Click(object sender, EventArgs e)
{
// 変数に設定した値を、設定ファイルとして保存したい場合
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
// 変数の値を設定した後で、現在の設定ファイルの値に戻したい場合
Properties.Settings.Default.Reload();
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
}
private void button3_Click(object sender, EventArgs e)
{
// 既定値に戻したい場合
Properties.Settings.Default.Reset();
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 設定の格納
Properties.Settings.Default.TextSetting1 = this.textBox1.Text;
this.label1.Text = Properties.Settings.Default.TextSetting1;
}
}
}
====================================================================
今回は、Visual Studio C# で、[設定] ページ (プロジェクト デザイナー)を利用する方法を紹介
[プロジェクト] メニューの [プロパティ] をクリックし、[設定] タブをクリックする。
ここで、ユーザー設定の保存先の[名前] 等を予め決めておく。
次に、プログラムでは、
Properties.Settings.Default.[名前] が、ユーザー設定の格納場所となる。
設定を呼び出すときは
this.label1.Text = Properties.Settings.Default.[名前];
設定を格納するときは
Properties.Settings.Default.[名前] = this.textBox1.Text;
となる。
格納した設定をファイルとして保存するには
Properties.Settings.Default.Save();
ファイルとして保存して置かないと、アプリ終了時に設定が消えてしまうので
終了前には必ず実行すること。
アプリ起動時のファイルからの読み込みは、自動的に行なわれる。
設定を変更後、保存した設定ファイルの値に戻したい場合
Properties.Settings.Default.Reload();
設定を既定値([設定] の[値]欄の値 )に戻したい場合
Properties.Settings.Default.Reset();
この場合、設定ファイルの値もリセットされるので注意
また、Save()、Reload()、Reset() は、設定[名前] ごとに行われるのではなく
すべての設定[名前] に対して行われる。
---------------------------------------
動作確認用にプログラムを作ってみた。
Settings Management WinDesktop C#
[設定] ページ (プロジェクト デザイナー)で、下記を入力
[名前] TextSetting1
[型] string
[スコープ] ユーザー
[値] 初期値
form に label と textBox それと button を3つ配置
button1 が Save
button2 が Reload
button3 が Reset
Form1.cs
====================================================================
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 Settings_Management
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 起動時に保存してある設定を呼び出す
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
private void button1_Click(object sender, EventArgs e)
{
// 変数に設定した値を、設定ファイルとして保存したい場合
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
// 変数の値を設定した後で、現在の設定ファイルの値に戻したい場合
Properties.Settings.Default.Reload();
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
}
private void button3_Click(object sender, EventArgs e)
{
// 既定値に戻したい場合
Properties.Settings.Default.Reset();
this.textBox1.Text = Properties.Settings.Default.TextSetting1;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 設定の格納
Properties.Settings.Default.TextSetting1 = this.textBox1.Text;
this.label1.Text = Properties.Settings.Default.TextSetting1;
}
}
}
ラベル:
C#,
WinDesktop,
プログラム
2012年11月19日月曜日
コマンドライン引数を取得するプログラム
コマンドライン引数を取得する方法を確認するため、作ってみた。
起動すると
ラベル1に System.Environment.CommandLine の値
フルパスでファイル名と引数
ラベル2に 配列に格納された System.Environment.GetCommandLineArgs() の値
配列[0] には、フルパスでファイル名、引数が指定されていない場合も有効
ラベル3に 配列からデータを得るための値
が表示される。
ボタンをクリックすると、
配列からデータを得るための値を増やし、ラベル2とラベル3が変更される。
配列[1] 以降が指定した引数となる。
配列のデータ数確認は、 配列名.Length
引数が指定されていない場合が 1、引数が2つあれば 3 となる。
Command line arguments WinDesktop C# のプログラム
Form に button と label を3つ配置
起動すると
ラベル1に System.Environment.CommandLine の値
フルパスでファイル名と引数
ラベル2に 配列に格納された System.Environment.GetCommandLineArgs() の値
配列[0] には、フルパスでファイル名、引数が指定されていない場合も有効
ラベル3に 配列からデータを得るための値
が表示される。
ボタンをクリックすると、
配列からデータを得るための値を増やし、ラベル2とラベル3が変更される。
配列[1] 以降が指定した引数となる。
配列のデータ数確認は、 配列名.Length
引数が指定されていない場合が 1、引数が2つあれば 3 となる。
デバッグ中に引数を指定する方法は、[プロジェクト名] のプロパティ (Alt+F7) の
デバッグ - コマンドライン引数 の欄に指定できる。
Command line arguments WinDesktop C# のプログラム
Form に button と label を3つ配置
Form1.cs
=======================================================================
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 Command_line_arguments
{
public partial class Form1 : Form
{
// 引数を配列に格納
string[] args = System.Environment.GetCommandLineArgs();
int i = 0;
public Form1()
{
InitializeComponent();
// フルパスでファイル名と引数を表示
this.label1.Text = System.Environment.CommandLine;
// 配列[0] には、フルパスでファイル名
this.label2.Text = args[i];
this.label3.Text = "変数 i の値 : " + i.ToString();
// this.label3.Text = args.Length.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
// 配列のデータ数確認、引数がなくてもデータ数は1
// i = i + 1;
if (args.Length > ++i)
{
// 配列[1] からが引数
this.label2.Text = args[i];
this.label3.Text = "変数 i の値 : " + i.ToString();
// Clipboard.SetText(args[i]);
}
}
}
}
=======================================================================
ラベル:
C#,
WinDesktop,
プログラム
登録:
投稿 (Atom)










