|
|
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 ) { try { int v = int.Parse( s ); errorProvider.SetError( resultTextBox, "" ); } catch ( Exception ) { errorProvider.SetError( resultTextBox, "Not a number!" ); } } else { errorProvider.SetError( resultTextBox, "" ); }
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; } }
nameStatusBarPanel.Text = Environment.CurrentDirectory + '\\' + file; sizeStatusBarPanel.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 );
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles();
private Bitmap bmpToGrayscale = null; private EventHandler onGrayscaleComplete;
private void OnGrayscaleComplete( object sender, EventArgs e ) { pictureBox.Image = bmpToGrayscale; grayscaleMenuItem.Enabled = true; } private void ThreadProc() { 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 )); } } BeginInvoke( onGrayscaleComplete, new object[] { this, EventArgs.Empty } ); }
onGrayscaleComplete = new EventHandler( OnGrayscaleComplete );
if ( pictureBox.Image != null ) { bmpToGrayscale = new Bitmap( pictureBox.Image ); grayscaleMenuItem.Enabled = false; Thread thread = new Thread( new ThreadStart( ThreadProc )); thread.Start(); }