I'm new ,and I don't understand why this method when copied to windows forms doesn't do anything when running the program. I was copy it from MSDN page.
public void DrawLinesPoint(PaintEventArgs e)
{
// Create pen.
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
Point[] points =
{
new Point(10, 10),
new Point(10, 100),
new Point(200, 50),
new Point(250, 300)
};
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}
Typically, when you draw on a form, you handle the form’s Paint event and perform the drawing using the Graphics property of the PaintEventArgs
In your code you need to add the DrawLinesPoint to the paint event before being able to use it
In your Constructor() add
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawLinesPoint);
And in your Paint PaintEventHandler
private void DrawLinesPoint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
Point[] points =
{
new Point(10, 10),
new Point(10, 100),
new Point(200, 50),
new Point(250, 300)
};
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}
Related
I add a polygon to the GraphicsPath and try to check if the rectangle is positioned inside of it. When rectangle is completely outside the polygon code responds correctly. However, when inside the polygon its return value is unpredictable.
For now I only check top-left corner of the rectangle.
private void Form1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
rect = new Rectangle(new Point(x, y), new Size(30,30));
g.FillRectangle(Brushes.Green, rect);
Point[] points = new Point[]
{
new Point(0, 0),
new Point(50, 0),
new Point(50, 50),
new Point(0, 50),
};
graphicsPath.AddPolygon(points);
g.DrawPolygon(Pens.Black, points);
if ((graphicsPath.IsVisible(rect.Location)))
{
this.Text = "Inside " + rect.Location.ToString();
}
else
{
this.Text = "not inside " + rect.Location.ToString();
}
}
I also have some code which is used to move the rectangle using MouseDown, MouseUp and MouseMove events.
The results I receive are:
But after repainting the form (even just minimizing window) those same coordinates are responding differently and the result is:
i need to draw a line for example, in a dynamically created PictureBox. What happens is that the picture box is created and shown in the form but the line is missing. My code is below, any ideas?? thnx
public void create_pb()
{
PictureBox pb = new PictureBox();
pb.Size = new Size(200, 200);
pb.BorderStyle = BorderStyle.Fixed3D;
pb.Location = new Point(0,0);
panel1.Controls.Add(pb);
g = pb.CreateGraphics();
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 0, 0, 200, 200);
}
g is defined as public Graphics g;
Don't use CreateGraphics. You need to do your drawing in the Paint event handler instead, using e.Graphics from the event arguments passed to you.
Otherwise your line will simply be erased when the picture box is next repainted (e.g. when the form is moved, resized, covered by another form, etc).
Example:
pb.Paint += (sender, e) =>
{
Pen p = new Pen(Color.Black, 2);
e.Graphics.DrawLine(p, 0, 0, 200, 200);
};
Is there a way to create non-rectangular pictureBoxes. I have round shapes that should overlap and if possible should be in different pictureboxes..
I tried the this here, but you could not see both pictureboxes that are overlapping at a time, but just one..
Here the picture that resulted from my tests:
You can make the PictureBoxes circular by adding an Ellipse to a GraphicsPath and then building a new Region from it.
Here's a quick example:
public class Target : PictureBox
{
public Target()
{
this.Size = new Size(100, 100);
this.Paint += Target_Paint;
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(rc);
this.Region = new Region(gp);
}
void Target_Paint(object sender, PaintEventArgs e)
{
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
using (Pen pen = new Pen(Color.Blue, 5))
{
e.Graphics.DrawEllipse(pen, rc);
}
rc = new Rectangle(new Point(this.Size.Width / 2, this.Size.Height / 2), new Size(1, 1));
rc.Inflate(9, 9);
e.Graphics.FillEllipse(Brushes.Red, rc);
}
}
After compiling, the new control appears at the top of your ToolBox.
Here's a screenshot with three of them overlapping each other:
I have problem with draw two polygons.
I want to fill two triangles, but one is greater than the second.
I am using UserControl in winforms.
Code:
Point[] DOWN = new Point[] {new Point(0, 0), new Point(10, 0), new Point(5, 5)};
Point[] UP = new Point[] { new Point(0, 15), new Point(10, 15), new Point(5, 10) };
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19));
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
brush.Dispose();
}
Where is problem?
Try setting the PixelOffsetMode property:
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19))) {
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
}
Result:
Try keeping the order counter-clockwise and start from the highest point:
new Point(5, 10), new Point(10, 15), new Point(0, 15)
Tell us if that helped. Sometimes those algorithms don't behave well on border conditions.
I am rendering some text in GUI and trying to recoganize the URLs. Also I need to open the URLs in browser when click events are performed. I have attached the model code snippent below. My question is how to make these texts a valid URLs and how to make them to respond to click events. And one more thing this due to some requirements I cant change the structure of the below code snnipet.
namespace Model
{
public partial class ParentView : Form
{
public ParentView()
{
InitializeComponent();
}
private void ParentView_Paint(object sender, PaintEventArgs e)
{
GraphicsState state = e.Graphics.Save();
GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
renderer.RenderAsImage();
e.Graphics.Restore(state);
e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);
//I believe need to do something here
}
}
public class GraphicsRenderer
{
Graphics PageGraphics;
public GraphicsRenderer(Graphics g)
{
PageGraphics = g;
}
public void RenderAsImage()
{
Point currentLocation = new Point(0, 0);
PageGraphics.TranslateTransform(20, 40);
PageGraphics.DrawString("www.google.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
PageGraphics.TranslateTransform(20, -40);
PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
currentLocation = new Point(50, 60);
PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
}
}
}
Thanks,
Mkn
If you're just writing text to the screen as graphics you can't make it clickable. You'll need to position a LinkLabel or other clickable control at the right position to trigger the url opening in a browser.
Thanks for your response. I have analyzed further and found a way. Drawing rectangles over the text and finding the rectangles in mouse click events using Rectangle. Contains(MouseLeftClikLocation).
Now I am having another problem. If scale transformation is performed I could not able to get the location after restoring the graphics.
Please find the code snnipet below.
private void ParentView_Paint(object sender, PaintEventArgs e)
{
GraphicsState state = e.Graphics.Save();
GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
renderer.RenderAsImage();
e.Graphics.Restore(state);
e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);
//I am changing here
float[] transformPoints = renderer.transformPoints.Elements;
e.Graphics.TranslateTransform(transformPoints[4], transformPoints[5]);
Point currentLocation = renderer.currentLocation;
e.Graphics.DrawRectangle(new Pen(Brushes.Black),new Rectangle(currentLocation.X,currentLocation.Y,190,30));
}
public class GraphicsRenderer
{
Graphics PageGraphics;
internal Matrix transformPoints = new Matrix();
internal Point currentLocation = new Point(0, 0);
public GraphicsRenderer(Graphics g)
{
PageGraphics = g;
}
public void RenderAsImage()
{
currentLocation = new Point(50, 60);
//PageGraphics.ScaleTransform(3, 3);
PageGraphics.DrawString("www.bing.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
transformPoints = PageGraphics.Transform;
}
}