Windows Develop Bookmark and Share   
 index > Windows Forms Designer > OnPaintEvent Issue(help please)
 

OnPaintEvent Issue(help please)

Ok,i'v just started messing around with the OnPaint Event i normally just make most of my Graphics in Adobe Photoshop.But want to start using the the OnPaint in my Forms..

The problem i'm coming across is moving my Straight LineDown the Form,As of right now it goes Horizontal but in the middle of my Form..here is the code i have so Far..

Now i'm not sure about the p3, p4 i know it can take up to 4 Arguments but when i use the p3, p4 i get an Compile Time Error in my g.DrawGraphics(MyPen, p1, p2);

Looking to move the DrawLine further down my Form Thxs in Advance for the Help

protected override void OnPaint(PaintEventArgs pe)

{

Graphics g = pe.Graphics;

Pen MyPen = new Pen(Color.CadetBlue);

Point p1 = new Point(8, 250);

Point p2 = new Point(250, 250);

Point p3 = new Point(10, 10);

Point p4 = new Point(10,10);

SolidBrush MyBrush = new SolidBrush(Color.Red);

g.FillRectangle(MyBrush, System.Drawing.RectangleF.Empty);

g.DrawRectangle(MyPen, 8, 35, 250, 220);

g.DrawLine(MyPen, p1, p2);

}

Rattlerr  Tuesday, July 18, 2006 1:03 AM
You may know all of this already, so please skip over it if it sounds familiar:

In Windows Forms programming, the coordinate system works like this. Let's saw you have a form that is 500 pixels wide by 300 pixels tall. The top left corner of the form's client area* will be (0,0), the origin. The bottom right corner is (500, 300).

* What's the client area? That's the part of the form that you can usually draw on...it doesn't include the borders, the titlebar, and min/max/close buttons.

That means that X increases from left to right, and Y increases from TOP to BOTTOM. This is different than you may used to.

In order to draw a line near the bottom of your form, the Y coordinate will tend to be larger. However, if it is too large, it will go off the edge of your form and won't be drawn. You can use the form's ClientRectangle.Height property to see how far down you can draw.

If you want to draw a line near the bottom of your form, try:

Point p5 = new Point(8, this.ClientRectangle.Height - 20);
Point p6 = new Point(this.ClientRectangle.Width - 8, this.ClientRectangle.Height - 20);
e.Graphics.DrawLine(Pens.CadetBlue, p5, p6);

This will draw a line that is always 8 pixels from the left and right edges, and 20 pixels above the bottom edge.

Some other notes:
- Remember to call Dispose() on your MyBrush and MyPen.
- Or, you can use Brushes.Red and Pens.CadetBlue, and you won't have to call Dispose().

Here's a trick that may help: Add this code to the Forms OnMouseMove event handler:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.Location.ToString();
}

This will show the mouse's current coordinates in the titlebar of the form, in client coordinates.



Hope this helps,

-- Tim Scott
http://geekswithblogs.net/tscott

>>> Please remember to mark answers <<<

Tim Scott  Tuesday, July 18, 2006 1:54 AM
You may know all of this already, so please skip over it if it sounds familiar:

In Windows Forms programming, the coordinate system works like this. Let's saw you have a form that is 500 pixels wide by 300 pixels tall. The top left corner of the form's client area* will be (0,0), the origin. The bottom right corner is (500, 300).

* What's the client area? That's the part of the form that you can usually draw on...it doesn't include the borders, the titlebar, and min/max/close buttons.

That means that X increases from left to right, and Y increases from TOP to BOTTOM. This is different than you may used to.

In order to draw a line near the bottom of your form, the Y coordinate will tend to be larger. However, if it is too large, it will go off the edge of your form and won't be drawn. You can use the form's ClientRectangle.Height property to see how far down you can draw.

If you want to draw a line near the bottom of your form, try:

Point p5 = new Point(8, this.ClientRectangle.Height - 20);
Point p6 = new Point(this.ClientRectangle.Width - 8, this.ClientRectangle.Height - 20);
e.Graphics.DrawLine(Pens.CadetBlue, p5, p6);

This will draw a line that is always 8 pixels from the left and right edges, and 20 pixels above the bottom edge.

Some other notes:
- Remember to call Dispose() on your MyBrush and MyPen.
- Or, you can use Brushes.Red and Pens.CadetBlue, and you won't have to call Dispose().

Here's a trick that may help: Add this code to the Forms OnMouseMove event handler:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.Location.ToString();
}

This will show the mouse's current coordinates in the titlebar of the form, in client coordinates.



Hope this helps,

-- Tim Scott
http://geekswithblogs.net/tscott

>>> Please remember to mark answers <<<

Tim Scott  Tuesday, July 18, 2006 1:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Using local HTML Page for help
• DataGridView column separator can't move beyond window limits.
• Strategy for keeping properties in User Control synchd
• Drag & Drop on a windows form?
• Closing multiple instances of a form
• how to open another windows form ??
• TextBox readOnly
• Developing a Keyboard component
• TreeView scrolling
• only validation on lose focus ....