[Tutorial]Drawing shapes around the mouse cursor in a GdViewer object

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5653
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

[Tutorial]Drawing shapes around the mouse cursor in a GdViewer object

Post by Loïc » Wed Jun 27, 2012 11:13 am

Prerequisites: GdPicture.NET 9 and higher.

C# example demonstration a good start to draw a cross at the mouse pointer position without refreshing the whole usercontrol surface between each drawing operation.


Code: Select all

        int prevX, prevY;
        const int CROSS_HEIGHT = 40;
        const int CROSS_WIDTH = 20;
        const float PEN_WIDTH = 1f;

        private void GdViewer1_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle rc = Rectangle.Union(new Rectangle((int)(prevX - CROSS_WIDTH / 2 - PEN_WIDTH),
                                                         (int)(prevY - CROSS_HEIGHT / 2 - PEN_WIDTH),
                                                         (int)((CROSS_WIDTH + PEN_WIDTH) * 2),
                                                         (int)((CROSS_HEIGHT + PEN_WIDTH) * 2)),
                                           new Rectangle((int)(e.X - CROSS_WIDTH / 2 - PEN_WIDTH),
                                                         (int)(e.Y - CROSS_HEIGHT / 2 - PEN_WIDTH),
                                                         (int)((CROSS_WIDTH + PEN_WIDTH) * 2),
                                                         (int)((CROSS_HEIGHT + PEN_WIDTH) * 2)));

            prevX = e.X; prevY = e.Y;
            GdViewer1.Invalidate(rc);
        }

        private void GdViewer1_Paint(object sender, PaintEventArgs e)
        {
            using (Pen pen = new Pen(Color.Red, PEN_WIDTH))
            {
                {
                    e.Graphics.DrawLine(pen, prevX - CROSS_WIDTH / 2, prevY, prevX + CROSS_WIDTH / 2, prevY);
                    e.Graphics.DrawLine(pen, prevX, prevY - CROSS_HEIGHT / 2, prevX, prevY + CROSS_HEIGHT / 2);
                }
            }
        }

Gabriela
Posts: 378
Joined: Wed Nov 22, 2017 9:52 am

Re: [Tutorial]Drawing shapes around the mouse cursor in a GdViewer object

Post by Gabriela » Mon Feb 04, 2019 4:16 pm

Hello,

This example can be useful when you want to implement "snap to grid" functionality for adding/editing annotations.
Kind regards,

Gabriela
GdPicture Team

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest