Re: A One Pixel Long Line
- From: "Martijn Mulder" <i@m>
- Date: Tue, 18 Apr 2006 16:36:40 +0200
Can't you just draw a pixel?
No, I need it as a line to do hit testing on it.
Or do you know a way to
1. do a hit test on a pixel?
2. draw an individual pixel?
The individual pixel I want to 'hit' is not part of a line, as I initially
thought, but the sole inhabitant of a System.Drawing.Rectangle object, as in
the code below. See if you are man (or woman) enough to hit the pixel with
the mouse pointer :-)
//namespace HitPixel
namespace HitPixel
{
//class Form
class Form:System.Windows.Forms.Form
{
//the pixel that we want to hit, the 'hitpixel'
System.Drawing.Point point=new System.Drawing.Point(150,150);
//line 1, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point1=new System.Drawing.Point(150,50);
System.Drawing.Point point2=new System.Drawing.Point(150,140);
//line 2, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point3=new System.Drawing.Point(150,250);
System.Drawing.Point point4=new System.Drawing.Point(150,160);
//line 3, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point5=new System.Drawing.Point(50,150);
System.Drawing.Point point6=new System.Drawing.Point(140,150);
//line 4, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point7=new System.Drawing.Point(250,150);
System.Drawing.Point point8=new System.Drawing.Point(160,150);
//the graphicspath. Holds nothing more than the 'hitpixel'
System.Drawing.Drawing2D.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();
//a Black pen
System.Drawing.Pen blackpen=new
System.Drawing.Pen(System.Drawing.Color.Black,20);
//a Red pen
System.Drawing.Pen redpen=new
System.Drawing.Pen(System.Drawing.Color.Red,20);
//a pen
System.Drawing.Pen pen;
//constructor
Form()
{
//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWmPaint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);
//set EndCaps of both pens to ArrowAnchor
blackpen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;
redpen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;
//set pen
pen=blackpen;
//add Rectangle to graphicspath. Rectangle is 1x1 pixel
graphicspath.AddRectangle
(
new System.Drawing.Rectangle
(
point.X,
point.Y,
1,
1
)
);
//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove);
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}
//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{
//use Black pen
pen=blackpen;
if(graphicspath.IsOutlineVisible(b.X,b.Y,System.Drawing.Pens.Black))
{
//if pixel is hit, use Red pen
pen=redpen;
}
//force redraw
Invalidate();
}
//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(pen,point1,point2);
b.Graphics.DrawLine(pen,point3,point4);
b.Graphics.DrawLine(pen,point5,point6);
b.Graphics.DrawLine(pen,point7,point8);
b.Graphics.FillPath(System.Drawing.Brushes.Black,graphicspath);
}
//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}
.
- Follow-Ups:
- Re: A One Pixel Long Line
- From: Michael C
- Re: A One Pixel Long Line
- From: Michael C
- Re: A One Pixel Long Line
- References:
- A One Pixel Long Line
- From: Martijn Mulder
- Re: A One Pixel Long Line
- From: Michael C
- Re: A One Pixel Long Line
- From: Martijn Mulder
- A One Pixel Long Line
- Prev by Date: Which codec is required?
- Next by Date: Detecting when a datagrid control loses focus
- Previous by thread: Re: A One Pixel Long Line
- Next by thread: Re: A One Pixel Long Line
- Index(es):
Relevant Pages
|