My code complies but doesn't show the ellipse in C# - c#

So I am trying to make a red ellipse flash in the middle but for some reason my codes runs and complies but no ellipse is drawn
As you can see in the code part I have put the drawing part in the default constructor, I have also tried creating a private function that draws it and tried calling it in the default constructor but that didn't work either
using System;
using System.Drawing;
using System.Windows.Forms;
public class RedLuserinterface : Form
{
private Panel panelTop = new Panel();
private Panel panelMid = new Panel();
private Panel panelBot = new Panel();
private Label title = new Label();
private Button pauseBut = new Button();
private Button resumeBut = new Button();
private Button exitBut = new Button();
private Size minInterfaceSize = new Size(400, 600);
private Size maxInterfaceSize = new Size(400, 600);
public RedLuserinterface()
{ //Set the size of the user interface box.
MaximumSize = minInterfaceSize;
MinimumSize = maxInterfaceSize;
//Initialize text strings
Text = "Red Light Assignment";
title.Text = "Red Light Program";
pauseBut.Text = "Pause";
resumeBut.Text = "Resume";
exitBut.Text = "Exit";
//Set Sizes
Size = new Size(400, 600);
panelTop.Size = new Size(400, 30);
panelMid.Size = new Size(400, 160);
panelBot.Size = new Size(400, 50);
title.Size = new Size(120, 30);
pauseBut.Size = new Size(85, 30);
resumeBut.Size = new Size(85, 30);
exitBut.Size = new Size(85, 30);
//Set Locations
title.Location = new Point(140, 20);
panelTop.Location = new Point(0, 0);
panelMid.Location = new Point(0, 30);
panelBot.Location = new Point(0, 480);
pauseBut.Location = new Point(50, 500);
resumeBut.Location = new Point(40, 150);
exitBut.Location = new Point(250, 500);
//Add controls to the form
Controls.Add(title);
Controls.Add(panelTop);
Controls.Add(panelMid);
Controls.Add(panelBot);
Controls.Add(pauseBut);
Controls.Add(resumeBut);
Controls.Add(exitBut);
//Set Color
panelTop.BackColor = Color.Green;
panelMid.BackColor = Color.Blue;
panelBot.BackColor = Color.Yellow;
//Create solid brush and draw ellipse
SolidBrush redBrush = new SolidBrush(Color.Red);
Graphics circle = this.CreateGraphics();
circle.FillEllipse(redBrush, 0, 0, 200, 200);
//send some stuff to the back
panelTop.SendToBack();
panelMid.SendToBack();
panelBot.SendToBack();
pauseBut.Enabled = true;
resumeBut.Enabled = false;
exitBut.Click += new EventHandler(stoprun);
//dispose stuff
redBrush.Dispose();
circle.Dispose();
}
}

You need to paint your ellipse when the Form engine requires you to do it. The form engine will call the Paint event handler if you define one and will pass the Graphics object to use to paint the ellipse. So you should remove the lines in the form constructor and add the proper delegate for the Paint event, then in the Paint event draw the ellipse
public RedLuserinterface()
{
.....
// Remove these lines
// SolidBrush redBrush = new SolidBrush(Color.Red);
// Graphics circle = this.CreateGraphics();
// circle.FillEllipse(redBrush, 0, 0, 200, 200)
....
exitBut.Click += new EventHandler(stoprun);
this.Paint += onFormPaint;
// No more needed here
// redBrush.Dispose();
// circle.Dispose()
}
private void onFormPain(object sender, PaintEventArgs e)
{
SolidBrush redBrush = new SolidBrush(Color.Red);
e.Graphics.FillEllipse(redBrush, 50,250, 200, 200);
redBrush.Dispose();
}

Related

How do I dynamically layer objects ontop of eachother in Windows Forms?

I'm using a FlowLayoutPanel to dynamically display an array of PictureBoxes and Labels. They're going in the order: PictureBox-Label-PictureBox-Label and so on. I need those labels to appear above each picture so they'll match, basically layring them ontop of picturebox margins. I tried using Controls.SetChildIndex(temp, 2); but it seem to just swap the postion of the picturebox. I also tried using temp.BringToFront(); but then all the pictureboxes being displayed at the top of the panel and all the labels are below (I need each label to match each picturebox above them). Here's the code:
public void RunMeta()
{
Label mostPickedLabel = new Label();
mostPickedLabel.Text = "Most picked heroes";
flowLayoutPanel1.Controls.Add(mostPickedLabel);
mostPickedLabel.Margin = new Padding(15, 0, 1000, 0);
mostPickedLabel.Font = new Font("Lucida Sans Unicode", 15);
mostPickedLabel.ForeColor = Color.DarkCyan;
mostPickedLabel.Size = new Size(200, 30);
foreach (var mostPickedHero in FetchDataFromDota2Site.MostUsedHeroesAndImages)
{
PictureBox temp = new PictureBox();
temp.ImageLocation = mostPickedHero.ImageSource;
temp.SizeMode = PictureBoxSizeMode.StretchImage;
temp.Left = temp.Width * flowLayoutPanel1.Controls.Count;
temp.Margin = new Padding(15, 30, 15, 30);
flowLayoutPanel1.Controls.Add(temp);
flowLayoutPanel1.AutoScroll = true;
Label heroName = new Label();
heroName.Text = mostPickedHero.MostPickedHeroName;
heroName.Font = new Font("Lucida Sans Unicode", 8);
heroName.ForeColor = Color.White;
flowLayoutPanel1.Controls.Add(heroName);
}
}

Winforms: ComboBox height doesn't resize when resolution is changed

I have a basic combobox in a form. Compared to other controls(Button,label, etc) the height of the combobox doesn't change when the resolution is changed.
public partial class Form1 : Form
{
string result;
string fontInformation;
private bool scaleFactorKnown = false;
private SizeF scaleFactor;
public Form1()
{
SizeChanged += Form1_SizeChanged;
InitializeComponent();
label1.Location = new Point(12, 36);
label1.Size = new Size(100, 21);
label1.Scale(scaleFactor);
//
// textBox1
//
textBox1.Location = new Point(133, 33);
textBox1.Size = new Size(100, 21);
textBox1.Scale(scaleFactor);
//
// comboBox1
//
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
// button1
//
button1.Location = new Point(365, 32);
button1.Size = new Size(100, 21);
button1.Scale(scaleFactor);
//
// radioButton1
//
radioButton1.Location = new Point(480, 32);
radioButton1.Size = new Size(100, 21);
radioButton1.Scale(scaleFactor);
//
// checkBox1
//
checkBox1.Location = new Point(586, 33);
checkBox1.Size = new Size(100, 21);
checkBox1.Scale(scaleFactor);
//
// textBox2
//
textBox2.Location = new Point(26, 102);
textBox2.Size = new Size(660, 250);
textBox2.Scale(scaleFactor);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (!scaleFactorKnown)
{
scaleFactor = AutoScaleFactor;
scaleFactorKnown = true;
}
Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
(int)(comboBox1.Height * scaleFactor.Height)); //use for sizing
//set bounds
comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);
}
}
I have tried the method Scale() to scale all other controls, it works for other controls except for combobox. I also tried manually changing the bound but it didn't work. I also tried change the anchor and dock as well.
Expected result: Combobox height(At 150%)=42
Actual result: Combobox
height(At 150%)=28
Would appreciate any help on how to fix this issue.
You have to set the IntegralHeight property of the ComboBox to false:
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
comboBox1.IntegralHeight = false;

WinForms, picturebox's image overlapping custom painted drawings

I have a custom user control with a picture box on it. Then, draw a circle on the control in Paint event.
var size = TextRenderer.MeasureText(this.UnreadCount.ToString(), lblDisplayname.Font);
var rec = new Rectangle(0, 0, size.Width, size.Width);
var smallFont = new Font(lblDisplayname.Font.Name, lblDisplayname.Font.Size - 1);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
format.LineAlignment = StringAlignment.Center;
using (LinearGradientBrush b = new LinearGradientBrush(
rec,
Color.FromArgb(242, 37, 37),
Color.FromArgb(178, 30, 30),
45F))
{
e.Graphics.FillEllipse(b, rec);
e.Graphics.DrawString(this.UnreadCount.ToString(), smallFont, Brushes.White, rec, format);
}
Then, later in the implementation, I set the image of the control's picture box.
MyControl ctrl = new MyControl();
ctrl.picImage.Image = Image.FromFile(imagePath);
ctrl.Refresh();
The issue is that: the picture box's image overlap the drawn circle.
current issue
Requirement is that: The circle is needed to display fully overlapping the image. What might be causing my issue?
The following fuction will return a user control with your requirements ,Posting the code for you to understand the parent child relationship.
Either the graphics has to be drawn on top of the image container or on another transparent container that is on top of the image.
The below sample has 2 panels ,panel1 and panel2 where panel 2 is a child of panel.
Panel1 is the background image and panel 2 has the graphics.Hope it helps.
private UserControl create_MyControl( string filenamepath)
{
//Create User Control
UserControl MyControl = new UserControl();
//Mention the size of control
MyControl.Size = new Size(100, 100);
//Create a panel to hold the background image that you wanted in the picture box
Panel panel1 = new Panel();
//dock the panel1 to fill the control background
panel1.Dock = DockStyle.Fill;
MyControl.Controls.Add(panel1);
//Create another panel as overlay for panel1
Panel panel2 = new Panel();
//dock the panel2 to fill the panel1;
panel2.Dock = DockStyle.Fill;
//Add panel2 as child of panel1
panel1.Controls.Add(panel2);
//Set panel2 background as transparent
panel2.BackColor = Color.Transparent;
// To replicate the variables that you have!
Label lblDisplayname = new Label();
lblDisplayname.Font = new Font("Arial", 24, FontStyle.Regular);
lblDisplayname.Size = panel2.Size;
lblDisplayname.TextAlign = ContentAlignment.TopCenter;
lblDisplayname.Text = "25";
lblDisplayname.Dock = DockStyle.Fill;
panel2.Controls.Add(lblDisplayname);
panel1.BackgroundImage = Image.FromFile(filenamepath);
panel1.BackgroundImageLayout = ImageLayout.Stretch;
//In Panel2 paint event put your code for stuff
panel2.Paint += (s, e) =>
{
var size = TextRenderer.MeasureText("Hello", lblDisplayname.Font);
var rec = new Rectangle(0, 0, size.Width, size.Width);
var smallFont = new Font(lblDisplayname.Font.Name, lblDisplayname.Font.Size - 1);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
format.LineAlignment = StringAlignment.Center;
using (System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(
rec,
Color.FromArgb(242, 37, 37),
Color.FromArgb(178, 30, 30),
45F))
{
e.Graphics.FillEllipse(b, rec);
e.Graphics.DrawString("Hello", smallFont, Brushes.White, rec, format);
}
};
return MyControl;
}

DrawString custom control text is not displayed in winforms C#

I have created a custom control and bind it to Form. I have draw graphics text in the control and added to Form. But it was not displaying the Form. This is my code.
//Create a custom control
public class DrawTextImage : Control
{
public void DrawBox(PaintEventArgs e, Size size)
{
e.Graphics.Clear(Color.White);
int a = 0;
SolidBrush textColor = new SolidBrush(Color.Black);
using (SolidBrush brush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(brush, new Rectangle(a, a, size.Width, size.Height));
e.Graphics.DrawString("Text", Font, textColor, new PointF(50, 50));
}
}
}
//Load Form1
public Form1()
{
InitializeComponent();
DrawTextImage call = new DrawTextImage();
call.Text = "TextControl";
call.Name = "TextContrl";
Size siz = new Size(200, 100);
call.Location = new Point(0, 0);
call.Visible = true;
call.Size = siz;
call.DrawBox(new PaintEventArgs(call.CreateGraphics(), call.ClientRectangle), siz);
this.Controls.Add(call);
}
Any help on this, what did I do wrong?
You should be using the control's own Paint event, not a custom method that you have to call manually.
public class DrawTextImage : Control
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
int a = 0;
SolidBrush textColor = new SolidBrush(Color.Black);
using (SolidBrush brush = new SolidBrush(Color.Red))
{
//Note: here you might want to replace the Size parameter with e.Bounds
e.Graphics.FillRectangle(brush, new Rectangle(a, a, Size.Width, Size.Height));
e.Graphics.DrawString("Text", Font, textColor, new PointF(50, 50));
}
}
}
Remove the call to DrawBox, it's unnecessary.
The Paint event is fired automatically whenever a redraw of the control surface is required. You can ask for this yourself in code by using the control's Invalidate() or Refresh() methods.

How do I draw polygon?( in C# WPF project)

Click the points, I want to make from the polygon area on image.
myPolygon = new Polygon();
myPolygon.Stroke = Brushes.Black;
myPolygon.Fill = Brushes.LightYellow;
myPolygon.StrokeThickness = 2;
myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
myPolygon.VerticalAlignment = VerticalAlignment.Center;
myPolygon.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Polygon_MouseDown);
myPolygon.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Polygon_MouseUp);
private void Polygon_MouseDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(image);
myPolygon.Points = new PointCollection() { new Point(p.X,p.Y) };
RootCanvas.Children.Add(myPolygon);
} //MouseClick Event BUT, did not click behavior.. I want draw a line along the points.
How can I do...?
We can draw Polygon using WPF canvas which is a collection of children objects.
Polygon p = new Polygon();
p.Stroke = Brushes.Black;
p.Fill = Brushes.LightBlue;
p.StrokeThickness = 1;
p.HorizontalAlignment = HorizontalAlignment.Left;
p.VerticalAlignment = VerticalAlignment.Center;
p.Points = new PointCollection() { new Point(10, 10), new Point(100, 100), new Point(200, 200) };
freeCanvas.Children.Add(p);
For more information,. please refer the following urls
http://www.codeproject.com/Articles/128705/WPF-rounded-corners-polygon
http://classicalprogrammer.wikidot.com/draw-dynamic-polygons-in-wpf
http://msdn.microsoft.com/en-us/library/ms747393.aspx

Categories