DrawString custom control text is not displayed in winforms C# - 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.

Related

My code complies but doesn't show the ellipse in 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();
}

Refresh does not refresh

I made an user control and I draw a rectangle directly in the window, like this (this is a simplified version):
private int rec_len = 200;
private void Draw_()
{
Pen pn = new Pen( Color.Black, WIDTH_LINE );
Graphics graph = this.CreateGraphics();
graph.Clear( Color.Transparent );
this.Refresh();
graph.DrawRectangle( pn, 20, 10, rec_len, 40 );
this.Refresh();
graph.Dispose();
}
public void button_Build_Click( object sender, EventArgs e )
{ rec_len += 10; Draw_(); }
The strange thing is that the second refresh actually poses a problem: if I comment it out, the rectangle is visible, if I let it in the code, the rectangle is not visible. In the real code I have to draw more than a rectangle and I want the refresh at the end, otherwise the background is visible between the moment I erase old drawing and the moment the new one is ready.
The surface of a control is not stored: When you paint on a control, the drawing is not saved and need to be redrawn each time the control is repainted (After a refresh for example). To create a persistante graphic, you can create a bitmap, draw on the bitmap and assign this bitmap to the BackgroundImage property.
Bitmap bmp = new Bitmap(WIDTH, HEIGHT);
void Initialize()
{
this.BackgroundImage = bmp;
}
private int rec_len = 200;
private void Draw_()
{
Pen pn = new Pen(Color.Black, WIDTH_LINE);
using (Graphics graph = Graphics.FromImage(bmp))
{
graph.Clear(Color.Transparent);
this.Refresh();
graph.DrawRectangle(pn, 20, 10, rec_len, 40);
this.Refresh();
}
}
public void button_Build_Click(object sender, EventArgs e) { rec_len += 10; Draw_(); }

Transparent control over drew shape

I'm new for create user control, and in my first usercontrol i used from picturebox and label ,
picturebox for draw a shape and label for show text over that shape.
i was set picturebox parent for label, and label backcolor to transparent also if don't have any text label set to visible = false
now i have a problem, when label is visible, i can't see picturebox correctly.
how can i solve this problem ?
also paint event on user control not work
private void Ucontrol_Paint(object sender, PaintEventArgs e)
{
if (RightToLeft)
{
lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
}
else
{
lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.No;
}
lblTxt.ForeColor = FontColor;
lblTxt.Text = Txt;
if (Question)
{
BorderColor = Color.DarkBlue;
BackColor = Color.FromArgb(75, 163, 234);
CreateQuestion(BorderColor, BackColor);
}
else
{
BorderColor = Color.DarkGreen;
BackColor = Color.FromArgb(59, 226, 75);
CreateAnswer(BorderColor, BackColor);
}
}
Forms controls don't have really a transpartent background, they copy it's parent content.
Also, a PictureBox can't be parent of another control as they aren't container.
Then, instead of using a picturebox just set the usercontrol background image and put the label on it, the transparency should work.
Here is a working example manually drawing the control content:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Label lbl = new Label();
lbl.Location = new Point(10, 10);
lbl.Width = 150;
lbl.Height = 150;
lbl.BackColor = Color.Transparent;
lbl.Text = #"asdfasdfasdfasdf\r\nasdfasdfasdf\r\n\r\nasdfasdfasdf";
lbl.Visible = true;
this.Controls.Add(lbl);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 100, 100));
e.Graphics.FillEllipse(Brushes.Yellow, new Rectangle(10, 10, 100, 100));
}
}

How to stop C# form from rendering the controls

I am using BufferedGraphics to redraw my form. I wish to redraw the form's controls all by myself, so I can draw lines wherever I want while I can still use the Form Designer to align my controls.
I've tried SuspendLayout but it make no use here.
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
BufferedGraphicsContext context;
BufferedGraphics grafx;
PictureBox pic1, pic2;
public Form1()
{
SetStyle((ControlStyles)(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint), true);
InitializeComponent();
//Add picturebox
pic1 = new PictureBox();
pic1.BackColor = Color.Black;
pic1.SetBounds(50, 50, 100, 50);
Controls.Add(pic1);
pic2 = new PictureBox();
pic2.BackColor = Color.Gray;
pic2.SetBounds(75, 50, 50, 100);
Controls.Add(pic2);
}
private void Form1_Load(object sender, EventArgs e)
{
context = BufferedGraphicsManager.Current;
context.MaximumBuffer = new Size(Width, Height);
grafx = context.Allocate(CreateGraphics(), new Rectangle(0, 0, Width, Height));
}
public override void Refresh() { }
protected override void OnPaintBackground(PaintEventArgs e) { }
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = grafx.Graphics;
g.Clear(BackColor);
Bitmap bmp = new Bitmap(Width, Height);
foreach (Control c in Controls)
c.DrawToBitmap(bmp, new Rectangle(c.Left, c.Top, c.Width, c.Height));
g.DrawImage(bmp, 0, 0);
g.DrawLine(new Pen(Color.Red, 4), 0, 0, ClientSize.Width, ClientSize.Height);
grafx.Render(Graphics.FromHwnd(Handle));
}
private void Form1_Resize(object sender, EventArgs e)
{
context.MaximumBuffer = new Size(this.Width, this.Height);
if (grafx != null)
{
grafx.Dispose();
grafx = null;
}
grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, Width, Height));
base.Refresh();
}
}
I need the line to be drawn at the top of the controls (picturebox), but the code turns out to be drawn below the controls.

calling a method with arguments in mouse click event

how can i call a method with system arguments in a mouse click event:
public void DrawStringRectangleF(PaintEventArgs e)
{
// Create string to draw.
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create rectangle for drawing.
float x = 150.0F;
float y = 150.0F;
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(x, y, width, height);
// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Black);
e.Graphics.DrawRectangle(blackPen, x, y, width, height);
// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
}
i want to call this method in this event:
private void button1_Click(object sender, EventArgs e)
{
// calling DrawStringRectangleF(PaintEventArgs e) here
}
it's easy to call a method if the parameters are variables just by mentioning the values when calling these methods, but how to call it in such a case?!
In order to satisfy the existing method signature you could encapsulate the Graphics instance that you wish to use to draw the rectangle within an instance of the PaintEventArgs class like this
using (var controlGraphics = CreateGraphics())
{
var paintEventArgs = new PaintEventArgs(controlGraphics, ClientRectangle);
DrawStringRectangleF(paintEventArgs);
}
The Control.CreateGraphics method will produce a Graphics instance that you can use to paint on the underlying control (or form).
Understand that everything painted with that Graphics object will be erased each time the control is invalidated so you must re-draw the graphic continuously.
You may consider adapting the method signature so that it accepts an instance of Graphics instead which should make for a cleaner solution.
Here's an example using the Paint() event and a List of a custom class to store the information about what should be drawn:
public partial class Form1 : Form
{
public class TextData
{
public string Text;
public string FontName;
public int FontSize;
public RectangleF TextRectF;
public Color TextColor;
}
private List<TextData> Texts = new List<TextData>();
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form1_Paint);
TextData td = new TextData();
td.Text = "Example";
td.FontName = "Courier";
td.FontSize = 24;
td.TextRectF = new RectangleF(50.0F, 50.0F, 175.0F, 75.0F);
td.TextColor = Color.Red;
Texts.Add(td);
}
void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (TextData td in Texts)
{
using (Pen drawPen = new Pen(td.TextColor))
{
e.Graphics.DrawRectangle(drawPen, Rectangle.Round(td.TextRectF));
}
using (Font drawFont = new Font(td.FontName, td.FontSize))
{
using (SolidBrush drawBrush = new SolidBrush(td.TextColor))
{
e.Graphics.DrawString(td.Text, drawFont, drawBrush, td.TextRectF);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
TextData td = new TextData();
td.Text = "Sample Text";
td.FontName = "Arial";
td.FontSize = 16;
td.TextRectF = new RectangleF(150.0F, 150.0F, 200.0F, 50.0F);
td.TextColor = Color.Black;
Texts.Add(td);
this.Refresh();
}
}

Categories