简体   繁体   中英

How to add a mouse click event handler to Bitmap image constructor?

I'm currently studying C# 3.0 design patterns using the book of Judith Bishop. The author explains the use of Decorator pattern giving as example the code which allows to display JPG image and to draw additional decorations (tags, borders, etc). The interesting point of the code is that the image is drawn without using WindowsForm Designer; overall solution seems very unfamiliar to me. One of the exercises proposes to add a mouse click event handler to the image constructor, which will cause a tag to appear. I have already found a lot of analogous questions on this and other sites. However, all these questions give solutions which fits only the case when WindowsForm Designer is used. Also, I have added several lines to image constructor and have written a method which is supposed to handle the mouse click; however, there is no response from the program.

Can anybody propose a solution to this problem?

// DECORATOR PATTERN
// The original Photo class - Component class
public class Photo : Form
{
    Image image;
    public Photo()
    {
        image = new Bitmap(@"D:\jug.jpg");
        this.Text = "Lemonade";
        this.Paint += new PaintEventHandler(Drawer);
    }

    public virtual void Drawer(Object source, PaintEventArgs e)
    {
        e.Graphics.DrawImage(image, 30, 20);
    }
}

}

class DecoratorPatternExample {

// This simple BorderedPhoto decorator adds a colored BorderedPhoto of fixed size (IF MOUSE WAS CLICKED)
class BorderedPhoto : Photo
{
    Photo photo;
    Color color;
    bool mouse_click = false; // state of the mouse button

    public BorderedPhoto(Photo p, Color c)
    {
        photo = p;
        color = c;
        this.MouseClick += new MouseEventHandler(mouse); // handler for the mouse clicking
    }

    public void mouse(object sender, MouseEventArgs e) // a method which will handle the mouse clicking
    {
        if (e.Button == MouseButtons.Left)
        {
            mouse_click = true;
        }
    }

    public override void Drawer(Object source, PaintEventArgs e)
    {
        photo.Drawer(source, e);
        if (mouse_click == true) // if mouse was clicked, then draw the border
            e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
    }
}

If you are interested, the whole code is available here .

"However, all these questions give solution which fits only the case when WindowsForm Designer is used."

The Windows Forms Designer works on another part of the Partial Class. It can do nothing you can not do as well. Everything it does, it does in C# code same as you. And everything it did is executed the moment "InitializeComponent()" is called in the constructor.

So there is no actuall difference between what the Forms Designer does and what you do.

Thanks to @LarsTech, this problem was resolved!

Here is working code:

class BorderedPhoto : Photo
{
    Photo photo;
    Color color;
    bool mouse_click = false;

    public BorderedPhoto(Photo p, Color c)
    {
        photo = p;
        color = c;
        this.MouseClick += new MouseEventHandler(mouse);
    }

    public void mouse(object sender, MouseEventArgs e) // a method which will handle the mouse clicking
    {
        if (e.Button == MouseButtons.Left)
        {
            mouse_click = true;
            Invalidate(); // ADD INVALIDATE HERE
        }
    }

    public override void Drawer(Object source, PaintEventArgs e)
    {
        photo.Drawer(source, e);
        if (mouse_click == true) // if mouse was clicked, then draw the border
            e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
    }
}

Guys, thank you all for taking time to resolve my problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM