简体   繁体   English

如何将鼠标单击事件处理程序添加到位图图像构造函数?

[英]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.我目前正在使用 Judith Bishop 的书学习 C# 3.0 设计模式。 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).作者解释了装饰模式的使用,以允许显示 JPG 图像和绘制额外装饰(标签、边框等)的代码为例。 The interesting point of the code is that the image is drawn without using WindowsForm Designer;代码的有趣之处在于,图像是在不使用 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.但是,所有这些问题都提供了仅适用于使用 WindowsForm Designer 时的情况的解决方案。 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." “然而,所有这些问题都给出了仅适用于使用 WindowsForm Designer 时的情况的解决方案。”

The Windows Forms Designer works on another part of the Partial Class. Windows 窗体设计器适用于 Partial 类的另一部分。 It can do nothing you can not do as well.它无能为力,你也做不到。 Everything it does, it does in C# code same as you.它所做的一切,都是用与您相同的 C# 代码完成的。 And everything it did is executed the moment "InitializeComponent()" is called in the constructor.它所做的一切都是在构造函数中调用“InitializeComponent()”的那一刻执行的。

So there is no actuall difference between what the Forms Designer does and what you do.因此,Forms Designer 所做的与您所做的并没有实际区别。

Thanks to @LarsTech, this problem was resolved!感谢@LarsTech,这个问题解决了!

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!伙计们,谢谢大家花时间解决我的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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