gpt4 book ai didi

c# - WinForms 中的 Button 有一个 CLICK 事件。但是谁告诉 Button 对象它已被单击?

转载 作者:太空宇宙 更新时间:2023-11-03 19:15:01 25 4
gpt4 key购买 nike

我正在学习 C#。我正在浏览语言的事件和委托(delegate)部分。我正在开发一个用于自学的 WinForms 应用程序。我尝试深入了解按钮及其工作原理。我发现了以下内容:

1) There is a line public partial class Form1 : Form in my default Form1.cs file. This is a partial class.

2) I also have a Form1.Designer.cs class file that has a line partial
class Form1
. Now the files mentioned in 1) and 2) combine to form a full class.

3) The From1.Designer.cs file has a lot of statements that eventually create the button object. It also has a statement that is of particular interest to me:

this.btn_BaseBuildLocation.Click += new System.EventHandler(this.btn_BaseBuildLocation_Click);

This statement adds a custom function to the delegate Click. This delegate is declared in the Control class (System.Windows.Forms.dll) as follows:

public event EventHandler Click;

4) The EventHandler is a delegate defined in System.EventHandler.cs (mscorlib.dll).

5) The Button class inherits Control class and thus has access to the Click EventHandler.

6) The Button class has all the logic to handle the flow once it knows that someone has clicked it. I had a look at the Button class used in Mono for understanding the inner details. I do this for almost all classes that I want to learn.

7) All this is extremely beautiful. But I was troubled by the fact that I did not know how the Button object knows that it has been clicked.

8) I went through VC++ and how it handles the events. I found a lot about Message Loops, Event Queues etc...

问题:

1) Is the VC++ way of handling events the same as .NET's?

2) If so, is there a way to look into those details?

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

从技术上讲,按钮是一个窗口。它有一个窗口句柄。

这意味着 Dispatcher 会在适当的时候将键盘和鼠标事件路由到 Button。 Button 具有确定 MouseDown 和 MouseUp 事件何时构成有效单击的内部逻辑,然后引发 Click 事件。

关于c# - WinForms 中的 Button 有一个 CLICK 事件。但是谁告诉 Button 对象它已被单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17649233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com