radi se o jednostavnom proogramu za crtanje. ima jedan prozor i 5 dugmadi kojima mjenjam boje.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace crta
{
//
//kratak opis za obrazac form
//
public class Form1 : System.Windows.Forms.Form
{
Bitmap bitmap = null;
Bitmap curBitmap = null;
Graphics curGraphics;
SolidBrush curBrush, black, red, blue, grin, grej, jelov;
Size fullSize;
int x, y;
bool write;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Neophodno za podršku Windows Forms Designera
//
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
///
/// Oslobodite korištene resurse.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(584, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Obriši sve";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.Black;
this.button2.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.button2.Location = new System.Drawing.Point(0, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Crna";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.Color.Red;
this.button3.Location = new System.Drawing.Point(64, 0);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 3;
this.button3.Text = "Crvena";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.BackColor = System.Drawing.Color.Blue;
this.button4.Location = new System.Drawing.Point(128, 0);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 23);
this.button4.TabIndex = 4;
this.button4.Text = "Plava";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.BackColor = System.Drawing.Color.Green;
this.button5.Location = new System.Drawing.Point(192, 0);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(64, 23);
this.button5.TabIndex = 5;
this.button5.Text = "Zelena";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.BackColor = System.Drawing.Color.Yellow;
this.button6.Location = new System.Drawing.Point(256, 0);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(64, 23);
this.button6.TabIndex = 6;
this.button6.Text = "Žuta";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(320, 0);
this.button7.Name = "button7";
this.button7.TabIndex = 7;
this.button7.Text = "Gumica";
this.button7.Click += new System.EventHandler(this.button7_Click);
this.button7.Paint += new System.Windows.Forms.PaintEventHandler(this.onPaint);
this.button7.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouseDown);
//
// button8
//
this.button8.Location = new System.Drawing.Point(456, 0);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(96, 23);
this.button8.TabIndex = 8;
this.button8.Text = "Bože Sacuvaj";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.LightGray;
this.ClientSize = new System.Drawing.Size(648, 266);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.HelpButton = true;
this.Name = "Form1";
this.Text = "CRTA!";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.mouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.onPaint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.mouseMove);
this.ResumeLayout(false);
}
#endregion
///
/// Glavna ulazna tocka za aplikaciju.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
string m_strFileName = "";
public void RefreshFormBackground()
{
// Clone the form and use it as a background image
curBitmap = bitmap.Clone(new Rectangle(0, 0, this.Width, this.Height),
bitmap.PixelFormat);
this.BackgroundImage = curBitmap;
}
private void Form1_Closed(object sender, System.EventArgs e)
{
curBrush.Dispose();
curGraphics.Dispose();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Get form size
fullSize = SystemInformation.PrimaryMonitorMaximizedWindowSize;
// Create a bitmap
bitmap = new Bitmap(fullSize.Width, fullSize.Height);
// Give the bitmap to a graphics object
curGraphics = Graphics.FromImage(bitmap);
// Use the background color of the form
curGraphics.Clear(this.BackColor);
black = new SolidBrush(Color.Black);
red = new SolidBrush(Color.Red);
blue = new SolidBrush(Color.Blue);
jelov = new SolidBrush(Color.Yellow);
grin = new SolidBrush(Color.Green);
grej = new SolidBrush(Color.LightGray);
curBrush = grej;
write = false;
}
private void button1_Click(object sender, System.EventArgs e)
{
curGraphics.Clear(this.BackColor);
RefreshFormBackground();
}
private void mouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// At every mouse mouse, get the current x and y position
// Capture the background
x = e.X;
y = e.Y;
RefreshFormBackground();
}
private void mouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
write = false;
}
private void mouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
x = e.X;
y = e.Y;
write = true;
}
private void onPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// When mouse is down, fill an ellipse at the current x and y position
if (write)
{
curGraphics.FillEllipse(curBrush, x, y, 5, 5);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
curBrush = black;
}
private void button3_Click(object sender, System.EventArgs e)
{
curBrush = red;
}
private void button4_Click(object sender, System.EventArgs e)
{
curBrush = blue;
}
private void button5_Click(object sender, System.EventArgs e)
{
curBrush = grin;
}
private void button6_Click(object sender, System.EventArgs e)
{
curBrush = jelov;
}
private void button7_Click(object sender, System.EventArgs e)
{
curBrush = grej;
}
private void button8_Click(object sender, System.EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
}
}
[Ovu poruku je menjao mostart dana 23.10.2006. u 16:11 GMT+1]
[Ovu poruku je menjao mostart dana 23.10.2006. u 16:12 GMT+1]