手动呼叫事件? [英] Call an event manually?

查看:102
本文介绍了手动呼叫事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我遇到了一个小问题:我的pictureBox1_Paint-event没有触发,因为我使用的是剥皮设备,显然会超过所有的油漆事件。



这不是一个大问题,只要我可以手动调用此事件。但是我该怎么做?

有可能吗?



事件使用参数object sender,PaintEventArgs e,我相信使事情有效的秘诀在于PaintEventArgs。有没有办法手工制作这些?



代码(示例):



Hello!

I'm having a little problem: My pictureBox1_Paint-event does not fire, because of my use of a skinning-device, that obviously overrules all the paint-events.

It is not that big a problem, if only I could call this event manually. But how do I do that?
Is it possible at all?

The event uses the parameters "object sender, PaintEventArgs e", and I believe the secret to make things work is in the PaintEventArgs. Is there any way to make these by hand?

The code (example):

public void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(Color.Red, 1))
    {
        e.Graphics.DrawRectangle(pen, 10, 10, 30, 20);
    }
}





提前致谢。



真诚/最好的问候

Mads Aggerholm



Thanks in advance.

Sincerely/Best regards
Mads Aggerholm

推荐答案

虽然您可以手动调用绘图事件,但可以为自己构建一个PaintEvenArgs实例并且调用该方法,这是一个糟糕的主意。

Although you could call a paint event manually, by constructing a PaintEvenArgs instance for yourself, and calling the method, it's a poor idea.
PaintEventArgs pea = new PaintEventArgs(MyGraphics, MyClipRect);
pictureBox1_Paint(pictureBox1, pea);



一个更好的解决方案是将代码从事件处理程序移到一个方法中,从Paint事件传递图形上下文,然后相反,调用它,提供适合绘画的Graphics对象(并在方法返回时记住Dispose它)



但是,如果你的皮肤机制没有通过Paint你控制下来的事件,然后你需要看看它并找出原因 - 因为它将禁用所有聪明的图形效果,并可能使许多,许多控件失败。你确定你在正确的控件上调用Invalidate来首先启动Paint吗?


A better solution would be to move the code out of the Event handler into a method, which you pass the Graphics Context from the Paint event, and then call that instead, supplying a Graphics object suitable for the painting (and remembering to Dispose it when the method returns)

However, if your skinning mechanism is not passing the Paint events on down you your controls, then you need to look at it and find out why - as it would disable all "clever" graphics effects and probably make many, many controls fail. Are you sure you are calling Invalidate on the correct controls to initiate the Paint in the first place?


你的皮肤组件是否允许你从皮肤化过程中排除某些控件?通常就是这种情况......



我会这样做,而不是试图手动操纵组件。



如果没有,那么你可以尝试询问皮肤组件的制造商是否有可能提供这样的选项。
Doesn't your skinning component allow for you to exclude certain controls from the skinning process? That is normally the case...

I would go that way instead of try to manipulate the components by hand.

If not, then you could try to ask the maker of the skinning component if it would be possible for them to provide such an option.


这样来自任何其他人的调用功能...



call like this from any other function...

public void Button1_Click(object sender, EventArgs e)
{
pictureBox1_Paint(null,null);

}

public void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(Color.Red, 1))
    {
 Graphics g = pictureBox1.CreateGraphics();
        g.DrawRectangle(pen, 10, 10, 30, 20);
    }
}


这篇关于手动呼叫事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆