Skip to content

Instantly share code, notes, and snippets.

@taka2
Created December 7, 2009 09:11
Show Gist options
  • Select an option

  • Save taka2/250728 to your computer and use it in GitHub Desktop.

Select an option

Save taka2/250728 to your computer and use it in GitHub Desktop.
using System;
using System.Windows.Forms;
public class Form1 : Form
{
public Form1()
{
this.AllowDrop = true;
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.form_DragEnter);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.form_DragDrop);
Button btn1 = new Button();
btn1.Text = "OK";
btn1.Click += btn1_Click;
this.Controls.Add(btn1);
}
private void btn1_Click(object sender, EventArgs e)
{
//SaveFileDialogクラスのインスタンスを作成
SaveFileDialog sfd = new SaveFileDialog();
//はじめのファイル名を指定する
sfd.FileName = "新しいファイル.jpg";
//はじめに表示されるフォルダを指定する
//sfd.InitialDirectory = @"C:\";
//[ファイルの種類]に表示される選択肢を指定する
sfd.Filter =
"Jpegファイル(*.jpeg;*.jpg)|*.jpeg;*.jpg|すべてのファイル(*.*)|*.*";
//[ファイルの種類]ではじめに
//「すべてのファイル」が選択されているようにする
sfd.FilterIndex = 1;
//タイトルを設定する
//sfd.Title = "保存先のファイルを選択してください";
//ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
sfd.RestoreDirectory = true;
//既に存在するファイル名を指定したとき警告する
//デフォルトでTrueなので指定する必要はない
//sfd.OverwritePrompt = true;
//存在しないパスが指定されたとき警告を表示する
//デフォルトでTrueなので指定する必要はない
//sfd.CheckPathExists = true;
//ダイアログを表示する
if (sfd.ShowDialog() == DialogResult.OK)
{
//OKボタンがクリックされたとき
//選択されたファイル名を表示する
MessageBox.Show(sfd.FileName);
}
}
private void form_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void form_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string[] s = (string[]) e.Data.GetData(DataFormats.FileDrop, false);
foreach(string str in s)
MessageBox.Show(str);
}
[STAThread]
static void Main(string[] args)
{
Application.Run(new Form1());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment