|
|
using System; using System.Windows.Forms; public class HelloWorld : Form { public static void Main() { Application.Run(new HelloWorld()); } public HelloWorld() { this.Text = "Hello World!"; } }
int left = 0, right = 0, result = 0; if ( leftTextBox.Text.Length > 0 ) { left = int.Parse( leftTextBox.Text ); } if ( rightTextBox.Text.Length > 0 ) { right = int.Parse( rightTextBox.Text ); } switch ( operatorComboBox.Text ) { case "-": result = left - right; break; case "+": result = left + right; break; case "*": result = left * right; break; case "/": if ( right == 0 ) { MessageBox.Show( "Divide by 0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } else { result = left / right; } break; } resultTextBox.Text = result.ToString();
string s = (sender as TextBox).Text; if (s.Length > 0) { int res; if (!int.TryParse(s, out res)) { errorProvider.SetError(resultTextBox, "Not a number!"); return; } } errorProvider.SetError(resultTextBox, "");
string s = (sender as TextBox).Text; if (s.Length > 0) { int res; if (!int.TryParse(s, out res)) { errorProvider.SetError(resultTextBox, "Not a number!"); e.Cancel = true; return; } } errorProvider.SetError(resultTextBox, "");notice the difference
if ( folderBrowserDialog.ShowDialog() == DialogResult.OK ) { try { filesListView.Items.Clear(); DirectoryInfo di = new DirectoryInfo( folderBrowserDialog.SelectedPath ); Environment.CurrentDirectory = folderBrowserDialog.SelectedPath; FileInfo []files = di.GetFiles(); foreach ( FileInfo file in files ) { filesListView.Items.Add( file.Name ); } } catch ( Exception ) { } }
filesListView.View = View.List;
ListView.SelectedListViewItemCollection selected = filesListView.SelectedItems; if ( selected.Count == 0 ) { pictureBox.Visible = notImageLabel.Visible = false; } else { string file = selected[0].Text; try { pictureBox.Image = Image.FromFile( file ); pictureBox.Visible = true; notImageLabel.Visible = false; } catch ( Exception ) { pictureBox.Visible = false; notImageLabel.Visible = true; } }
nameToolStripStatusLabel.Text = Environment.CurrentDirectory + '\\' + file; sizeToolStripStatusLabel.Text = string.Format( "{0} x {1}", pictureBox.Image.Width, pictureBox.Image.Height );
imagePanel.AutoScrollMinSize = new Size( pictureBox.Image.Width, pictureBox.Image.Height );
if ( pictureBox.Image != null ) { Cursor = Cursors.WaitCursor; Color clr; int v; Bitmap bmp = new Bitmap( pictureBox.Image ); for ( int x = 0; x < bmp.Width; x++ ) { for ( int y = 0; y < bmp.Height; y++ ) { clr = bmp.GetPixel( x, y ); v = ( clr.R + clr.G + clr.B ) / 3; bmp.SetPixel( x, y, Color.FromArgb( v, v, v )); } } pictureBox.Image = bmp; Cursor = Cursors.Arrow; }
AboutForm about = new AboutForm(); about.ShowDialog();
e.Graphics.FillRectangle( Brushes.White, 0, 0, ClientRectangle.Width, ClientRectangle.Height ); Font font = new Font( "Verdana", 52, FontStyle.Bold ); Brush specialBrush = new LinearGradientBrush( ClientRectangle, Color.Yellow, Color.Red, 45, false ); StringFormat sf = (StringFormat)StringFormat.GenericDefault.Clone(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString( "I'm painting!", font, specialBrush, ClientRectangle.Right/2, ClientRectangle.Bottom/2, sf );
const int ballRadius = 50; Point ballCenter = new Point( ballRadius, ballRadius ); int ballSpeed = 5;
ballCenter.X += ballSpeed; if ( ballCenter.X < ballRadius ) { ballCenter.X = ballRadius; ballSpeed = Math.Abs( ballSpeed ); } else if ( ballCenter.X > ClientRectangle.Width - ballRadius ) { ballCenter.X = ClientRectangle.Width - ballRadius; ballSpeed = -Math.Abs( ballSpeed ); } Invalidate();
e.Graphics.FillEllipse( Brushes.Black, ballCenter.X - ballRadius, ballCenter.Y - ballRadius, 2*ballRadius, 2*ballRadius );
DoubleBuffered = true;
private Bitmap bmpToGrayscale = null;
Color clr; int v; for (int x = 0; x < bmpToGrayscale.Width; x++) { for (int y = 0; y < bmpToGrayscale.Height; y++) { clr = bmpToGrayscale.GetPixel(x, y); v = (clr.R + clr.G + clr.B) / 3; bmpToGrayscale.SetPixel(x, y, Color.FromArgb(v, v, v)); } }
pictureBox.Image = bmpToGrayscale; grayscaleToolStripMenuItem.Enabled = true;
if (pictureBox.Image != null) { bmpToGrayscale = new Bitmap(pictureBox.Image); grayscaleToolStripMenuItem.Enabled = false; backgroundWorker.RunWorkerAsync(); }
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;had been generated for you
private Point[] points = new Point[4]; public Point[] Points { get { return points; } set { points = value; Invalidate(); } }and initialization code to the constructor
points[0].X = 100; points[0].Y = 100; points[1].X = 100; points[1].Y = 200; points[2].X = 200; points[2].Y = 200; points[3].X = 200; points[3].Y = 100;
base.OnPaint(e); GraphicsPath gp = new GraphicsPath(); gp.AddClosedCurve(points); e.Graphics.DrawPath(Pens.Black, gp); gp.Dispose(); for(int i = 0; i < 4; i++) { e.Graphics.FillRectangle(Brushes.Red, Points[i].X - 5, Points[i].Y - 5, 10, 10); }
private int x, y; private int movePoint = -1; protected override void OnMouseDown(MouseEventArgs e) { x = e.X; y = e.Y; if(e.Button == MouseButtons.Left) { movePoint = PointByXY(e.X, e.Y); this.Capture = false; } base.OnMouseDown(e); } protected override void OnMouseMove(MouseEventArgs e) { if(movePoint >= 0 && e.Button == MouseButtons.Left) { Points[movePoint].X = e.X; Points[movePoint].Y = e.Y; Invalidate(); } base.OnMouseMove(e); } protected override void OnMouseUp(MouseEventArgs e) { if(movePoint >= 0 && e.Button == MouseButtons.Left) { Points[movePoint].X = e.X; Points[movePoint].Y = e.Y; movePoint = -1; Invalidate(); } base.OnMouseUp(e); } internal int PointByXY(int x, int y) { for(int i = 0; i < points.Length; i++) { if(x >= Points[i].X - 5 && x <= Points[i].X + 5 && y >= Points[i].Y - 5 && y <= Points[i].Y + 5) { return i; } } return -1; }
public class MyEditor : UITypeEditor { private IWindowsFormsEditorService edSvc = null; public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { if(context != null && context.Instance != null && provider != null) { edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if(edSvc != null) { MyControl orgControl = (MyControl)context.Instance; MyControl propertiesControl = new MyControl(); propertiesControl.Width = orgControl.Width; propertiesControl.Height = orgControl.Height; Array.Copy(orgControl.Points, propertiesControl.Points, 4); edSvc.DropDownControl(propertiesControl); return propertiesControl.Points; } } return value; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } }
[Browsable(true)] [Category("Image")] [Editor(typeof(MyEditor), typeof(UITypeEditor))]
private MyControl component; private int movePoint = -1;and initialization code
public override void Initialize(IComponent component) { this.component = component as MyControl; base.Initialize(component); }
protected override void OnMouseDragBegin(int x, int y) { if (MyControl.MouseButtons == MouseButtons.Left) { Point p = component.PointToClient(new Point(x, y)); movePoint = component.PointByXY(p.X, p.Y); if(movePoint >= 0) { component.Capture = true; } } if (movePoint < 0) { base.OnMouseDragBegin(x, y); } } protected override void OnMouseDragMove(int x, int y) { if (movePoint >= 0) { Point p = component.PointToClient(new Point(x, y)); component.Points[movePoint].X = p.X; component.Points[movePoint].Y = p.Y; component.Invalidate(); } else { base.OnMouseDragMove(x, y); } } protected override void OnMouseDragEnd(bool cancel) { if(movePoint >= 0) { movePoint = -1; component.Capture = false; TypeDescriptor.GetProperties(component)["Points"].SetValue(component, component.Points); } else { base.OnMouseDragEnd(cancel); } }
[Designer(typeof(MyDesigner))]
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if(sourceType == typeof(string)) { return true; } else { return base.CanConvertFrom(context, sourceType); } } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if(destinationType == typeof(string)) { return true; } else { return base.CanConvertTo(context, destinationType); } } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if(value is string) { try { string[] pointsString = ((string)value).Split(','); List<Point> points = new List<Point>(); foreach(string s in pointsString) { string tmp = s.TrimStart('(', ' ').TrimEnd(')', ' '); string[] pointCoordinates = tmp.Split(';'); points.Add(new Point(int.Parse(pointCoordinates[0]), int.Parse(pointCoordinates[1]))); } return points.ToArray(); } catch(Exception ex) { throw new FormatException(ex.Message, ex); } } else { return base.ConvertFrom(context, culture, value); } } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if(destinationType == typeof(string)) { try { Point[] points = (Point[])value; StringBuilder sb = new StringBuilder(); for(int i = 0; i < points.Length; i++) { sb.AppendFormat("({0}; {1})", points[i].X, points[i].Y); if(i < points.Length - 1) { sb.Append(','); } } return sb.ToString(); } catch(Exception ex) { throw new FormatException(ex.Message, ex); } } else { return base.ConvertTo(context, culture, value, destinationType); } }
[TypeConverter(typeof(MyTypeConverter))]
[ToolboxBitmap(typeof(MyControl), "tool.bmp")]