.NET的WinForms自定义控件:如何让一个引用包含窗体 [英] .NET WinForms Custom Control: how to get a reference to the containing form

查看:165
本文介绍了.NET的WinForms自定义控件:如何让一个引用包含窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,创建一个自定义的WinForms控制/用户控制的时候,让在运行时控制置于表格的参考,这样才能访问的形式不同的属性?我宁愿不通过引用的形式进入控件的构造函数,如,由于种种原因,我需要保持一个默认的构造函数不带参数。

Is there a way, when creating a WinForms Custom Control/User Control, to get a reference at runtime to the form the control is placed on, so as to access various properties of that form? I would rather not pass a reference to the form into the constructor of the control, as, for various reasons, I need to keep a default constructor with no parameters.

一个例子:我有几个自定义控件封装的Win32 API调用,而一些调用需要窗口句柄作为参数传递。我想能够检索包含窗体的手柄,所以我可以把它传递给API函数。

One example: I have several Custom Controls that encapsulate Win32 API calls, and some of the calls require Window handles to be passed as parameters. I would like to be able to retrieve the handle of the containing form so I can pass it to the API function.

另外一个例子:我有一个自定义控制,提供面包式用户通知。我想在的位置相对于开放申报书的主要申请表格的地点,如集中在主窗口中,关闭的权利,等等的选项这是不可能的,很明显,不知道协调主应用程序的窗口。

Another example: I have a Custom Control that provides "toast"-style user notifications. I would like to have the option of opening the notification form in a location relative to the location of the main application form, such as centered on the main window, off to the right, etc. This is not possible, obviously, without knowing the coordinates of the main application's window.

I 的诉诸使用FindWindowEx() - 。在一些情况下类型的API调用,但感觉像组装机,也不会在所有情况下

I could resort to using FindWindowEx()-type API calls in some cases, but that feels like a kludge, and would not work in all cases.

有谁知道这是可能的吗?

Does anyone know if this is possible?

推荐答案

您可以使用 Control.TopLevelControl 属性。

这篇关于.NET的WinForms自定义控件:如何让一个引用包含窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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