Draw rectangle on Panel C# Form - c#

Question: How do i draw a rectangle on a Panel, rather than a form. Here is what my code looks like:
/*
* based on a some flags i determine which shape i want to draw.
* All shapes are stored in a list. I loop through the list
* and call each shape specific draw method - as shown below:.
*
*/
namespace myDrawProgram
{
private void panelArea_Paint(object sender, PaintEventArgs e)
{
if (drawWithPaint == true)
{
Pen p = new Pen(Color.Blue);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (IsShapeRectangle == true)
{
e.Graphics.DrawRectangle(p, rect);
}
else if (IsShapeCircle == true)
{
e.Graphics.DrawEllipse(p, rect);
}
}
foreach (Shapes shape in listOfShapes)
{
shape.Draw(e.Graphics);
}
}
}
/*
* In another file i have my class which deals with
* drawing rectangles. It is as follows:
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SETPaint;
namespace myDrawProgram
{
class TheRectangles : Shapes
{
public Rectangle MyRect { set; get; }
public TheRectangles(Rectangle rect, Color colour, Color boarderColour, Int32 brushThickness)
: base(colour, boarderColour, brushThickness)
{
MyRect = rect;
}
public override void Draw(Graphics g)
{
base.Draw(g);
g.FillRectangle(new SolidBrush(Shapes.c), MyRect);
g.DrawRectangle(new Pen(bc, MyBrushThickness), MyRect);
}
}
}
i'm assuming i need to do something like this:
using (Graphics g = this.panel1.CreateGraphics()) {}
I'm just not sure how to implement this with regards to my code...

It sounds like you haven't hooked up the paint event of the panel:
panelArea.Paint += new PaintEventHandler(panelArea_Paint);
If panelArea is the name of your form, then just change it to your panel:
panel1.Paint += new PaintEventHandler(panel1_Paint);
and then move your painting logic to that method:
private void panel1_Paint(object sender, PaintEventArgs e) {
// the rest of your drawing
}

It looks like the parent (form?) is responsible for drawing each of its controls.
I would not do it that way.
If you just need a control that draws shapes (and doesn't necessarily need other behavior of a Panel), I would just create a User Control that has a property indicating what shape to draw and make it responsible for its own rendering.
If you do need the behavior of a panel, you can subclass Panel and implement the drawing behavior in your subclassed control. Again, that makes the control responsible for its own rendering.
For info on user drawn controls see
http://msdn.microsoft.com/en-us/library/b818z6z6(v=vs.71).aspx

Related

How to make a custom control be updated in designer?

I have created a custom control derived from Panel.
Everything works fine, except that in designer my control is only redrawn when it loses focus.
What am I missing?
Here is the code of CustomControl.cs
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Picwing
{
public partial class xPanel : Panel
{
private SizeF textSize;
public xPanel() {
InitializeComponent();
}
[Browsable(true)]
public override string Text {
get { return base.Text; }
set { base.Text = value; }
}
protected override void OnPaint(PaintEventArgs pe) {
base.OnPaint(pe);
textSize = pe.Graphics.MeasureString(Text, Font);
pe.Graphics.DrawRectangle(new Pen(ForeColor, 1), pe.ClipRectangle.X, pe.ClipRectangle.Y + textSize.Height / 2, pe.ClipRectangle.Width - 1, pe.ClipRectangle.Height - textSize.Height / 2 - 1);
pe.Graphics.FillRectangle(new SolidBrush(BackColor), 5, 0, textSize.Width, textSize.Height);
pe.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), pe.ClipRectangle.X + 6, pe.ClipRectangle.Y);
}
}
}
This is a print screen taken after moving label1 inside xPanel1, before it loses focus.
Your problem is because of using pe.ClipRectangle while painting on control.
Don't use pe.ClipRectangle. Use DisplayRectangle or ClientRactangle instead, based on your requirement.
When you draw in pe.ClipRectangle bounds, as you can see, the drawing will be done on the smallest invalidated part of your control.
Note:
You don't need to have InitializeComponent(); in constructor unless you use designer of your panel component to add some other components to it.
If you used DisplayRectangle, then in the FillRectangle method, instead of 0,5 use DisplayRectangle.X + 5, DisplayRectangle.Y.
If you are trying you draw a custom GroupBox, you can take a look at Custom GroupBox BackColor to Transparent

Trying to load layers to a control c#

im trying to load some rectangles as "layers" of the form, i loaded an image as also a layer of the form, but the problem with those rectangles is that they "overlay" the image, erasing portions of it, i want them to be seen as boxes of the image to display info, and i also want to be able to overlay a rectangle onto another rectangle without erasing eachother.
here is the class of the rectangle
namespace Imagen_capas
{
class rectangulotransp : UserControl
{
public Pen pen11;
private Rectangle Myrectangle;
public rectangulotransp(int x,int y,int alto, int ancho, Rectangle tamacontrol)
{
Size = tamacontrol.Size;
Location = tamacontrol.Location;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
Myrectangle = new Rectangle(x, y, alto, ancho);
pen11 = nuevopen();
}
private Pen nuevopen()
{
Pen mypen1 = new Pen(Color.Red);
return mypen1;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawRectangle(pen11,Myrectangle);
base.OnPaint(e);
}
}
}
And the code of the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Imagen_capas
{
public partial class Form1 : Form
{
rectangulotransp rect;
rectangulotransp rect2;
public Form1()
{
InitializeComponent();
rect = new rectangulotransp(50, 14, 500, 100,new Rectangle(0,0,Width+400,Height));
// this.Controls.Add(rect);
rect2 = new rectangulotransp(0, 50, 20, 100,new Rectangle(20,50,Width,Height));
this.Controls.Add(rect2);
this.Controls.Add(rect);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I think the problem is that when you set Transparent background, the rectangle is using the Form Backcolor and it seems to you that is erasing the other rectangle.
Maybe you could try painting your transparent rectangle as 4 lines instead of a rectangle.
Hope it helps you
my friends, i solved it in this way.
I created a class for the rectangle, and then another class for a control, in which i painted all the rectangles just like you told me, then the image, and then i added it as a layer of the form using the controls.add, in that way i was also able to put an image under it without being erased by the rectangles.
thanks!!

Children inherit parents appearance

I have created a simple custom panel using ContainerControl as my base. I've added custom properties to create borders and gradient backgrounds. If I override OnPaint and OnPaintBackground all child controls of the parent inherit the gradient and border styles. As a work around I have used the parents BackgroundImage property which works fine but has a few random quirks. There has to be a better way of approaching this issue but I have found no solution. Are there any Window API functions via Interop or other C# methods to fix this? If so please provide an example.
EDIT! Here is the style being copied (ugly example but makes the point):
EDIT 2! Here is a simple hard-coded ContainerControl without all the properties, designer attributes, etc.
public class Container : ContainerControl
{
protected override void OnPaintBackground( PaintEventArgs e )
{
using ( var brush = new LinearGradientBrush( e.ClipRectangle, Color.Red, Color.Blue, LinearGradientMode.Vertical ) )
{
e.Graphics.FillRectangle( brush, e.ClipRectangle );
}
}
}
If a Label control is created with its BackColor property set to Color.Transparent, it will end up calling its parent's OnPaintBackground() implementation.
If you modify Jon's example like this:
var label = new Label {
Text = "Label",
Location = new Point(20, 50),
BackColor = Color.Transparent
};
Then you will reproduce the issue.
There is an easy workaround, however. The problem comes from the way you're creating the linear gradient brush. Since you're passing e.ClipRectangle to its constructor, the shape of the gradient will vary depending on the control being rendered (container or label). On the other hand, if you pass the ClientRectangle of the container, then the gradient will always have the same shape and the result should be what you're looking for:
protected override void OnPaintBackground(PaintEventArgs e)
{
using (var brush = new LinearGradientBrush(ClientRectangle,
Color.Red, Color.Blue, LinearGradientMode.Vertical)) {
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
}
The result is:
Initialize the properties on control create/load
Then "INVALIDATE" the control to force a redraw of the control
I can't reproduce this simply on my Windows 7 machine - which suggests it may be one of the properties you've set in the designer. Short but complete program:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class GradientContainer : ContainerControl
{
protected override void OnPaintBackground(PaintEventArgs e)
{
using (var brush = new LinearGradientBrush(e.ClipRectangle,
Color.Red, Color.Blue, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
}
}
class Test
{
static void Main()
{
var label = new Label {
Text = "Label",
Location = new Point(20, 50)
};
var container = new GradientContainer {
Size = new Size(200, 200),
Location = new Point(0, 0),
Controls = { label }
};
Form form = new Form {
Controls = { container },
Size = new Size(300, 300)
};
Application.Run(form);
}
}
And the result:

Problem with CreateGraphics and drawing strings

The control below draws a string in a rectangle. On mouse move there is a hit test on the string rectangle, and the string is redrawn via CreateGraphics. The irritating problem is that the text is not drawn the same as in the Paint handler; it appears to be displaced by about 1 pixel, and the effect is like a bold font. How can I create a graphics object exactly like the one in the Paint handler so the text is drawn the same way? Ordinarily you would invalidate and redraw everything in the Paint event, but I have potentially hundreds of of other drawing items and only want to draw the string. Should I try to do any drawing outside of the Paint event or is this a mistake?
Example control:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test.TestModes
{
public partial class ExampleControl: UserControl
{
private const string testString = "0123456789";
private RectangleF stringRect = new RectangleF(10, 10, 100, 20);
public ExampleControl()
{
InitializeComponent();
}
private void ExampleControl_Paint(object sender, PaintEventArgs e)
{
Font font = new Font("Arial", 12, FontStyle.Regular);
e.Graphics.DrawString(testString, font, Brushes.Black, stringRect);
font.Dispose();
}
private void DrawString(bool hit)
{
Font font = new Font("Arial", 12, FontStyle.Regular);
using(Graphics g = CreateGraphics())
{
g.SetClip(ClientRectangle);
if(hit)
g.DrawString(testString, font, Brushes.Red, stringRect);
else
g.DrawString(testString, font, Brushes.Black, stringRect);
}
font.Dispose();
}
private void ExampleControl_MouseMove(object sender, MouseEventArgs e)
{
if(stringRect.Contains(e.Location))
DrawString(true);
else
DrawString(false);
}
private void button1_Click(object sender, EventArgs e)
{
Invalidate();
}
}
}
It is the CreateGraphics() call that is getting you in trouble, indirectly. The problem is anti-aliasing of the text. A normal painting cycle erases the background before drawing something on top. That doesn't happen in your case, your draw text on top of existing text. The side effect is that the pixels uses to create the aliasing get darker each time your draw. The end result is bold looking, and noticeably jagged text outlines.
The fix is easy: start with a clean slate before you draw:
using (Graphics g = CreateGraphics()) {
g.Clear(this.BackColor); <=== added
g.SetClip(ClientRectangle);
// etc..
}
You'll now also get to encounter a problem in drawing known as "flicker". It might not yet be that noticeable yet, but it will when you do more drawing. Flicker is suppressed with double-buffering. A feature supported by Windows Forms, but only if you use standard drawing techniques. In other words: no CreateGraphics().

Draw an ellipse without OnPaint method

I was looking about some GDI tutorial but everything I have found so far works with OnPaint method, which passes Paintarguments to Graphics. I have not found how to start from scratch, I mean how to use Graphics class itself?
This is the whole code I have treid that just doesnt work for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Pen pen= new Pen(Color.Red, 3);
Graphics g;
g = this.CreateGraphics();
g.DrawEllipse(pen, 150, 150, 100, 100);
}
}
}
It just doesnt do anything. I tried it in new form, nothing.
Thank you in advance!
The code is probably OK, it is drawing a ellipse as you are hoping for. However, after the Load event, there will be a PaintBackground event and a PaintEvent as the form is displayed. The PaintBackground will, by default, erase the contents of the control, effectively removing the ellipse you've just drawn.
Painting is a two stage process:
for each (region in set of regions that need updating)
{
PaintBackground (region)
Paint (region)
}
The window manager only redraws the parts of the control that require updating, if the contents of the control haven't changed or no user action has altered the control's visibility then no painting is done.
So, why do you want to draw the ellipse in the Load method? Usually, you only want to draw something when something needs to be drawn, and your form is told when something needs drawing in the PaintBackground and Paint events.
Are you worried about flickering? Or is it a speed issue? Ellipses are quick to draw. Flickering, however, is harder to fix. You need to create a bitmap, draw to the bitmap and blit the bitmap to the control during the Paint event. Also, make the PaintBackground event do nothing - no erasing the control, it's the erasing that causes the flicker.
EDIT: An example, I'm using DevStudio 2005 here.
Create a new C# winform application.
In Form1.cs add the following:
protected override void OnPaintBackground (PaintEventArgs e)
{
// do nothing! prevents flicker
}
protected override void OnPaint (PaintEventArgs e)
{
e.Graphics.FillRectangle (new SolidBrush (BackColor), e.ClipRectangle);
Point
mouse = PointToClient (MousePosition);
e.Graphics.DrawEllipse (new Pen (ForeColor), new Rectangle (mouse.X - 20, mouse.Y - 10, 40, 20));
}
protected override void OnMouseMove (MouseEventArgs e)
{
base.OnMouseMove (e);
Invalidate ();
}
Compile and run.

Categories

Resources