using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
namespace StudySampleCSharp
... {
/**//// <summary>
/// Form2 の概要の説明です。
/// </summary>
public class Form2 : System.Windows.Forms.Form
...{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/**//// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnForeColor;
private System.Windows.Forms.Button btnBackColor;
private System.Windows.Forms.Button btnFonts;
private System.Windows.Forms.ComboBox cmbPosition;
private System.Windows.Forms.Button btnExcute;
private System.Windows.Forms.ListBox listBoxFile;
private System.Windows.Forms.Button btnSource;
private System.Windows.Forms.Button btnDestination;
private System.Windows.Forms.TextBox txtDestination;
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.Panel panelDemo;
private System.Windows.Forms.Label lblDemo;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
//Custom
private Font titleFont = new Font("MS UI Gothic",9);
private Color foreColor = Color.White;
private Color backColor = Color.Transparent;
private int iPanelLeft = 0;
private int iPanelTop = 0;
private int iLeft = 0;
private int iTop = 0;
private int iWidth = 0;
private int iHeight = 0;
private int iDirection = 0;
private bool isOnLbl = false;
private bool isOnSizing = false;
private int iLblInLeft = 0;
private System.Windows.Forms.TextBox txtDemo;
private int iLblInTop = 0;
private int iLblX = 0;
private int iLblY = 0;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.CheckBox checkBox1;
private bool isBorder = false;
public Form2()
...{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/**//// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows フォーム デザイナで生成されたコード#region Windows フォーム デザイナで生成されたコード
/**//// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
...{
this.btnSource = new System.Windows.Forms.Button();
this.listBoxFile = new System.Windows.Forms.ListBox();
this.txtSource = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtDestination = new System.Windows.Forms.TextBox();
this.btnFonts = new System.Windows.Forms.Button();
this.btnForeColor = new System.Windows.Forms.Button();
this.btnBackColor = new System.Windows.Forms.Button();
this.cmbPosition = new System.Windows.Forms.ComboBox();
this.btnExcute = new System.Windows.Forms.Button();
this.btnDestination = new System.Windows.Forms.Button();
this.panelDemo = new System.Windows.Forms.Panel();
this.txtDemo = new System.Windows.Forms.TextBox();
this.lblDemo = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.panelDemo.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// btnSource
//
this.btnSource.Location = new System.Drawing.Point(496, 8);
this.btnSource.Name = "btnSource";
this.btnSource.Size = new System.Drawing.Size(24, 23);
this.btnSource.TabIndex = 1;
this.btnSource.Text = "...";
this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
//
// listBoxFile
//
this.listBoxFile.ItemHeight = 12;
this.listBoxFile.Location = new System.Drawing.Point(8, 88);
this.listBoxFile.Name = "listBoxFile";
this.listBoxFile.Size = new System.Drawing.Size(128, 304);
this.listBoxFile.TabIndex = 3;
this.listBoxFile.SelectedIndexChanged += new System.EventHandler(this.listBoxFile_SelectedIndexChanged);
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(80, 8);
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(408, 19);
this.txtSource.TabIndex = 5;
this.txtSource.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 6;
this.label1.Text = "Source:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 7;
this.label2.Text = "Destination:";
//
// txtDestination
//
this.txtDestination.Location = new System.Drawing.Point(80, 40);
this.txtDestination.Name = "txtDestination";
this.txtDestination.Size = new System.Drawing.Size(408, 19);
this.txtDestination.TabIndex = 8;
this.txtDestination.Text = "";
//
// btnFonts
//
this.btnFonts.Location = new System.Drawing.Point(208, 112);
this.btnFonts.Name = "btnFonts";
this.btnFonts.TabIndex = 11;
this.btnFonts.Text = "Fonts";
this.btnFonts.Click += new System.EventHandler(this.btnFonts_Click);
//
// btnForeColor
//
this.btnForeColor.Location = new System.Drawing.Point(208, 144);
this.btnForeColor.Name = "btnForeColor";
this.btnForeColor.TabIndex = 12;
this.btnForeColor.Text = "Front Color";
this.btnForeColor.Click += new System.EventHandler(this.btnForeColor_Click);
//
// btnBackColor
//
this.btnBackColor.Location = new System.Drawing.Point(200, 208);
this.btnBackColor.Name = "btnBackColor";
this.btnBackColor.TabIndex = 13;
this.btnBackColor.Text = "Back Color";
this.btnBackColor.Click += new System.EventHandler(this.btnBackColor_Click);
//
// cmbPosition
//
this.cmbPosition.Location = new System.Drawing.Point(192, 64);
this.cmbPosition.Name = "cmbPosition";
this.cmbPosition.Size = new System.Drawing.Size(104, 20);
this.cmbPosition.TabIndex = 14;
this.cmbPosition.SelectedIndexChanged += new System.EventHandler(this.cmbPosition_SelectedIndexChanged);
//
// btnExcute
//
this.btnExcute.AllowDrop = true;
this.btnExcute.Location = new System.Drawing.Point(152, 304);
this.btnExcute.Name = "btnExcute";
this.btnExcute.Size = new System.Drawing.Size(88, 23);
this.btnExcute.TabIndex = 15;
this.btnExcute.Text = "Excute";
this.btnExcute.Click += new System.EventHandler(this.btnExcute_Click);
//
// btnDestination
//
this.btnDestination.Location = new System.Drawing.Point(496, 40);
this.btnDestination.Name = "btnDestination";
this.btnDestination.Size = new System.Drawing.Size(24, 23);
this.btnDestination.TabIndex = 16;
this.btnDestination.Text = "...";
this.btnDestination.Click += new System.EventHandler(this.btnDestination_Click);
//
// panelDemo
//
this.panelDemo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panelDemo.Controls.Add(this.txtDemo);
this.panelDemo.Controls.Add(this.lblDemo);
this.panelDemo.Location = new System.Drawing.Point(296, 104);
this.panelDemo.Name = "panelDemo";
this.panelDemo.Size = new System.Drawing.Size(320, 240);
this.panelDemo.TabIndex = 17;
this.panelDemo.Click += new System.EventHandler(this.panelDemo_Click);
this.panelDemo.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseUp);
this.panelDemo.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseMove);
this.panelDemo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseDown);
//
// txtDemo
//
this.txtDemo.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.txtDemo.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtDemo.Location = new System.Drawing.Point(8, 8);
this.txtDemo.Multiline = true;
this.txtDemo.Name = "txtDemo";
this.txtDemo.Size = new System.Drawing.Size(176, 48);
this.txtDemo.TabIndex = 1;
this.txtDemo.Text = "Input the text";
this.txtDemo.Leave += new System.EventHandler(this.txtDemo_Leave);
//
// lblDemo
//
this.lblDemo.Location = new System.Drawing.Point(48, 104);
this.lblDemo.Name = "lblDemo";
this.lblDemo.Size = new System.Drawing.Size(208, 32);
this.lblDemo.TabIndex = 0;
this.lblDemo.Text = "Input the text";
this.lblDemo.Click += new System.EventHandler(this.lblDemo_Click);
this.lblDemo.LocationChanged += new System.EventHandler(this.lblDemo_LocationChanged);
this.lblDemo.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseUp);
this.lblDemo.DoubleClick += new System.EventHandler(this.lblDemo_DoubleClick);
this.lblDemo.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseMove);
this.lblDemo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(352, 72);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 18;
this.textBox1.Text = "";
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(496, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 19;
this.textBox2.Text = "";
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 424);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] ...{
this.statusBarPanel1});
this.statusBar1.Size = new System.Drawing.Size(632, 22);
this.statusBar1.TabIndex = 20;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "statusBarPanel1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(144, 176);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(88, 24);
this.checkBox1.TabIndex = 21;
this.checkBox1.Text = "Transparent";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(632, 446);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnDestination);
this.Controls.Add(this.btnExcute);
this.Controls.Add(this.cmbPosition);
this.Controls.Add(this.btnBackColor);
this.Controls.Add(this.btnForeColor);
this.Controls.Add(this.btnFonts);
this.Controls.Add(this.txtDestination);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.listBoxFile);
this.Controls.Add(this.btnSource);
this.Controls.Add(this.panelDemo);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.panelDemo.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Form2_Load(object sender, System.EventArgs e)
...{
cmbPosition.Items.Add("TopLeft");
cmbPosition.Items.Add("TopRight");
cmbPosition.Items.Add("MiddleCenter");
cmbPosition.Items.Add("BottomLeft");
cmbPosition.Items.Add("BottomRight");
cmbPosition.SelectedIndex = 3;
lblDemo.Width = 100;
lblDemo.Height = 20;
txtDestination.Text = @"C:Documents and Settings84デスクトップpic";
Demo();
iPanelLeft = this.Left + (this.Width - this.ClientSize.Width) / 2 + panelDemo.Left + 2;
iPanelTop = this.Top + (this.Height - this.ClientSize.Height - (this.Width - this.ClientSize.Width) / 2) + panelDemo.Top + 2;
txtDemo.Visible = false;
}
button Event#region button Event
private void btnSource_Click(object sender, System.EventArgs e)
...{
string strFileName = "";
OpenFileDialog fDlg = new OpenFileDialog();
fDlg.Multiselect = true;
fDlg.InitialDirectory = @"C:Documents and SettingsAll UsersDocumentsMy PicturesÐx240";
if (fDlg.ShowDialog() != DialogResult.OK)
...{
return;
}
strFileName = fDlg.FileNames[0].ToString();
txtSource.Text = strFileName.Substring(0,strFileName.LastIndexOf("/"));
for(int ix=0; ix < fDlg.FileNames.Length; ix++)
...{
strFileName = fDlg.FileNames[ix].ToString();
strFileName = strFileName.Substring(strFileName.LastIndexOf("/") + 1);
listBoxFile.Items.Add(strFileName);
}
fDlg.Dispose();
}
private void btnDestination_Click(object sender, System.EventArgs e)
...{
FolderBrowserDialog foldDlg = new FolderBrowserDialog();
if (foldDlg.ShowDialog() != DialogResult.OK)
...{
return;
}
txtDestination.Text = foldDlg.SelectedPath;
}
private void listBoxFile_SelectedIndexChanged(object sender, System.EventArgs e)
...{
string strFilePath;
if (listBoxFile.SelectedIndex < 0)
...{
return;
}
strFilePath = txtSource.Text + "/" + listBoxFile.Items[listBoxFile.SelectedIndex];
if (File.Exists(strFilePath))
...{
Image img = Image.FromFile(strFilePath);
panelDemo.BackgroundImage = img;
//img.Dispose();
}
}
private void btnFonts_Click(object sender, System.EventArgs e)
...{
FontDialog fontDlg = new FontDialog();
fontDlg.Font = titleFont;
if (fontDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
titleFont = fontDlg.Font;
fontDlg.Dispose();
Demo();
}
private void btnForeColor_Click(object sender, System.EventArgs e)
...{
ColorDialog colorDlg = new ColorDialog();
colorDlg.Color = foreColor;
if (colorDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
foreColor = colorDlg.Color;
colorDlg.Dispose();
Demo();
}
private void btnBackColor_Click(object sender, System.EventArgs e)
...{
ColorDialog colorDlg = new ColorDialog();
colorDlg.Color = backColor;
if (colorDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
backColor = colorDlg.Color;
colorDlg.Dispose();
Demo();
}
private void btnExcute_Click(object sender, System.EventArgs e)
...{
string strLogoTitle = lblDemo.Text;
if (strLogoTitle == string.Empty)
...{
MessageBox.Show("The LogoTitle can't be empty!");
txtDemo.Width = lblDemo.Width;
txtDemo.Height = lblDemo.Height;
txtDemo.Location = lblDemo.Location;
txtDemo.Font = lblDemo.Font;
if (lblDemo.BackColor != Color.Transparent)
txtDemo.BackColor = lblDemo.BackColor;
txtDemo.ForeColor = lblDemo.ForeColor;
lblDemo.Visible = false;
txtDemo.Visible = true;
txtDemo.Focus();
return;
}
if (listBoxFile.Items.Count < 1)
...{
MessageBox.Show("Please set the files that you want to add title to!");
btnSource.Select();
return;
}
if (txtDestination.Text == string.Empty)
...{
MessageBox.Show("Please set the destination fold that you want to save the images!");
btnDestination.Select();
return;
}
string fileName = "";
string FILE_NAME = "";
System.Drawing.Image image;
Bitmap bitmap;
Graphics g;
float rectX = lblDemo.Left;
float rectY = lblDemo.Top;
float rectWidth = lblDemo.Width;
float rectHeight = lblDemo.Height;
Brush frontBrush = new SolidBrush(foreColor);
Brush backBrush = new SolidBrush(backColor);
for (int ix = 0; ix < listBoxFile.Items.Count; ix++)
...{
fileName = txtSource.Text + "/";
fileName = fileName + listBoxFile.Items[ix].ToString();
if(!File.Exists(fileName))
...{
continue;
}
image = System.Drawing.Image.FromFile(fileName);
bitmap = new Bitmap(image,image.Width,image.Height);
g = Graphics.FromImage(bitmap);
// switch(cmbPosition.SelectedIndex)
// {
// case 0:
// rectX = 0;
// rectY = 0;
// break;
// case 1:
// rectX = image.Width - titleFont.Size * lblDemo.Text.Length;
// rectY = 0;
// break;
// case 2:
// rectX = image.Width / 2;
// rectY = image.Height / 2;
// break;
// case 3:
// rectX = 0;
// rectY = image.Height - titleFont.Size;
// break;
// case 4:
// rectX = image.Width - titleFont.Size * lblDemo.Text.Length;
// rectY = image.Height - titleFont.Size;
// break;
// default:
// rectX = 0;
// rectY = image.Height - titleFont.Size;
// break;
// }
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
g.FillRectangle(backBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(strLogoTitle,titleFont,frontBrush,textArea);
FILE_NAME = txtDestination.Text + "/";
FILE_NAME = FILE_NAME + listBoxFile.Items[ix].ToString();
bitmap.Save(FILE_NAME,ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
MessageBox.Show("complete!");
}
#endregion
Demo#region Demo
private void Demo()
...{
//int iGood = 0;
// int iTitleLen = System.Text.Encoding.GetEncoding("GB18030").GetByteCount(txtLogo.Text);
lblDemo.BackColor = backColor;
lblDemo.ForeColor = foreColor;
lblDemo.Font = titleFont;
// switch(cmbPosition.SelectedIndex)
// {
// case 0:
// lblDemo.Left = iGood;
// lblDemo.Top = iGood;
// break;
// case 1:
// lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
// lblDemo.Top = iGood;
// break;
// case 2:
// lblDemo.Left = panelDemo.Width / 2 - lblDemo.Width / 2;
// lblDemo.Top = panelDemo.Height / 2 - lblDemo.Height / 2;
// break;
// case 3:
// lblDemo.Left = iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// case 4:
// lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// default:
// lblDemo.Left = iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// }
}
#endregion
sonota Event#region sonota Event
private void cmbPosition_SelectedIndexChanged(object sender, System.EventArgs e)
...{
int iGood = 0;
switch(cmbPosition.SelectedIndex)
...{
case 0:
lblDemo.Left = iGood;
lblDemo.Top = iGood;
break;
case 1:
lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
lblDemo.Top = iGood;
break;
case 2:
lblDemo.Left = panelDemo.Width / 2 - lblDemo.Width / 2;
lblDemo.Top = panelDemo.Height / 2 - lblDemo.Height / 2;
break;
case 3:
lblDemo.Left = iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
case 4:
lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
default:
lblDemo.Left = iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
}
}
private void txtLogo_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
Demo();
}
#endregion
Panel Event#region Panel Event
private void panelDemo_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (isOnSizing == false)
...{
panelDemo.Cursor = Cursors.Arrow;
}
if (lblDemo.Visible == true)
...{
int ioffset = 5;
//Left
if ((e.X <= lblDemo.Left && e.X >= lblDemo.Left - ioffset) &&
(e.Y <= lblDemo.Top + lblDemo.Height && e.Y >= lblDemo.Top))
...{
panelDemo.Cursor = Cursors.SizeWE;
}
//Right
if ((e.X >= lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + ioffset) &&
(e.Y <= lblDemo.Top + lblDemo.Height && e.Y >= lblDemo.Top))
...{
panelDemo.Cursor = Cursors.SizeWE;
}
//Top
if ((e.X >= lblDemo.Left && e.X <= lblDemo.Left + lblDemo.Width) &&
(e.Y >= lblDemo.Top - ioffset) && e.Y <= lblDemo.Top)
...{
panelDemo.Cursor = Cursors.SizeNS;
}
//Bottom
if ((e.X >= lblDemo.Left && e.X <=lblDemo.Left + lblDemo.Width) &&
(e.Y >= lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + ioffset))
...{
panelDemo.Cursor = Cursors.SizeNS;
}
//TopLeft
if ((e.X < lblDemo.Left && e.X >= lblDemo.Left - 2 * ioffset) &&
(e.Y < lblDemo.Top && e.Y >= lblDemo.Top - 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNWSE;
}
//TopRight
if ((e.X > lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + 2 * ioffset) &&
(e.Y < lblDemo.Top && e.Y >= lblDemo.Top - 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNESW;
}
//BottomLeft
if ((e.X < lblDemo.Left && e.X >= lblDemo.Left - 2 * ioffset) &&
(e.Y > lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNESW;
}
//BottomRight
if ((e.X > lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + 2 * ioffset) &&
(e.Y > lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNWSE;
}
}
if (isOnSizing == true)
...{
switch (iDirection)
...{
case 1:
//Left
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Left = e.X;
}
break;
case 2:
//Right
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
}
break;
case 3:
//Top
if (e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Top = e.Y;
}
break;
case 4:
//Bottom
if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
case 5:
//TopLeft
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else if(e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Left = e.X;
lblDemo.Top = e.Y;
}
break;
case 6:
//TopRight
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
if (e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Top = e.Y;
}
break;
case 7:
//BottomLeft
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Left = e.X;
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
case 8:
//BottomRight
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
default:
break;
}
}
}
private void panelDemo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (panelDemo.Cursor == Cursors.SizeWE ||
panelDemo.Cursor == Cursors.SizeNS ||
panelDemo.Cursor == Cursors.SizeNESW ||
panelDemo.Cursor == Cursors.SizeNWSE)
...{
isOnSizing = true;
iLeft = lblDemo.Left;
iTop = lblDemo.Top;
iWidth = lblDemo.Width;
iHeight = lblDemo.Height;
//Left
if (e.X <= lblDemo.Left && e.Y > lblDemo.Top && e.Y < lblDemo.Top + lblDemo.Height)
...{
iDirection = 1;
}
//Right
if (e.X >= lblDemo.Left + lblDemo.Width && e.Y > lblDemo.Top && e.Y < lblDemo.Top + lblDemo.Height)
...{
iDirection = 2;
}
//Top
if (e.Y <= lblDemo.Top && e.X > lblDemo.Left && e.X < lblDemo.Left + lblDemo.Width)
...{
iDirection = 3;
}
//Bottom
if (e.Y >= lblDemo.Top + lblDemo.Height && e.X > lblDemo.Left && e.X < lblDemo.Left + lblDemo.Width)
...{
iDirection = 4;
}
//TopLeft
if (e.X < lblDemo.Left && e.Y < lblDemo.Top)
...{
iDirection = 5;
}
//TopRight
if (e.Y < lblDemo.Top && e.X > lblDemo.Left + lblDemo.Width)
...{
iDirection = 6;
}
//BottomLeft
if (e.X < lblDemo.Left && e.Y > lblDemo.Top + lblDemo.Height)
...{
iDirection = 7;
}
//BottomRight
if (e.Y > lblDemo.Top + lblDemo.Height && e.X > lblDemo.Left + lblDemo.Width)
...{
iDirection = 8;
}
}
}
private void panelDemo_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
...{
panelDemo.Cursor = Cursors.Arrow;
iDirection = 0;
isOnSizing = false;
isOnLbl = false;
}
#endregion
Label Event#region Label Event
private void lblDemo_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
lblDemo.Cursor = Cursors.SizeAll;
if (isOnLbl == true)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else
...{
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
else if (Cursor.Position.X - iPanelLeft - iLblInLeft > panelDemo.Width - lblDemo.Width)
...{
lblDemo.Left = panelDemo.Width - lblDemo.Width;
if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else
...{
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
}
else
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
}
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
}
else
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
}
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else if (Cursor.Position.X - iPanelLeft - iLblInLeft > 0 &&
Cursor.Position.X - iPanelLeft - iLblInLeft < panelDemo.Width - lblDemo.Width &&
Cursor.Position.Y - iPanelTop - iLblInTop > 0 &&
Cursor.Position.Y - iPanelTop - iLblInTop < panelDemo.Height - lblDemo.Height)
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
}
private void lblDemo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (lblDemo.BorderStyle == BorderStyle.FixedSingle)
...{
isBorder = true;
}
else
...{
isBorder = false;
}
lblDemo.BorderStyle = BorderStyle.FixedSingle;
isOnLbl = true;
iLblInLeft = e.X;
iLblInTop = e.Y;
iLblX = lblDemo.Left;
iLblY = lblDemo.Top;
}
private void lblDemo_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
...{
isOnLbl = false;
if (isBorder == false && (lblDemo.Left != iLblX || lblDemo.Top != iLblY))
...{
lblDemo.BorderStyle = BorderStyle.None;
}
}
private void lblDemo_DoubleClick(object sender, System.EventArgs e)
...{
txtDemo.Width = lblDemo.Width;
txtDemo.Height = lblDemo.Height;
txtDemo.Location = lblDemo.Location;
txtDemo.Font = lblDemo.Font;
if (lblDemo.BackColor != Color.Transparent)
txtDemo.BackColor = lblDemo.BackColor;
txtDemo.ForeColor = lblDemo.ForeColor;
lblDemo.Visible = false;
txtDemo.Visible = true;
txtDemo.Focus();
}
private void txtDemo_Leave(object sender, System.EventArgs e)
...{
lblDemo.Visible = true;
txtDemo.Visible = false;
lblDemo.Text = txtDemo.Text;
}
private void lblDemo_Click(object sender, System.EventArgs e)
...{
lblDemo.BorderStyle = BorderStyle.FixedSingle;
}
private void panelDemo_Click(object sender, System.EventArgs e)
...{
if (lblDemo.Visible == true)
...{
lblDemo.BorderStyle = BorderStyle.None;
}
else if (txtDemo.Visible == true)
...{
lblDemo.Visible = true;
txtDemo.Visible = false;
lblDemo.Text = txtDemo.Text;
lblDemo.BorderStyle = BorderStyle.None;
}
}
#endregion
private void lblDemo_LocationChanged(object sender, System.EventArgs e)
...{
textBox1.Text = lblDemo.Left.ToString();
textBox2.Text = lblDemo.Top.ToString();
}
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
string strLeft = textBox1.Text;
for (int ix = 0; ix < strLeft.Length; ix++)
...{
if (Char.IsNumber(strLeft,ix) == false)
...{
MessageBox.Show("Please input a integer!");
textBox1.Select();
return;
}
}
int iLeft = Convert.ToInt32(strLeft);
if (iLeft < 0)
...{
iLeft = 0;
}
if (iLeft > panelDemo.Width - lblDemo.Width)
...{
iLeft = panelDemo.Width - lblDemo.Width;
}
lblDemo.Left = iLeft;
}
private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
string strTop = textBox2.Text;
for (int ix = 0; ix < strTop.Length; ix++)
...{
if (Char.IsNumber(strTop,ix) == false)
...{
MessageBox.Show("Please input a integer!");
textBox2.Select();
return;
}
}
int iTop = Convert.ToInt32(strTop);
if (iTop < 0)
...{
iTop = 0;
}
if (iTop > panelDemo.Height - lblDemo.Height)
...{
iTop = panelDemo.Height - lblDemo.Height;
}
lblDemo.Top = iTop;
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
...{
if (checkBox1.Checked == true)
...{
btnBackColor.Enabled = false;
backColor = Color.Transparent;
Demo();
}
else
...{
btnBackColor.Enabled = true;
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
namespace StudySampleCSharp
... {
/**//// <summary>
/// Form2 の概要の説明です。
/// </summary>
public class Form2 : System.Windows.Forms.Form
...{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/**//// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnForeColor;
private System.Windows.Forms.Button btnBackColor;
private System.Windows.Forms.Button btnFonts;
private System.Windows.Forms.ComboBox cmbPosition;
private System.Windows.Forms.Button btnExcute;
private System.Windows.Forms.ListBox listBoxFile;
private System.Windows.Forms.Button btnSource;
private System.Windows.Forms.Button btnDestination;
private System.Windows.Forms.TextBox txtDestination;
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.Panel panelDemo;
private System.Windows.Forms.Label lblDemo;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
//Custom
private Font titleFont = new Font("MS UI Gothic",9);
private Color foreColor = Color.White;
private Color backColor = Color.Transparent;
private int iPanelLeft = 0;
private int iPanelTop = 0;
private int iLeft = 0;
private int iTop = 0;
private int iWidth = 0;
private int iHeight = 0;
private int iDirection = 0;
private bool isOnLbl = false;
private bool isOnSizing = false;
private int iLblInLeft = 0;
private System.Windows.Forms.TextBox txtDemo;
private int iLblInTop = 0;
private int iLblX = 0;
private int iLblY = 0;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.CheckBox checkBox1;
private bool isBorder = false;
public Form2()
...{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/**//// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows フォーム デザイナで生成されたコード#region Windows フォーム デザイナで生成されたコード
/**//// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
...{
this.btnSource = new System.Windows.Forms.Button();
this.listBoxFile = new System.Windows.Forms.ListBox();
this.txtSource = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtDestination = new System.Windows.Forms.TextBox();
this.btnFonts = new System.Windows.Forms.Button();
this.btnForeColor = new System.Windows.Forms.Button();
this.btnBackColor = new System.Windows.Forms.Button();
this.cmbPosition = new System.Windows.Forms.ComboBox();
this.btnExcute = new System.Windows.Forms.Button();
this.btnDestination = new System.Windows.Forms.Button();
this.panelDemo = new System.Windows.Forms.Panel();
this.txtDemo = new System.Windows.Forms.TextBox();
this.lblDemo = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.panelDemo.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// btnSource
//
this.btnSource.Location = new System.Drawing.Point(496, 8);
this.btnSource.Name = "btnSource";
this.btnSource.Size = new System.Drawing.Size(24, 23);
this.btnSource.TabIndex = 1;
this.btnSource.Text = "...";
this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
//
// listBoxFile
//
this.listBoxFile.ItemHeight = 12;
this.listBoxFile.Location = new System.Drawing.Point(8, 88);
this.listBoxFile.Name = "listBoxFile";
this.listBoxFile.Size = new System.Drawing.Size(128, 304);
this.listBoxFile.TabIndex = 3;
this.listBoxFile.SelectedIndexChanged += new System.EventHandler(this.listBoxFile_SelectedIndexChanged);
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(80, 8);
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(408, 19);
this.txtSource.TabIndex = 5;
this.txtSource.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 6;
this.label1.Text = "Source:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 7;
this.label2.Text = "Destination:";
//
// txtDestination
//
this.txtDestination.Location = new System.Drawing.Point(80, 40);
this.txtDestination.Name = "txtDestination";
this.txtDestination.Size = new System.Drawing.Size(408, 19);
this.txtDestination.TabIndex = 8;
this.txtDestination.Text = "";
//
// btnFonts
//
this.btnFonts.Location = new System.Drawing.Point(208, 112);
this.btnFonts.Name = "btnFonts";
this.btnFonts.TabIndex = 11;
this.btnFonts.Text = "Fonts";
this.btnFonts.Click += new System.EventHandler(this.btnFonts_Click);
//
// btnForeColor
//
this.btnForeColor.Location = new System.Drawing.Point(208, 144);
this.btnForeColor.Name = "btnForeColor";
this.btnForeColor.TabIndex = 12;
this.btnForeColor.Text = "Front Color";
this.btnForeColor.Click += new System.EventHandler(this.btnForeColor_Click);
//
// btnBackColor
//
this.btnBackColor.Location = new System.Drawing.Point(200, 208);
this.btnBackColor.Name = "btnBackColor";
this.btnBackColor.TabIndex = 13;
this.btnBackColor.Text = "Back Color";
this.btnBackColor.Click += new System.EventHandler(this.btnBackColor_Click);
//
// cmbPosition
//
this.cmbPosition.Location = new System.Drawing.Point(192, 64);
this.cmbPosition.Name = "cmbPosition";
this.cmbPosition.Size = new System.Drawing.Size(104, 20);
this.cmbPosition.TabIndex = 14;
this.cmbPosition.SelectedIndexChanged += new System.EventHandler(this.cmbPosition_SelectedIndexChanged);
//
// btnExcute
//
this.btnExcute.AllowDrop = true;
this.btnExcute.Location = new System.Drawing.Point(152, 304);
this.btnExcute.Name = "btnExcute";
this.btnExcute.Size = new System.Drawing.Size(88, 23);
this.btnExcute.TabIndex = 15;
this.btnExcute.Text = "Excute";
this.btnExcute.Click += new System.EventHandler(this.btnExcute_Click);
//
// btnDestination
//
this.btnDestination.Location = new System.Drawing.Point(496, 40);
this.btnDestination.Name = "btnDestination";
this.btnDestination.Size = new System.Drawing.Size(24, 23);
this.btnDestination.TabIndex = 16;
this.btnDestination.Text = "...";
this.btnDestination.Click += new System.EventHandler(this.btnDestination_Click);
//
// panelDemo
//
this.panelDemo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panelDemo.Controls.Add(this.txtDemo);
this.panelDemo.Controls.Add(this.lblDemo);
this.panelDemo.Location = new System.Drawing.Point(296, 104);
this.panelDemo.Name = "panelDemo";
this.panelDemo.Size = new System.Drawing.Size(320, 240);
this.panelDemo.TabIndex = 17;
this.panelDemo.Click += new System.EventHandler(this.panelDemo_Click);
this.panelDemo.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseUp);
this.panelDemo.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseMove);
this.panelDemo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelDemo_MouseDown);
//
// txtDemo
//
this.txtDemo.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.txtDemo.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtDemo.Location = new System.Drawing.Point(8, 8);
this.txtDemo.Multiline = true;
this.txtDemo.Name = "txtDemo";
this.txtDemo.Size = new System.Drawing.Size(176, 48);
this.txtDemo.TabIndex = 1;
this.txtDemo.Text = "Input the text";
this.txtDemo.Leave += new System.EventHandler(this.txtDemo_Leave);
//
// lblDemo
//
this.lblDemo.Location = new System.Drawing.Point(48, 104);
this.lblDemo.Name = "lblDemo";
this.lblDemo.Size = new System.Drawing.Size(208, 32);
this.lblDemo.TabIndex = 0;
this.lblDemo.Text = "Input the text";
this.lblDemo.Click += new System.EventHandler(this.lblDemo_Click);
this.lblDemo.LocationChanged += new System.EventHandler(this.lblDemo_LocationChanged);
this.lblDemo.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseUp);
this.lblDemo.DoubleClick += new System.EventHandler(this.lblDemo_DoubleClick);
this.lblDemo.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseMove);
this.lblDemo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDemo_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(352, 72);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 18;
this.textBox1.Text = "";
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(496, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 19;
this.textBox2.Text = "";
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 424);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] ...{
this.statusBarPanel1});
this.statusBar1.Size = new System.Drawing.Size(632, 22);
this.statusBar1.TabIndex = 20;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "statusBarPanel1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(144, 176);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(88, 24);
this.checkBox1.TabIndex = 21;
this.checkBox1.Text = "Transparent";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(632, 446);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnDestination);
this.Controls.Add(this.btnExcute);
this.Controls.Add(this.cmbPosition);
this.Controls.Add(this.btnBackColor);
this.Controls.Add(this.btnForeColor);
this.Controls.Add(this.btnFonts);
this.Controls.Add(this.txtDestination);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.listBoxFile);
this.Controls.Add(this.btnSource);
this.Controls.Add(this.panelDemo);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.panelDemo.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Form2_Load(object sender, System.EventArgs e)
...{
cmbPosition.Items.Add("TopLeft");
cmbPosition.Items.Add("TopRight");
cmbPosition.Items.Add("MiddleCenter");
cmbPosition.Items.Add("BottomLeft");
cmbPosition.Items.Add("BottomRight");
cmbPosition.SelectedIndex = 3;
lblDemo.Width = 100;
lblDemo.Height = 20;
txtDestination.Text = @"C:Documents and Settings84デスクトップpic";
Demo();
iPanelLeft = this.Left + (this.Width - this.ClientSize.Width) / 2 + panelDemo.Left + 2;
iPanelTop = this.Top + (this.Height - this.ClientSize.Height - (this.Width - this.ClientSize.Width) / 2) + panelDemo.Top + 2;
txtDemo.Visible = false;
}
button Event#region button Event
private void btnSource_Click(object sender, System.EventArgs e)
...{
string strFileName = "";
OpenFileDialog fDlg = new OpenFileDialog();
fDlg.Multiselect = true;
fDlg.InitialDirectory = @"C:Documents and SettingsAll UsersDocumentsMy PicturesÐx240";
if (fDlg.ShowDialog() != DialogResult.OK)
...{
return;
}
strFileName = fDlg.FileNames[0].ToString();
txtSource.Text = strFileName.Substring(0,strFileName.LastIndexOf("/"));
for(int ix=0; ix < fDlg.FileNames.Length; ix++)
...{
strFileName = fDlg.FileNames[ix].ToString();
strFileName = strFileName.Substring(strFileName.LastIndexOf("/") + 1);
listBoxFile.Items.Add(strFileName);
}
fDlg.Dispose();
}
private void btnDestination_Click(object sender, System.EventArgs e)
...{
FolderBrowserDialog foldDlg = new FolderBrowserDialog();
if (foldDlg.ShowDialog() != DialogResult.OK)
...{
return;
}
txtDestination.Text = foldDlg.SelectedPath;
}
private void listBoxFile_SelectedIndexChanged(object sender, System.EventArgs e)
...{
string strFilePath;
if (listBoxFile.SelectedIndex < 0)
...{
return;
}
strFilePath = txtSource.Text + "/" + listBoxFile.Items[listBoxFile.SelectedIndex];
if (File.Exists(strFilePath))
...{
Image img = Image.FromFile(strFilePath);
panelDemo.BackgroundImage = img;
//img.Dispose();
}
}
private void btnFonts_Click(object sender, System.EventArgs e)
...{
FontDialog fontDlg = new FontDialog();
fontDlg.Font = titleFont;
if (fontDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
titleFont = fontDlg.Font;
fontDlg.Dispose();
Demo();
}
private void btnForeColor_Click(object sender, System.EventArgs e)
...{
ColorDialog colorDlg = new ColorDialog();
colorDlg.Color = foreColor;
if (colorDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
foreColor = colorDlg.Color;
colorDlg.Dispose();
Demo();
}
private void btnBackColor_Click(object sender, System.EventArgs e)
...{
ColorDialog colorDlg = new ColorDialog();
colorDlg.Color = backColor;
if (colorDlg.ShowDialog()!= DialogResult.OK)
...{
return;
}
backColor = colorDlg.Color;
colorDlg.Dispose();
Demo();
}
private void btnExcute_Click(object sender, System.EventArgs e)
...{
string strLogoTitle = lblDemo.Text;
if (strLogoTitle == string.Empty)
...{
MessageBox.Show("The LogoTitle can't be empty!");
txtDemo.Width = lblDemo.Width;
txtDemo.Height = lblDemo.Height;
txtDemo.Location = lblDemo.Location;
txtDemo.Font = lblDemo.Font;
if (lblDemo.BackColor != Color.Transparent)
txtDemo.BackColor = lblDemo.BackColor;
txtDemo.ForeColor = lblDemo.ForeColor;
lblDemo.Visible = false;
txtDemo.Visible = true;
txtDemo.Focus();
return;
}
if (listBoxFile.Items.Count < 1)
...{
MessageBox.Show("Please set the files that you want to add title to!");
btnSource.Select();
return;
}
if (txtDestination.Text == string.Empty)
...{
MessageBox.Show("Please set the destination fold that you want to save the images!");
btnDestination.Select();
return;
}
string fileName = "";
string FILE_NAME = "";
System.Drawing.Image image;
Bitmap bitmap;
Graphics g;
float rectX = lblDemo.Left;
float rectY = lblDemo.Top;
float rectWidth = lblDemo.Width;
float rectHeight = lblDemo.Height;
Brush frontBrush = new SolidBrush(foreColor);
Brush backBrush = new SolidBrush(backColor);
for (int ix = 0; ix < listBoxFile.Items.Count; ix++)
...{
fileName = txtSource.Text + "/";
fileName = fileName + listBoxFile.Items[ix].ToString();
if(!File.Exists(fileName))
...{
continue;
}
image = System.Drawing.Image.FromFile(fileName);
bitmap = new Bitmap(image,image.Width,image.Height);
g = Graphics.FromImage(bitmap);
// switch(cmbPosition.SelectedIndex)
// {
// case 0:
// rectX = 0;
// rectY = 0;
// break;
// case 1:
// rectX = image.Width - titleFont.Size * lblDemo.Text.Length;
// rectY = 0;
// break;
// case 2:
// rectX = image.Width / 2;
// rectY = image.Height / 2;
// break;
// case 3:
// rectX = 0;
// rectY = image.Height - titleFont.Size;
// break;
// case 4:
// rectX = image.Width - titleFont.Size * lblDemo.Text.Length;
// rectY = image.Height - titleFont.Size;
// break;
// default:
// rectX = 0;
// rectY = image.Height - titleFont.Size;
// break;
// }
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
g.FillRectangle(backBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(strLogoTitle,titleFont,frontBrush,textArea);
FILE_NAME = txtDestination.Text + "/";
FILE_NAME = FILE_NAME + listBoxFile.Items[ix].ToString();
bitmap.Save(FILE_NAME,ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
MessageBox.Show("complete!");
}
#endregion
Demo#region Demo
private void Demo()
...{
//int iGood = 0;
// int iTitleLen = System.Text.Encoding.GetEncoding("GB18030").GetByteCount(txtLogo.Text);
lblDemo.BackColor = backColor;
lblDemo.ForeColor = foreColor;
lblDemo.Font = titleFont;
// switch(cmbPosition.SelectedIndex)
// {
// case 0:
// lblDemo.Left = iGood;
// lblDemo.Top = iGood;
// break;
// case 1:
// lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
// lblDemo.Top = iGood;
// break;
// case 2:
// lblDemo.Left = panelDemo.Width / 2 - lblDemo.Width / 2;
// lblDemo.Top = panelDemo.Height / 2 - lblDemo.Height / 2;
// break;
// case 3:
// lblDemo.Left = iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// case 4:
// lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// default:
// lblDemo.Left = iGood;
// lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
// break;
// }
}
#endregion
sonota Event#region sonota Event
private void cmbPosition_SelectedIndexChanged(object sender, System.EventArgs e)
...{
int iGood = 0;
switch(cmbPosition.SelectedIndex)
...{
case 0:
lblDemo.Left = iGood;
lblDemo.Top = iGood;
break;
case 1:
lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
lblDemo.Top = iGood;
break;
case 2:
lblDemo.Left = panelDemo.Width / 2 - lblDemo.Width / 2;
lblDemo.Top = panelDemo.Height / 2 - lblDemo.Height / 2;
break;
case 3:
lblDemo.Left = iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
case 4:
lblDemo.Left = panelDemo.Width - lblDemo.Width - iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
default:
lblDemo.Left = iGood;
lblDemo.Top = panelDemo.Height - lblDemo.Height - iGood;
break;
}
}
private void txtLogo_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
Demo();
}
#endregion
Panel Event#region Panel Event
private void panelDemo_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (isOnSizing == false)
...{
panelDemo.Cursor = Cursors.Arrow;
}
if (lblDemo.Visible == true)
...{
int ioffset = 5;
//Left
if ((e.X <= lblDemo.Left && e.X >= lblDemo.Left - ioffset) &&
(e.Y <= lblDemo.Top + lblDemo.Height && e.Y >= lblDemo.Top))
...{
panelDemo.Cursor = Cursors.SizeWE;
}
//Right
if ((e.X >= lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + ioffset) &&
(e.Y <= lblDemo.Top + lblDemo.Height && e.Y >= lblDemo.Top))
...{
panelDemo.Cursor = Cursors.SizeWE;
}
//Top
if ((e.X >= lblDemo.Left && e.X <= lblDemo.Left + lblDemo.Width) &&
(e.Y >= lblDemo.Top - ioffset) && e.Y <= lblDemo.Top)
...{
panelDemo.Cursor = Cursors.SizeNS;
}
//Bottom
if ((e.X >= lblDemo.Left && e.X <=lblDemo.Left + lblDemo.Width) &&
(e.Y >= lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + ioffset))
...{
panelDemo.Cursor = Cursors.SizeNS;
}
//TopLeft
if ((e.X < lblDemo.Left && e.X >= lblDemo.Left - 2 * ioffset) &&
(e.Y < lblDemo.Top && e.Y >= lblDemo.Top - 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNWSE;
}
//TopRight
if ((e.X > lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + 2 * ioffset) &&
(e.Y < lblDemo.Top && e.Y >= lblDemo.Top - 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNESW;
}
//BottomLeft
if ((e.X < lblDemo.Left && e.X >= lblDemo.Left - 2 * ioffset) &&
(e.Y > lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNESW;
}
//BottomRight
if ((e.X > lblDemo.Left + lblDemo.Width && e.X <= lblDemo.Left + lblDemo.Width + 2 * ioffset) &&
(e.Y > lblDemo.Top + lblDemo.Height && e.Y <= lblDemo.Top + lblDemo.Height + 2 * ioffset))
...{
panelDemo.Cursor = Cursors.SizeNWSE;
}
}
if (isOnSizing == true)
...{
switch (iDirection)
...{
case 1:
//Left
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Left = e.X;
}
break;
case 2:
//Right
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
}
break;
case 3:
//Top
if (e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Top = e.Y;
}
break;
case 4:
//Bottom
if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
case 5:
//TopLeft
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else if(e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Left = e.X;
lblDemo.Top = e.Y;
}
break;
case 6:
//TopRight
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
if (e.Y > iTop + iHeight - 20)
...{
lblDemo.Height = 20;
lblDemo.Top = iTop + iHeight - 20;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
lblDemo.Height = (lblDemo.Top - e.Y) + lblDemo.Height;
lblDemo.Top = e.Y;
}
break;
case 7:
//BottomLeft
if (e.X > iLeft + iWidth - 5)
...{
lblDemo.Width = 5;
lblDemo.Left = iLeft + iWidth - 5;
}
else if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Width = (lblDemo.Left - e.X) + lblDemo.Width;
lblDemo.Left = e.X;
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
case 8:
//BottomRight
if (e.X < iLeft + 5)
...{
lblDemo.Width = 5;
}
if (e.Y < iTop + 20)
...{
lblDemo.Height = 20;
}
else
...{
lblDemo.Width = e.X - lblDemo.Left;
lblDemo.Height = e.Y - lblDemo.Top;
}
break;
default:
break;
}
}
}
private void panelDemo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (panelDemo.Cursor == Cursors.SizeWE ||
panelDemo.Cursor == Cursors.SizeNS ||
panelDemo.Cursor == Cursors.SizeNESW ||
panelDemo.Cursor == Cursors.SizeNWSE)
...{
isOnSizing = true;
iLeft = lblDemo.Left;
iTop = lblDemo.Top;
iWidth = lblDemo.Width;
iHeight = lblDemo.Height;
//Left
if (e.X <= lblDemo.Left && e.Y > lblDemo.Top && e.Y < lblDemo.Top + lblDemo.Height)
...{
iDirection = 1;
}
//Right
if (e.X >= lblDemo.Left + lblDemo.Width && e.Y > lblDemo.Top && e.Y < lblDemo.Top + lblDemo.Height)
...{
iDirection = 2;
}
//Top
if (e.Y <= lblDemo.Top && e.X > lblDemo.Left && e.X < lblDemo.Left + lblDemo.Width)
...{
iDirection = 3;
}
//Bottom
if (e.Y >= lblDemo.Top + lblDemo.Height && e.X > lblDemo.Left && e.X < lblDemo.Left + lblDemo.Width)
...{
iDirection = 4;
}
//TopLeft
if (e.X < lblDemo.Left && e.Y < lblDemo.Top)
...{
iDirection = 5;
}
//TopRight
if (e.Y < lblDemo.Top && e.X > lblDemo.Left + lblDemo.Width)
...{
iDirection = 6;
}
//BottomLeft
if (e.X < lblDemo.Left && e.Y > lblDemo.Top + lblDemo.Height)
...{
iDirection = 7;
}
//BottomRight
if (e.Y > lblDemo.Top + lblDemo.Height && e.X > lblDemo.Left + lblDemo.Width)
...{
iDirection = 8;
}
}
}
private void panelDemo_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
...{
panelDemo.Cursor = Cursors.Arrow;
iDirection = 0;
isOnSizing = false;
isOnLbl = false;
}
#endregion
Label Event#region Label Event
private void lblDemo_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
lblDemo.Cursor = Cursors.SizeAll;
if (isOnLbl == true)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else
...{
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
else if (Cursor.Position.X - iPanelLeft - iLblInLeft > panelDemo.Width - lblDemo.Width)
...{
lblDemo.Left = panelDemo.Width - lblDemo.Width;
if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else
...{
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop < 0)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
}
else
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
}
lblDemo.Top = 0;
}
else if (Cursor.Position.Y - iPanelTop - iLblInTop > panelDemo.Height - lblDemo.Height)
...{
if (Cursor.Position.X - iPanelLeft - iLblInLeft < 0)
...{
lblDemo.Left = 0;
}
else
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
}
lblDemo.Top = panelDemo.Height - lblDemo.Height;
}
else if (Cursor.Position.X - iPanelLeft - iLblInLeft > 0 &&
Cursor.Position.X - iPanelLeft - iLblInLeft < panelDemo.Width - lblDemo.Width &&
Cursor.Position.Y - iPanelTop - iLblInTop > 0 &&
Cursor.Position.Y - iPanelTop - iLblInTop < panelDemo.Height - lblDemo.Height)
...{
lblDemo.Left = Cursor.Position.X - iPanelLeft - iLblInLeft;
lblDemo.Top = Cursor.Position.Y - iPanelTop - iLblInTop;
}
}
}
private void lblDemo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
if (lblDemo.BorderStyle == BorderStyle.FixedSingle)
...{
isBorder = true;
}
else
...{
isBorder = false;
}
lblDemo.BorderStyle = BorderStyle.FixedSingle;
isOnLbl = true;
iLblInLeft = e.X;
iLblInTop = e.Y;
iLblX = lblDemo.Left;
iLblY = lblDemo.Top;
}
private void lblDemo_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
...{
isOnLbl = false;
if (isBorder == false && (lblDemo.Left != iLblX || lblDemo.Top != iLblY))
...{
lblDemo.BorderStyle = BorderStyle.None;
}
}
private void lblDemo_DoubleClick(object sender, System.EventArgs e)
...{
txtDemo.Width = lblDemo.Width;
txtDemo.Height = lblDemo.Height;
txtDemo.Location = lblDemo.Location;
txtDemo.Font = lblDemo.Font;
if (lblDemo.BackColor != Color.Transparent)
txtDemo.BackColor = lblDemo.BackColor;
txtDemo.ForeColor = lblDemo.ForeColor;
lblDemo.Visible = false;
txtDemo.Visible = true;
txtDemo.Focus();
}
private void txtDemo_Leave(object sender, System.EventArgs e)
...{
lblDemo.Visible = true;
txtDemo.Visible = false;
lblDemo.Text = txtDemo.Text;
}
private void lblDemo_Click(object sender, System.EventArgs e)
...{
lblDemo.BorderStyle = BorderStyle.FixedSingle;
}
private void panelDemo_Click(object sender, System.EventArgs e)
...{
if (lblDemo.Visible == true)
...{
lblDemo.BorderStyle = BorderStyle.None;
}
else if (txtDemo.Visible == true)
...{
lblDemo.Visible = true;
txtDemo.Visible = false;
lblDemo.Text = txtDemo.Text;
lblDemo.BorderStyle = BorderStyle.None;
}
}
#endregion
private void lblDemo_LocationChanged(object sender, System.EventArgs e)
...{
textBox1.Text = lblDemo.Left.ToString();
textBox2.Text = lblDemo.Top.ToString();
}
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
string strLeft = textBox1.Text;
for (int ix = 0; ix < strLeft.Length; ix++)
...{
if (Char.IsNumber(strLeft,ix) == false)
...{
MessageBox.Show("Please input a integer!");
textBox1.Select();
return;
}
}
int iLeft = Convert.ToInt32(strLeft);
if (iLeft < 0)
...{
iLeft = 0;
}
if (iLeft > panelDemo.Width - lblDemo.Width)
...{
iLeft = panelDemo.Width - lblDemo.Width;
}
lblDemo.Left = iLeft;
}
private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
...{
string strTop = textBox2.Text;
for (int ix = 0; ix < strTop.Length; ix++)
...{
if (Char.IsNumber(strTop,ix) == false)
...{
MessageBox.Show("Please input a integer!");
textBox2.Select();
return;
}
}
int iTop = Convert.ToInt32(strTop);
if (iTop < 0)
...{
iTop = 0;
}
if (iTop > panelDemo.Height - lblDemo.Height)
...{
iTop = panelDemo.Height - lblDemo.Height;
}
lblDemo.Top = iTop;
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
...{
if (checkBox1.Checked == true)
...{
btnBackColor.Enabled = false;
backColor = Color.Transparent;
Demo();
}
else
...{
btnBackColor.Enabled = true;
}
}
}
}