简体   繁体   English

C#事件/按下GUI时阻止

[英]C# Events / Block when hit GUI

I'm working on a custom GUI with SharpDX. 我正在使用SharpDX创建自定义GUI。

I have user Input from a Form Object and assign Action Methods to the specific events. 我从表单对象获得用户输入,并将操作方法​​分配给特定事件。 Below my UI I have a "drawing canvas" and I use Tool Objects that also listen to those Form Events. 在我的UI下方,我有一个“绘图画布”,并且我使用的工具对象还侦听那些表单事件。

But I'm a bit stuck on the matter of how to design my program to only pass those events to a second layer (in this case my canvas) when the first layer did not "hit" anything. 但是我在如何设计程序以仅在第一层没有“命中”任何东西的情况下仅将那些事件传递给第二层(在本例中为我的画布)的问题上遇到了麻烦。 In short: Only call "Tool.OnMouseDown" when "Button.OnMouseDown" did return false? 简而言之:仅当“ Button.OnMouseDown”返回false时才调用“ Tool.OnMouseDown”吗? Would a Chain Of Responsibility be the/a correct or possible approach? 责任链是/正确或可能的方法吗?

Or shall I make the current Tool check if "Excecute (Vector2)" is above some gui element but I think this would lead to the kind of coupling I want to prevent. 还是我应该让当前的工具检查“ Excecute(Vector2)”是否在某些gui元素之上,但是我认为这会导致我想防止这种耦合。

Hope someone is willing to help/hint (sorry for no code examples, if it's to confusingly descriped please tell me ;)) 希望有人愿意帮助/提示(很抱歉,没有代码示例,如果要描述的内容混乱,请告诉我;))

Thanks! 谢谢!

(Disclaimer: I know I don't have to reinvent the wheel, but I use it partly to learn and improve on my design patterns and coding skills) (免责声明:我知道我不必重新发明轮子,但我将其部分用于学习和改进我的设计模式和编码技巧)


thanks to sharp-ninja's answer i did the following: 多亏了Sharp-Ninja的回答,我做到了以下几点:

ok working with it like this now :) thanks again Mister Ninja 好的,现在就这样工作吧:)再次感谢忍者先生

using System.Windows.Forms;  

public class HandleMouseEventArgs : MouseEventArgs  
{  
    public bool handled { get; protected set; }  

    public HandleMouseEventArgs(MouseEventArgs args) : base(args.Button, args.Clicks, args.X, args.Y, args.Delta)  
    {  
        handled = false;  
    }  

    public void SetHandled()  
    {  
        handled = true;  
    }  
}  

Fortunately in .Net events get called in the order in which they are registered. 幸运的是,在.Net事件中,事件是以它们注册的顺序被调用的。 You can use a handlable event arg so that the first handler of the event can tell subsequent event handlers whether the event was handled. 您可以使用可处理的事件arg,以便事件的第一个处理程序可以告诉后续事件处理程序是否处理了该事件。

 event EventHandler<MyHandlableEventArg> MultiLevelEvent;

Then in your main program: 然后在您的主程序中:

 // First event handler
 MultiLevelEvent += (s, e) => { if(x) e.Handled = true; };

 // Subsequent event handler
 MultiLevelEvent += (s, e) => { if(!e.Handled) { /* Do Work */ } };

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

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