Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Draw a 1x1 rectangle; oddly enough, there's no DrawPixel() function.

 

myGraphics.FillRectangle(Brushes.Red, x, y, 1, 1); // Draw a red point at (x, y)

  • *Experts*
Posted

You can create one, or you can use the one from paint event.

To create one do this:

Graphics gr = this.CreateGraphics(); //this refers to the form here

Now you will be able to draw to the form.

or use the one from Paint event:

e.Graphics.DrawRectangle(coordinates that you pick);

  • *Experts*
Posted
In the Paint event for whatever object you are painting:
Graphics g = e.Graphics; // e is the PaintEventArgs for the paint event, and the graphics object is passed into it

g.FillRectangle(Brushes.Red, x, y, 1, 1);

You should never do any drawing outside of the Paint event (not usually, anyway), as it will simply cause flickering and will require you to redraw more often, causing a potential performance hit.

Posted

Guys,

Thanks for the help. I can GET a point to draw now. However, it isn't EXACTLY where my cursor is set. It's almost opposite coordinates of where I click. Ever have this occur?

Posted

Here's my code...

 

Point p = Cursor.Position;

int x = p.X;

int y = p.Y;

textBox1.Text = "(" + x.ToString() + " , " + y.ToString()+")";

Graphics g = this.CreateGraphics();

g.FillRectangle(Brushes.Red, x,y, 1, 1);

  • *Experts*
Posted
You need to translate the cursor position to be relative to the form. Use the PointToClient() method of your form, and pass in the cursor coordinates to it. That will return the values that will corespond to the form.
Posted

Mutant,

Thanks for the help, as always. What's the scope on this PointToClient method?? Is it a method of my form?? (i.e. form.PointToClient(Point p)) I need some code snippet here, sorry.

  • *Experts*
Posted

It is a method of your form.

 

Point screenPoint, formPoint;

screenPoint = Cursor.Location;
formPoint = this.PointToClient(screenPoint);

// use formPoint for your position

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...