This is a migrated thread and some comments may be shown as answers.

Move bar drawing on gridview

2 Answers 16 Views
This is a migrated thread and some comments may be shown as answers.
Lek
Top achievements
Rank 1
Lek asked on 19 Oct 2013, 02:39 AM
from
Demo Application - RadControls for WinForms.
Gridview -> Custom Painting.

I want to move the drawing image in grid view.
- change cursor on mouse over
- drag and drop drawing
- show image moving 

please help me...

2 Answers, 1 is accepted

Sort by
0
Accepted
George
Telerik team
answered on 23 Oct 2013, 04:05 PM
Hello Lek,

Thank you for contacting us.

There is a lot of information in internet about how to drag-drop a drawn shapes. Please, refer to some of the articles I found:
  1. http://stackoverflow.com/questions/2442105/drag-and-drop-rectangle-in-c-sharp
  2. http://stackoverflow.com/questions/5307938/drag-and-drop-function-with-drawing-rectangle-c-sharp-net-forms
  3. http://social.msdn.microsoft.com/forums/wpapps/en-us/26c21f6d-ef4f-4e66-817e-7145d8f57e66/adding-dragdropresizable-selection-rectangle-to-image-editor

You can also try the code below, which enables you to create rectangles on the form and drag-drop them. I used a plain Form for simplicity. The case with RadGridView will be no different.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
    }
 
    Rectangle rec = new Rectangle(0, 0, 0, 0);
    List<Figure> figures = new List<Figure>();
    bool moveFigure;
    Figure currentFigure;
    Figure tempFigure;
 
    protected override void OnPaint(PaintEventArgs e)
    {
        foreach (Figure figure in figures)
        {
            figure.Draw(e.Graphics);
        }
 
        if (this.tempFigure != null)
        {
            this.tempFigure.Draw(e.Graphics);
        }
    }
 
    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            rec = new Rectangle(e.X, e.Y, 0, 0);
            foreach (var figure in this.figures)
            {
                if (figure.Bounds.Contains(rec))
                {
                    currentFigure = figure;
                    moveFigure = true;
                    break;
                }
            }
             
            Invalidate();
        }
    }
 
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (this.moveFigure)
            {
                this.currentFigure.Bounds = new Rectangle(e.Location.X, e.Location.Y
                    , this.currentFigure.Bounds.Width, this.currentFigure.Bounds.Height);
            }
            else
            {
                rec.Width = e.X - rec.X;
                rec.Height = e.Y - rec.Y;
            }
 
            this.tempFigure = new Figure
            {
                Bounds = rec,
            };
             
            Invalidate();
        }
 
        if (this.moveFigure)
        {
            this.Cursor = Cursors.Hand;
        }
        else
        {
            this.Cursor = Cursors.Default;
        }
 
    }
 
    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
 
        this.moveFigure = false;
        this.currentFigure = null;
 
        figures.Add(this.tempFigure);
    }
}
 
public class Figure
{
    private Rectangle bounds;
    private Color fill = Color.Blue;
 
    public Rectangle Bounds
    {
        get { return this.bounds; }
        set { this.bounds = value; }
    }
 
    public Color Fill
    {
        get { return this.fill; }
        set { this.fill = value; }
    }
 
    public void Draw(Graphics g)
    {
        g.FillRectangle(new SolidBrush(this.Fill), this.bounds);
    }
 
    public void Clear(Graphics g, Color color)
    {
        g.Clear(color);
    }
}

I hope this helps.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Lek
Top achievements
Rank 1
answered on 26 Oct 2013, 04:24 PM
Thanks, George.

Thanks very much for your answer, I am very glad.
Your code is very simple, I like it.
I will try your suggestion.

Regards
Lek.
Asked by
Lek
Top achievements
Rank 1
Answers by
George
Telerik team
Lek
Top achievements
Rank 1
Share this question
or