rustyfancy Posted August 7, 2003 Posted August 7, 2003 Anyone know how to draw a single point in C#.NET??? I'm not good with GDI yet. ---Matt Quote
*Experts* Volte Posted August 7, 2003 *Experts* Posted August 7, 2003 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) Quote
rustyfancy Posted August 7, 2003 Author Posted August 7, 2003 Hey Volte Face, How do I set up the Graphics object?? Quote
*Experts* mutant Posted August 7, 2003 *Experts* Posted August 7, 2003 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); Quote
*Experts* Volte Posted August 7, 2003 *Experts* Posted August 7, 2003 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. Quote
rustyfancy Posted August 7, 2003 Author Posted August 7, 2003 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? Quote
rustyfancy Posted August 7, 2003 Author Posted August 7, 2003 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); Quote
*Experts* mutant Posted August 7, 2003 *Experts* Posted August 7, 2003 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. Quote
rustyfancy Posted August 7, 2003 Author Posted August 7, 2003 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. Quote
*Experts* Volte Posted August 7, 2003 *Experts* Posted August 7, 2003 It is a method of your form. Point screenPoint, formPoint; screenPoint = Cursor.Location; formPoint = this.PointToClient(screenPoint); // use formPoint for your position Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.