Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI WinForm
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 16-Dec-11 4:28am
Edited 16-Dec-11 4:45am
v4
Comments
SAKryukov at 16-Dec-11 10:53am
   
What is "rectangular point", etc.?
--SA
Tushar Sriv at 16-Dec-11 10:55am
   
well, I mean that I have created a rectangle of 5px x 5px at that point
Tushar Sriv at 16-Dec-11 10:58am
   
Thank you very much, but should I explain it again?
Tushar Sriv at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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):
 
//--------------------------------------------------------------------------------
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:
 
//--------------------------------------------------------------------------------
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 231
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100