Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello Friends,
I am working on a project where in I need to create a visual dynamic point. It is required that when I click on the point it turns from a simple rectangular point to a filled rectangular point and when I drag it, it changes its position dynamically. I am required to do all this strictly in C sharp. I cannot use WPF. Currently I am using Windows.System.Drawing utility but I don't know how to move further. Please help me. Any support will be appreciated.
Thank You
Posted
Updated 16-Dec-11 4:45am
v4
Comments
Sergey Alexandrovich Kryukov 16-Dec-11 10:53am    
What is "rectangular point", etc.?
--SA
Er. Tushar Srivastava 16-Dec-11 10:55am    
well, I mean that I have created a rectangle of 5px x 5px at that point
Er. Tushar Srivastava 16-Dec-11 10:58am    
Thank you very much, but should I explain it again?
Er. Tushar Srivastava 16-Dec-11 11:05am    
It is certainly useful for me. Well actually I am not a programmer but I am an electronics engineer but for one of my interfacing design I am required to design this system. Well once again Thank you very much.

That's certainly possible. I'm doing something like that in an app I'm writing, and I'll try to formulate an answer you can actually use.

In my case, I created a custom control derived from the Panel control. I then overrode the Paint method, and put code in to draw an image in the specified location, and the MouseMove method to actually change the specified position. Here's most of the Mouse handler code as it exists in my code (keep in mind that you WILL have to massage it to fit your own needs, especially the painting part):

C#
//--------------------------------------------------------------------------------
protected override void OnMouseDown(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && m_weapon != null)
    {
        m_moving = true;
    }
    base.OnMouseDown(e);
}
 
//--------------------------------------------------------------------------------
protected override void OnMouseEnter(EventArgs e)
{
    if (m_weapon != null)
    {
        Cursor = Cursors.SizeAll;
    }
    base.OnMouseEnter(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseLeave(EventArgs e)
{
    Cursor = Cursors.Default;
    base.OnMouseLeave(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseUp(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        m_moving = false;
    }
    base.OnMouseUp(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseMove(MouseEventArgs e)
{
    if (m_moving)
    {
        m_origin.X = e.Location.X - m_offset.X;
        m_origin.Y = e.Location.Y - m_offset.Y;
        m_bmpRect.X = m_origin.X;
        m_bmpRect.Y = m_origin.Y;
        Refresh();
    }
    else
    {
        m_offset.X = e.Location.X - m_bmpRect.X;
        m_offset.Y = e.Location.Y - m_bmpRect.Y;
    }
    base.OnMouseMove(e);
}


The applicable paint hander code looks like this:

C#
//--------------------------------------------------------------------------------
protected override void OnPaint(PaintEventArgs e)
{
    Rectangle imgRect = new Rectangle(m_origin.X, m_origin.Y, m_weapon.Width, m_weapon.Height);
    e.Graphics.DrawImage(m_weapon, imgRect, 0, 0, m_weapon.Width, m_weapon.Height, GraphicsUnit.Pixel, imageAttributes);
    base.OnPaint(e);
}


Google is a great resource, btw.
 
Share this answer
 
The question is not clear, but don't worry: you just need to understand basic principles of making interactive graphics using System.Windows.Forms and System.Drawing.

I explained them several times. Please see my past solutions here:

What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

See also How do I clear a panel from old drawing[^].

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900