?
Solved

Click inside graphic square WinForms GDI

Posted on 2007-08-05
8
Medium Priority
?
483 Views
Last Modified: 2013-12-26
I'm working with WINFORMS and 2005 C#;
How do I know when a click happens inside a graphics square?

     Graphics gfx = this.panel1.CreateGraphics();
     SolidBrush sb = new SolidBrush(Color.Yellow);
     gfx.FillRectangle(sb, 120, 60, 40, 40);
     Pen bPen = new Pen(Color.Black)
     gfx.DrawRectangle(bPen, 120, 60, 40, 40);

Above code draws a black box filled with yellow. Thats good! How do I know when a user clicks INSIDE the yellow box? What technique can I use to capture the mouse click?
0
Comment
Question by:volking
8 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 19636510
easiest way keep a panel exactly there and trap the click

or

hook the mouse down of the form and this will give u the x and y cordinates and see whether this is in that zone
0
 
LVL 11

Assisted Solution

by:udhayakumard
udhayakumard earned 400 total points
ID: 19637868
Hi,

Try This Sample and let me know if any doubts:

        private int xValue = 4;
        private int yValue = 4;
        private int width = 30;
        private int height = 30;
       
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(15, 15, 215), 1), xValue, yValue, width, height);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if((e.X >= xValue) && (e.X <= (xValue+width)) && (e.Y >= yValue) && (e.Y <= (yValue+height)))
            {  
                MessageBox.Show("Square Clicked");  
            }
        }
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19637900
Since you are using a rectangle, you can use:


Rectangle rect = new Rectangle(120, 60, 40, 40);

if (rect.Contains(new Point(e.X, e.Y)))
{
}

Bob

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1600 total points
ID: 19638671
A combination udhayakumard's and Bob's posts...with slight modification:

        Rectangle rect = new Rectangle(120, 60, 40, 40);

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Yellow, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (rect.Contains(new Point(e.X, e.Y)))
                {
                    MessageBox.Show("That tickles!");
                }
            }
        }
0
 
LVL 5

Author Comment

by:volking
ID: 19641574
So ..... I capture the mouse click ANYWHERE inside the panel, then see if the mouse X-Y coordinates are within the rectangle X-Y coordinates. That's what I was afraid y'all were going to suggest, because, actually I need to draw between 3 and 24 little boxes (depending on conditions) and remembring all those coordinates is gonna be a pain in the arse! I guess an array of little objects made up of X,Y,Height,Width which I can cycle through every click is the right way.

Oh, well. I had hope there was an easier way. Thanks people!
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 19642240
"I guess an array of little objects made up of X,Y,Height,Width which I can cycle through every click is the right way."

Right...you basically only have two choices:

(1) Store the location of all your rectangles in some kind of structure (Array, ArrayList, whatever) and cycle thru them all doing a "hit-test" against each one.

(2) Add actual controls to the panel, either a built-in one, a derived one, or one you build from Control or UserControl, so that they can raise their own click events that the form traps.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19646029
Here is an example, using a GraphicsPath of drawing rectangles, and then checking if there is a click in a rectangle:

        GraphicsPath _path = new GraphicsPath();

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            _path.AddRectangle(new Rectangle(10, 10, 50, 50));
            _path.AddRectangle(new Rectangle(100, 100, 50, 50));

            e.Graphics.DrawPath(Pens.Red, _path);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (_path.IsVisible(e.X, e.Y))
                MessageBox.Show("Click " + e.X + ", " + e.Y);
        }

Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19646504
That'll work but then you can't paint the rectangles with different coloring schemes and you can't tell which one was clicked on.  If you had to change the rectangles or remove one of them it would be a pain too...

Guess it just depends on what you're doing I suppose.  ;)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question