using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Color c1 = Color.FromArgb(100, Color.Blue);
Color c2 = Color.FromArgb(50, Color.Green);
g.FillEllipse(Brushes.Red, 20, 20, 80, 80);
g.FillRectangle(new SolidBrush(c1), 60, 80, 60, 60);
Point[] pa = new Point[] {
new Point(150, 40),
new Point(90, 40),
new Point(90, 120)};
g.FillPolygon(new SolidBrush(c2), pa);
}
public static void Main() {
Application.Run(new Form1());
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Five yellow squares with different alpha values(Transparensy)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g;
g = Graphics.FromHwnd(this.Handle);
g.FillRectangle(new SolidBrush(Color.Red), 10, 10, 210, 50);
//
Rectangle r = new Rectangle(40, 20, 30, 30);
Color c = Color.FromArgb(255, 255, 255, 0);
g.FillRectangle(new SolidBrush(c), r);
r.Offset(30, 0);
c = Color.FromArgb(200, 255, 255, 0);
g.FillRectangle(new SolidBrush(c), r);
r.Offset(30, 0);
c = Color.FromArgb(150, 255, 255, 0);
g.FillRectangle(new SolidBrush(c), r);
r.Offset(30, 0);
c = Color.FromArgb(100, 255, 255, 0);
g.FillRectangle(new SolidBrush(c), r);
r.Offset(30, 0);
c = Color.FromArgb(50, 255, 255, 0);
g.FillRectangle(new SolidBrush(c), r);
g.Dispose();
}
}
Known Colors
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace KnownColors
{
///
///
public class KnownColors : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
///
///
private System.ComponentModel.Container components = null;
ArrayList cAL;
ArrayList cNAL;
public KnownColors()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = “Known non-system colors”;
cAL = new ArrayList(); // colors
cNAL = new ArrayList(); // strings
NonSystemColors(cAL, cNAL);
this.comboBox1.Sorted = true;
this.comboBox1.DataSource = cNAL; //set the combo's data source
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(8, 8);
this.comboBox1.Name = “comboBox1”;
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = “comboBox1”;
//
// KnownColors
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1});
this.Name = “KnownColors”;
this.Text = “KnownColors”;
this.ResumeLayout(false);
}
#endregion
///
///
[STAThread]
static void Main()
{
Application.Run(new KnownColors());
}
private void NonSystemColors(ArrayList cAL, ArrayList cNAL)
{
Array cA = Enum.GetValues(typeof(KnownColor));
foreach(KnownColor knwnC in cA) // cX.Length = 167
{
Color curC = Color.FromKnownColor(knwnC);
if(!curC.IsSystemColor)
{
cAL.Add(curC);
cNAL.Add(curC.Name.ToString());
}
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics;
int wi = 70, hi = 12, rectNb = 8;
int cALNb = cAL.Count;
this.Width = (wi +2)*rectNb + 9;
int y = (int)(cALNb / rectNb);
this.Height = y*(2 + hi) + 60;
DisplayKnownColors(g, cALNb, wi, hi, rectNb);
g.Dispose();
}
private void DisplayKnownColors(Graphics g, int cALNb, int wi, int hi, int rectNb)
{
Rectangle rec;
Pen p = new Pen(this.ForeColor);
Brush b;
StringFormat strfmt = new StringFormat();
strfmt.LineAlignment = strfmt.Alignment = StringAlignment.Near;
int x, y;
for (int i = 0; i < cALNb; i++)
{
x = (int)(i % rectNb);
y = (int)(i / rectNb);
rec = new Rectangle(1 + x*(2 + wi), 1 + y*(2 + hi), wi, hi);
g.DrawRectangle(p, rec);
b = new SolidBrush((Color)cAL[i]);
g.FillRectangle(b, rec);
b = new SolidBrush(Color.Black);
g.DrawString((string)cNAL[i], this.Font, b, rec, strfmt);
}
x = (int)(cALNb % rectNb);
y = (int)(cALNb / rectNb);
this.comboBox1.Location = new Point(x*(wi + 2) + 2, y*(2 + hi) + 2);
}
}
}
[/csharp]
Color Changer
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ColorChanger
{
public class ColorChanger : System.Windows.Forms.Form
{
internal System.Windows.Forms.Label lblSaturation;
internal System.Windows.Forms.Label lblHue;
internal System.Windows.Forms.Label lblBrightness;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.ListBox lstColors;
private System.ComponentModel.Container components = null;
public ColorChanger()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.lblSaturation = new System.Windows.Forms.Label();
this.lblHue = new System.Windows.Forms.Label();
this.lblBrightness = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.lstColors = new System.Windows.Forms.ListBox();
this.SuspendLayout();
this.lblSaturation.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblSaturation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblSaturation.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblSaturation.Location = new System.Drawing.Point(264, 56);
this.lblSaturation.Name = "lblSaturation";
this.lblSaturation.Size = new System.Drawing.Size(136, 20);
this.lblSaturation.TabIndex = 9;
this.lblSaturation.Text = " Saturation";
this.lblHue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblHue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblHue.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblHue.Location = new System.Drawing.Point(264, 32);
this.lblHue.Name = "lblHue";
this.lblHue.Size = new System.Drawing.Size(136, 20);
this.lblHue.TabIndex = 8;
this.lblHue.Text = " Hue";
this.lblBrightness.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblBrightness.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblBrightness.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblBrightness.Location = new System.Drawing.Point(264, 8);
this.lblBrightness.Name = "lblBrightness";
this.lblBrightness.Size = new System.Drawing.Size(136, 20);
this.lblBrightness.TabIndex = 7;
this.lblBrightness.Text = " Brightness";
this.Label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.Label1.Location = new System.Drawing.Point(8, 8);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(200, 20);
this.Label1.TabIndex = 6;
this.Label1.Text = " Choose a Background Color:";
this.lstColors.Location = new System.Drawing.Point(8, 36);
this.lstColors.Name = "lstColors";
this.lstColors.Size = new System.Drawing.Size(200, 238);
this.lstColors.TabIndex = 5;
this.lstColors.SelectedIndexChanged += new System.EventHandler(this.lstColors_SelectedIndexChanged);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(472, 290);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblSaturation,
this.lblHue,
this.lblBrightness,
this.Label1,
this.lstColors});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "ColorChanger";
this.Text = "Color Changer";
this.Load += new System.EventHandler(this.ColorChanger_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new ColorChanger());
}
private void ColorChanger_Load(object sender, System.EventArgs e)
{
string[] colorNames;
colorNames = System.Enum.GetNames(typeof(KnownColor));
lstColors.Items.AddRange(colorNames);
}
private void lstColors_SelectedIndexChanged(object sender, System.EventArgs e)
{
KnownColor selectedColor;
selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), lstColors.Text);
this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);
lblBrightness.Text = "Brightness = " +this.BackColor.GetBrightness().ToString();
lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
lblSaturation.Text = "Saturation = " + this.BackColor.GetSaturation().ToString();
}
}
}
All the colors that are supported in C# according
//ColorGuide.cs
/*
This Program will generate all the colors that are supported in C# according
to the Name....
I expected from you a better version of this program
If you can , then inform me how you made it
e.g. you make a dll file where the whole color array is stored then
your coding lines will decrease. I tried that way but the trial was
in vane....
*/
using System;
using System.Windows.Forms;
using System.Drawing;
public class CreateMyPanel : Form
{
Color[] color = new Color[]{
Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine,
Color.Azure, Color.Beige, Color.Bisque, Color.Black,
Color.BlanchedAlmond,Color.Blue, Color.BlueViolet,Color.Brown,
Color.BurlyWood, Color.CadetBlue, Color.Chartreuse,Color.Chocolate,
Color.Coral, Color.Cornsilk,Color.Crimson,
Color.Cyan, Color.DarkBlue, Color.DarkCyan,Color.DarkGoldenrod,
Color.DarkGray, Color.DarkGreen, Color.DarkKhaki,Color.DarkMagenta,
Color.DarkOliveGreen,Color.DarkOrange,Color.DarkOrchid,Color.DarkRed,
Color.DarkSalmon,Color.DarkSeaGreen,Color.DarkSlateBlue,Color.DarkSlateGray,
Color.DarkTurquoise,Color.DarkViolet,Color.DeepPink,Color.DeepSkyBlue,
Color.DimGray,Color.DodgerBlue,Color.Firebrick,Color.FloralWhite,
Color.ForestGreen,Color.Fuchsia,Color.Gainsboro,Color.GhostWhite,
Color.Gold,Color.Goldenrod,Color.Gray,Color.Green,Color.GreenYellow,
Color.Honeydew,Color.HotPink,Color.IndianRed,Color.Indigo,
Color.Ivory,Color.Khaki,Color.Lavender,Color.LavenderBlush,
Color.LawnGreen,Color.LemonChiffon,Color.LightBlue,Color.LightCoral,
Color.LightCyan,Color.LightGoldenrodYellow,Color.LightGray,
Color.LightGreen,Color.LightPink,Color.LightSalmon,Color.LightSeaGreen,
Color.LightSkyBlue,Color.LightSlateGray,Color.LightSteelBlue,
Color.LightYellow,Color.Lime,Color.LimeGreen,Color.Linen,
Color.Magenta,Color.Maroon,Color.MediumAquamarine,Color.MediumBlue,
Color.MediumOrchid,Color.MediumPurple,Color.MediumSeaGreen,
Color.MediumSlateBlue,Color.MediumSpringGreen,Color.MediumTurquoise,
Color.MediumVioletRed,Color.MidnightBlue,Color.MintCream,Color.MistyRose,
Color.Moccasin,Color.NavajoWhite,Color.Navy,Color.OldLace,
Color.Olive,Color.OliveDrab,Color.Orange,Color.OrangeRed,
Color.Orchid,Color.PaleGoldenrod,Color.PaleGreen,Color.PaleTurquoise,
Color.PaleVioletRed,Color.PapayaWhip,Color.PeachPuff,Color.Peru,
Color.Pink,Color.Plum,Color.PowderBlue,Color.Purple,Color.Red,
Color.RosyBrown,Color.RoyalBlue,Color.SaddleBrown,Color.Salmon,
Color.SandyBrown,Color.SeaGreen,Color.SeaShell,Color.Sienna,Color.Silver,
Color.SkyBlue,Color.SlateBlue,Color.SlateGray,Color.Snow,
Color.SpringGreen,Color.SteelBlue,Color.Tan,Color.Teal,
Color.Thistle,Color.Tomato,Color.Transparent,Color.Turquoise,
Color.Violet,Color.Wheat,Color.White,Color.WhiteSmoke,Color.Yellow,
Color.YellowGreen
};
private Panel panel1 = new Panel();
private Label[] col = new Label[140];
public CreateMyPanel()
{
// Initialize the Panel control.
panel1.Location = new Point(ClientRectangle.Left + 5,ClientRectangle.Top + 5);
panel1.Size = new Size(ClientRectangle.Right-5, ClientRectangle.Bottom-5);
panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Controls.Add(panel1); // Add the Panel control to (inside) the form.
// Initalize the Label controls.
int ystart = ClientRectangle.Top;
for(int j=0; j<140; j++)
col[j] = new Label();
for(int i = 0; i<140; i++)
{
col[i].Size = new Size(ClientRectangle.Right, 20);
col[i].Font = new System.Drawing.Font("Comic Sans MS",10,FontStyle.Bold);
col[i].ForeColor = Color.Black;
if(col[i].Equals(Color.Black) == true)
{
col[i].ForeColor = Color.White;
}
col[i].Text = color[i].ToString();
col[i].Location = new Point(ClientRectangle.Left,ystart);
col[i].BackColor = color[i];
col[i].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
panel1.Controls.Add(col[i]); // Add the Label controls to (inside) the Panel.
if((col[i].Location.Y > panel1.Location.Y))
{
panel1.AutoScroll = true;
}
ystart += 20;
}
this.Size = new Size(315, 300);
this.Text = "A Color Guide - JAYANT";
this.MaximizeBox = false;
// this.BorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
}
public static void Main()
{
Application.Run(new CreateMyPanel());
}
}
/* To Compile :----
csc /r:System.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll /r:Microssft.Win32.InterOp.dll /out:ColorGuide.exe Colorguide.cs
*/
Filled with the semi transparent and transparent color
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Color c1 = Color.FromArgb(100, Color.Blue);
Color c2 = Color.FromArgb(50, Color.Green);
g.FillEllipse(Brushes.Red, 20, 20, 80, 80);
g.FillRectangle(new SolidBrush(c1), 60, 80, 60, 60);
Point[] pa = new Point[] {
new Point(150, 40),
new Point(90, 40),
new Point(90, 120)};
g.FillPolygon(new SolidBrush(c2), pa);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw each of 100 cells with randomly chosen colors
using System;
using System.Drawing;
using System.Windows.Forms;
public class ColorChips : Form {
public ColorChips() {
Size = new Size(300,300);
Text = “Color Chips”;
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
int h = DisplayRectangle.Height;
int w = DisplayRectangle.Width;
Random r = new Random();
for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++) { Color color = Color.FromArgb (r.Next(256), r.Next(256), r.Next(256)); Brush brush = new SolidBrush(color); g.FillRectangle(brush, i*w/10, j*h/10, w/10, h/10); } } base.OnPaint(e); } static void Main() { Application.Run(new ColorChips()); } } [/csharp]