Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


CSharp Programming

CSharp > Code Examples

Color Fill dialog

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
Color Fill dialog using System; using System.Drawing; using System.Windows.Forms; class ColorFillDialogBox: Form { GroupBox grpbox = new GroupBox(); CheckBox chkbox = new CheckBox(); public ColorFillDialogBox() { string[] astrColor = { "Black", "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White"}; grpbox.Parent = this; grpbox.Text = "Color"; grpbox.Location = new Point(9, 9); grpbox.Size = new Size(96, 12 * (astrColor.Length + 1)); for (int j = 0; j < astrColor.Length; j++) { RadioButton radiobtn = new RadioButton(); radiobtn.Parent = grpbox; radiobtn.Text = astrColor[j]; radiobtn.Location = new Point(8, 12 * (j + 1)); radiobtn.Size = new Size(80, 10); } chkbox.Parent = this; chkbox.Text = "Fill Ellipse"; chkbox.Location = new Point(8, grpbox.Bottom + 4); chkbox.Size = new Size(80, 10); Button btn = new Button(); btn.Parent = this; btn.Text = "OK"; btn.Location = new Point(8, chkbox.Bottom + 4); btn.Size = new Size(40, 16); btn.DialogResult = DialogResult.OK; AcceptButton = btn; btn = new Button(); btn.Parent = this; btn.Text = "Cancel"; btn.Location = new Point(64, chkbox.Bottom + 4); btn.Size = new Size(40, 16); btn.DialogResult = DialogResult.Cancel; CancelButton = btn; ClientSize = new Size(112, btn.Bottom + 8); AutoScaleBaseSize = new Size(4, 8); } public Color Color { get { for (int j = 0; j < grpbox.Controls.Count; j++) { RadioButton radiobtn = (RadioButton) grpbox.Controls[j]; if (radiobtn.Checked) return Color.FromName(radiobtn.Text); } return Color.Black; } set { for (int j = 0; j < grpbox.Controls.Count; j++) { RadioButton radiobtn = (RadioButton) grpbox.Controls[j]; if (value == Color.FromName(radiobtn.Text)) { radiobtn.Checked = true; break; } } } } public bool Fill { get { return chkbox.Checked; } set { chkbox.Checked = value; } } } class DrawOrFillEllipse: Form { Color colorEllipse = Color.Red; bool bFillEllipse = false; public static void Main() { Application.Run(new DrawOrFillEllipse()); } public DrawOrFillEllipse() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&Options"); Menu.MenuItems[0].MenuItems.Add("&Color...", new EventHandler(MenuColorOnClick)); } void MenuColorOnClick(object obj, EventArgs ea) { ColorFillDialogBox dlg = new ColorFillDialogBox(); dlg.Color = colorEllipse; dlg.Fill = bFillEllipse; if (dlg.ShowDialog() == DialogResult.OK) { colorEllipse = dlg.Color; bFillEllipse = dlg.Fill; Invalidate(); } } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Rectangle rect = new Rectangle(0, 0, 50, 50); if(bFillEllipse) grfx.FillEllipse(new SolidBrush(colorEllipse), rect); else grfx.DrawEllipse(new Pen(colorEllipse), rect); } }