Csharp/C Sharp by API/System.Windows.Forms/PictureBox
Содержание
extends PictureBox
using System;
using System.Drawing;
using System.Windows.Forms;
class PictureBoxPlusDemo: Form
{
public static void Main()
{
Application.Run(new PictureBoxPlusDemo());
}
public PictureBoxPlusDemo()
{
Text = "PictureBoxPlus Demo";
PictureBoxPlus picbox = new PictureBoxPlus();
picbox.Parent = this;
picbox.Dock = DockStyle.Fill;
picbox.Image = Image.FromFile("Color.jpg");
}
}
class PictureBoxPlus: PictureBox
{
protected override void OnPaint(PaintEventArgs pea)
{
ScaleImageIsotropically(pea.Graphics, Image,ClientRectangle);
}
void ScaleImageIsotropically(Graphics grfx, Image image,
Rectangle rect)
{
SizeF sizef =
new SizeF(image.Width / image.HorizontalResolution,
image.Height / image.VerticalResolution);
float fScale = Math.Min(rect.Width / sizef.Width,
rect.Height / sizef.Height);
sizef.Width *= fScale;
sizef.Height *= fScale;
grfx.DrawImage(image,
rect.X + (rect.Width - sizef.Width ) / 2,
rect.Y + (rect.Height - sizef.Height) / 2,
sizef.Width, sizef.Height);
}
}
PictureBox.BorderStyle
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
public class PictureBoxTest : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox imagePictureBox;
private System.Windows.Forms.Label promptLabel;
public PictureBoxTest()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.promptLabel = new System.Windows.Forms.Label();
this.imagePictureBox = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
this.promptLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.promptLabel.Location = new System.Drawing.Point(22, 8);
this.promptLabel.Name = "promptLabel";
this.promptLabel.Size = new System.Drawing.Size(124, 56);
this.promptLabel.TabIndex = 0;
this.promptLabel.Text = "Click On PictureBox to View Images";
this.promptLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.imagePictureBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.imagePictureBox.Location = new System.Drawing.Point(34, 72);
this.imagePictureBox.Name = "imagePictureBox";
this.imagePictureBox.Size = new System.Drawing.Size(100, 100);
this.imagePictureBox.TabIndex = 1;
this.imagePictureBox.TabStop = false;
this.imagePictureBox.Click += new System.EventHandler(this.imagePictureBox_Click );
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(168, 189);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.imagePictureBox,
this.promptLabel});
this.Text = "PictureBoxTest";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run( new PictureBoxTest() );
}
private void imagePictureBox_Click(object sender, System.EventArgs e )
{
imagePictureBox.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\winter.jpg" );
}
}
PictureBox.Cursor
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private bool isDragging = false;
private int currentX, currentY;
Rectangle dropRect = new Rectangle(180, 180, 60, 60);
private PictureBox myPictureBox;
public Form1()
{
InitializeComponent();
CenterToScreen();
myPictureBox = new PictureBox();
myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
myPictureBox.Location = new System.Drawing.Point(64, 32);
myPictureBox.Size = new System.Drawing.Size(50, 50);
myPictureBox.Image = new Bitmap("winter.jpg");
myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown);
myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp);
myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove);
myPictureBox.Cursor = Cursors.Hand;
Controls.Add(myPictureBox);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Dragging Images";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
currentX = e.X;
currentY = e.Y;
}
private void myPictureBox_MouseMove(object sender, MouseEventArgs e) {
if (isDragging) {
myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
}
}
private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
Console.WriteLine(dropRect.Contains(myPictureBox.Bounds));
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.AntiqueWhite, dropRect);
g.DrawString("Drag and drop the image here.", new Font("Times New Roman", 8), Brushes.Red, dropRect);
}
}
PictureBox.DoubleClick
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.GroupBox GroupBox1;
private System.Windows.Forms.Label Label4;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.PictureBox pic;
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.Button cmd;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label Label3;
private System.Windows.Forms.ListBox eventLogList;
public Form1() {
InitializeComponent();
}
private void Log(String data)
{
eventLogList.Items.Add(data);
int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.eventLogList = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.GroupBox1.Controls.Add(this.Label4);
this.GroupBox1.Controls.Add(this.Label1);
this.GroupBox1.Controls.Add(this.pic);
this.GroupBox1.Controls.Add(this.txt);
this.GroupBox1.Controls.Add(this.cmd);
this.GroupBox1.Controls.Add(this.Label2);
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(7, 0);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 12;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(23, 100);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 11;
this.Label3.Text = "Label3";
//
// eventLogList
//
this.eventLogList.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.eventLogList.FormattingEnabled = true;
this.eventLogList.IntegralHeight = false;
this.eventLogList.Location = new System.Drawing.Point(7, 156);
this.eventLogList.Name = "eventLogList";
this.eventLogList.Size = new System.Drawing.Size(384, 212);
this.eventLogList.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 374);
this.Controls.Add(this.GroupBox1);
this.Controls.Add(this.Label3);
this.Controls.Add(this.eventLogList);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.GroupBox1.PerformLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
PictureBox.Image
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace PicControls
{
/// <summary>
/// Summary description for PicControls.
/// </summary>
public class PicControls : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox p;
private System.Windows.Forms.Panel PicPanel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public PicControls()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.p = new System.Windows.Forms.PictureBox();
this.PicPanel = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// p
//
this.p.Location = new System.Drawing.Point(32, 40);
this.p.Name = "p";
this.p.Size = new System.Drawing.Size(136, 104);
this.p.TabIndex = 0;
this.p.TabStop = false;
//
// PicPanel
//
this.PicPanel.Location = new System.Drawing.Point(200, 120);
this.PicPanel.Name = "PicPanel";
this.PicPanel.Size = new System.Drawing.Size(200, 184);
this.PicPanel.TabIndex = 1;
//
// PicControls
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.PicPanel,
this.p});
this.Name = "PicControls";
this.Text = "PicControls";
this.Load += new System.EventHandler(this.PicControls_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new PicControls());
}
private void PicControls_Load(object sender, System.EventArgs e)
{
Bitmap b = new Bitmap("crane.jpg");
//PictureBox is "p"
p.Image = (Image)b;
}
}
}
PictureBox.MouseDown
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private bool isDragging = false;
private int currentX, currentY;
Rectangle dropRect = new Rectangle(180, 180, 60, 60);
private PictureBox myPictureBox;
public Form1()
{
InitializeComponent();
CenterToScreen();
myPictureBox = new PictureBox();
myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
myPictureBox.Location = new System.Drawing.Point(64, 32);
myPictureBox.Size = new System.Drawing.Size(50, 50);
myPictureBox.Image = new Bitmap("winter.jpg");
myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown);
myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp);
myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove);
myPictureBox.Cursor = Cursors.Hand;
Controls.Add(myPictureBox);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Dragging Images";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
currentX = e.X;
currentY = e.Y;
}
private void myPictureBox_MouseMove(object sender, MouseEventArgs e) {
if (isDragging) {
myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
}
}
private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
Console.WriteLine(dropRect.Contains(myPictureBox.Bounds));
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.AntiqueWhite, dropRect);
g.DrawString("Drag and drop the image here.", new Font("Times New Roman", 8), Brushes.Red, dropRect);
}
}
PictureBox.MouseEnter
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace EventTracker
{
/// <summary>
/// Summary description for EventTracker.
/// </summary>
public class EventTracker : System.Windows.Forms.Form
{
internal System.Windows.Forms.GroupBox GroupBox1;
internal System.Windows.Forms.Label Label4;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.PictureBox pic;
internal System.Windows.Forms.TextBox txt;
internal System.Windows.Forms.Button cmd;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label3;
internal System.Windows.Forms.ListBox lstLog;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public EventTracker()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.lstLog = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Label4,
this.Label1,
this.pic,
this.txt,
this.cmd,
this.Label2});
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(8, 5);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 9;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.Text = "";
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(24, 105);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 8;
this.Label3.Text = "Label3";
//
// lstLog
//
this.lstLog.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.lstLog.IntegralHeight = false;
this.lstLog.Location = new System.Drawing.Point(8, 161);
this.lstLog.Name = "lstLog";
this.lstLog.Size = new System.Drawing.Size(384, 212);
this.lstLog.TabIndex = 7;
//
// EventTracker
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(400, 378);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.GroupBox1,
this.Label3,
this.lstLog});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "EventTracker";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Log(String data)
{
lstLog.Items.Add(data);
int itemsPerPage = (int)(lstLog.Height / lstLog.ItemHeight);
lstLog.TopIndex = lstLog.Items.Count - itemsPerPage;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new EventTracker());
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + "Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + "Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
}
}
PictureBox.MouseMove
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private bool isDragging = false;
private int currentX, currentY;
Rectangle dropRect = new Rectangle(180, 180, 60, 60);
private PictureBox myPictureBox;
public Form1()
{
InitializeComponent();
CenterToScreen();
myPictureBox = new PictureBox();
myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
myPictureBox.Location = new System.Drawing.Point(64, 32);
myPictureBox.Size = new System.Drawing.Size(50, 50);
myPictureBox.Image = new Bitmap("winter.jpg");
myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown);
myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp);
myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove);
myPictureBox.Cursor = Cursors.Hand;
Controls.Add(myPictureBox);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Dragging Images";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
currentX = e.X;
currentY = e.Y;
}
private void myPictureBox_MouseMove(object sender, MouseEventArgs e) {
if (isDragging) {
myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
}
}
private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
Console.WriteLine(dropRect.Contains(myPictureBox.Bounds));
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.AntiqueWhite, dropRect);
g.DrawString("Drag and drop the image here.", new Font("Times New Roman", 8), Brushes.Red, dropRect);
}
}
PictureBox.MouseUp
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.GroupBox GroupBox1;
private System.Windows.Forms.Label Label4;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.PictureBox pic;
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.Button cmd;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label Label3;
private System.Windows.Forms.ListBox eventLogList;
public Form1() {
InitializeComponent();
}
private void Log(String data)
{
eventLogList.Items.Add(data);
int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.eventLogList = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.GroupBox1.Controls.Add(this.Label4);
this.GroupBox1.Controls.Add(this.Label1);
this.GroupBox1.Controls.Add(this.pic);
this.GroupBox1.Controls.Add(this.txt);
this.GroupBox1.Controls.Add(this.cmd);
this.GroupBox1.Controls.Add(this.Label2);
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(7, 0);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 12;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(23, 100);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 11;
this.Label3.Text = "Label3";
//
// eventLogList
//
this.eventLogList.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.eventLogList.FormattingEnabled = true;
this.eventLogList.IntegralHeight = false;
this.eventLogList.Location = new System.Drawing.Point(7, 156);
this.eventLogList.Name = "eventLogList";
this.eventLogList.Size = new System.Drawing.Size(384, 212);
this.eventLogList.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 374);
this.Controls.Add(this.GroupBox1);
this.Controls.Add(this.Label3);
this.Controls.Add(this.eventLogList);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.GroupBox1.PerformLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
PictureBox.TabStop
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button cmdCapture;
private System.Windows.Forms.PictureBox pictureBox1;
public Form1() {
InitializeComponent();
}
private void cmdCapture_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
g.Dispose();
pictureBox1.Image = bmp;
pictureBox1.Size = bmp.Size;
}
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.cmdCapture = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
this.panel1.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.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Size = new System.Drawing.Size(270, 233);
this.cmdCapture.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.cmdCapture.Location = new System.Drawing.Point(169, 249);
this.cmdCapture.Size = new System.Drawing.Size(110, 30);
this.cmdCapture.Text = "Capture Screen";
this.cmdCapture.UseVisualStyleBackColor = true;
this.cmdCapture.Click += new System.EventHandler(this.cmdCapture_Click);
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabStop = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(290, 288);
this.Controls.Add(this.cmdCapture);
this.Controls.Add(this.panel1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Text = "Screen Capture";
this.panel1.ResumeLayout(false);
((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
PictureBox.Visible
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class ListText : Form {
private Button choose = new Button();
private ListBox animalList = new ListBox( );
private ListBox things = new ListBox( );
private TextBox text = new TextBox( );
private PictureBox picture = new PictureBox();
private PictureBox photo = new PictureBox();
private CheckBox show = new CheckBox();
private CheckBox author = new CheckBox();
private Bitmap moon = new Bitmap("winter.jpg");
public ListText( ) {
Text = "List Text";
choose.Text = "Choose";
show.Text = "Show a bitmap";
author.Text = "Show another bitmap";
Size = new Size(400, 300);
choose.Size = new Size(100,20);
text.Size = new Size(150,50);
photo.Size = new Size(100,100);
choose.Location = new Point(20,30);
animalList.Location = new Point(30 + choose.Width, 30);
things.Location = new Point(40 + choose.Width + animalList.Width, 30);
text.Location = new Point(20, 150);
photo.Location = new Point(40 + text.Width, 150);
picture.Location = new Point(60 + text.Width + photo.Width, 150);
show.Location = new Point(20,70);
author.Location = new Point(20,110);
animalList.SelectionMode = SelectionMode.MultiSimple;
things.SelectionMode = SelectionMode.One;
text.Multiline = true;
picture.Image = (Image)moon;
picture.Visible = false;
photo.Image = Image.FromFile("winter.jpg");
photo.Visible = false;
BackColor = Color.White;
choose.BackColor = Color.Pink;
animalList.Items.Add("A");
animalList.Items.Add("B");
animalList.Items.Add("C");
animalList.Items.Add("D");
animalList.Items.Add("E");
things.Items.Add("1");
things.Items.Add("2");
things.Items.Add("3");
things.Items.Add("4");
Controls.Add(animalList);
Controls.Add(things);
Controls.Add(choose);
Controls.Add(text);
Controls.Add(picture);
Controls.Add(show);
Controls.Add(author);
Controls.Add(photo);
choose.Click += new EventHandler(Choose_Click);
things.SelectedIndexChanged += new EventHandler(Things_Changed);
show.CheckedChanged += new EventHandler(Picture_Changed);
author.CheckedChanged += new EventHandler(Photo_Changed);
}
protected void Choose_Click(object sender, EventArgs e) {
for(int i = 0; i < animalList.SelectedItems.Count; i++){
Console.WriteLine(animalList.SelectedItems[i].ToString());
}
}
protected void Things_Changed(object sender, EventArgs e) {
text.Text = "You selected " + things.SelectedItem;
}
protected void Picture_Changed(Object sender, EventArgs e) {
if (show.Checked)
picture.Visible = true;
else
picture.Visible = false;
Invalidate();
}
protected void Photo_Changed(Object sender, EventArgs e) {
if (author.Checked)
photo.Visible = true;
else
photo.Visible = false;
Invalidate();
}
static void Main() {
Application.Run(new ListText());
}
}