Click here to Skip to main content
15,880,543 members
Articles / Multimedia / GDI
Tip/Trick

Clickable areas in a picturebox

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
16 Mar 2011CPOL1 min read 29K   11   4
Like HTML images, we can have clickable areas in a picturebox of windows application
In HTML pages, many of us saw click-able HTML transparent map areas which show some tip or have a hyperlink. We can achieve the same thing in Windows application where we can handle the click and do whatever necessary.

How this can be done? Let us take the simple Panel control which has device context and fire click event. The simplicity of this control is useful. In html map complex area are defined by set of coordinate pairs. Here we can have that by using the GrpahicsPath from System.Drawing.Drawing2D. Then we can set the Region of the panel to this graphics path. Make the background color of the panel to Transparent. Now the panel is ready and we can add this to the controls collection of the picture box, position it using the location property of the panel.

Here is the code. I use a custom panel class to draw the panel in the picture box.

C#
class CirclePanel:Panel
{
    //Properties to draw circle
    float radius;
    public float Radius
    {
        get { return radius; }
        set
        {
            radius=value;
            this.Size = new Size((int)Radius, (int)Radius);
        }
    }

    Point centre;
    public Point Centre
    {
        get { return centre; }

        set
        {
            centre=value;
            this.Location = Centre;
        }
    }

    public string Message { get; set; }
    public CirclePanel()
    {
        //Default Values
        this.BackColor = Color.Transparent;
        Radius = 1;
        Centre = new Point(0, 0);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        //Defines a graphic path and set it as the panel's region
        //For custom region use different path's
        if (centre != null)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, radius, radius);
            this.Region = new Region(path);
            path.Dispose();
        }
    }
}


Now how to use this class:

C#
private void Form2_Load(object sender, EventArgs e)
 {
     //Create a circle panel and setting the properties
     CirclePanel panel2 = new CirclePanel();

     //The 30,30 are X and Y in the picture box coordinates
     panel2.Centre = new Point(100, 100);
     panel2.Radius = 50;
     panel2.Message = "you clicked Me";

     // Add it to picture box at the coordinates defined
     pictureBox1.Controls.Add(panel2);
     panel2.Click += new EventHandler(panel2_Click);
 }

 protected void panel2_Click(object sender, EventArgs e)
 {
     //Display Message
     CirclePanel panel = (CirclePanel)sender;
     MessageBox.Show(panel.Message);
 }


This example will plot a transparent click-able area at 100x, 100y point with radius of 50 pixels. The click event can be handled. This technique may be useful for fancy UI, Interactive display like maps, etc.

License

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


Written By
Software Developer
India India
I am developer in .Net and GIS. albin_gis@yahoo.com

Comments and Discussions

 
GeneralCool Pin
GenJerDan16-Mar-11 4:25
GenJerDan16-Mar-11 4:25 
GeneralRe: Cool Pin
Albin Abel16-Mar-11 8:00
Albin Abel16-Mar-11 8:00 
Hi GenjerDan,

Thanks for your comment. Tool tip is possible here. Can use a tool tip control and assign the tooltip for the this extended control. If the tooltip need to be embedded inside the class then we can go like this.

Let us have these private members in the extended class

private Lazy<tooltip> tip;
private string message;

In the constructor instantiate this tooltip

tip = new Lazy<tooltip>(() => new ToolTip());

By setting it lazy the tooltip not instantiate unless a message is assigned.

When setting the message we will set the tool tip

public string Message
{
get { return message; }

set
{
message = value;
tip.Value.SetToolTip(this,value);
}

}

Now you will be having inbuilt tool tip on mouse over apart from the click handling.
GeneralRe: Cool Pin
GenJerDan16-Mar-11 8:36
GenJerDan16-Mar-11 8:36 
GeneralRe: Cool Pin
Albin Abel16-Mar-11 9:57
Albin Abel16-Mar-11 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.