简体   繁体   English

使用Windows窗体作为控件是否不好?

[英]Is it bad to use a Windows Form as a Control?

I know it's possible to use a Form as a child control in another Form. 我知道可以将Form用作另一个Form中的子控件。

ChildForm child = new ChildForm();
child.TopLevel = false;
child.FormBorderStyle = FormBorderStyle.None;
child.Show();
containerControl.Controls.Add(child);

I realise this is not what a Form was designed for. 我意识到这不是Form设计的目的。 I was wondering what the implications here are, as opposed to using a UserControl ? 我想知道与使用UserControl相比,这里的含义是什么? Are there any real downsides? 有真正的不利之处吗? Any large, unnecessary overheads? 有没有不必要的大笔开销? Any unexpected behaviours? 有任何意外行为吗?

On the other hand, are there any advantages of using a form? 另一方面,使用表格有什么好处? Is there anything you can't do with a UserControl that you can do with a form, which may be useful if used as a child control? 用户控件有什么不能像窗体一样使用的,如果用作子控件,这可能会很有用?

I understand that both Form and UserControl inherit ContainerControl , but how different do their non-inherited members make them? 我知道FormUserControl都继承ContainerControl ,但是它们的非继承成员使它们有何不同?

My scenario 我的情况

The reason I'm asking is because I have an existing Form control, that I'd like to be displayed on its own sometimes (for development and as a little extra feature), but it doesn't need to be. 我问的原因是因为我有一个现有的Form控件,我有时显示在自己的(发展和一些额外的功能),但它并不需要的人。 However, I do need the Closing event of the form (which is not inherited from ContainerControl ) and I need to be able to cancel closing the form if a file has been edited and not saved. 但是,我确实需要表单的Closing事件(它不是从ContainerControl继承的),并且如果文件已被编辑但未保存,我需要能够取消关闭表单。

I've also noticed in my findings that a Form seems to have some transparency options that a UserControl doesn't seem to have, but I'm not sure if these will work when the form is used as a child control. 我在发现中还注意到, Form似乎具有一些UserControl似乎没有的透明度选项,但是我不确定当将窗体用作子控件时这些选项是否会起作用。

if you check the Inheritance Hierarchy in MSDN you find out that both UserControl and Form inherit from ContainerControl so in fact up to their base class nearly everything should be the same. 如果在MSDN中检查“继承层次结构”,则会发现UserControl和Form都从ContainerControl继承,因此实际上,直到它们的基类,几乎所有内容都应该相同。

said so, in general, I think you should avoid using a Form object hosted as a control unless you are dealing with legacy unchangeable code or unless you absolutely want this design so to be able, in certain other cases, to show the form all alone as top level non hosted anywhere. 如此说来,通常来说, 我认为您应该避免使用作为控件提供宿主的Form对象,除非您要处理遗留的不可更改的代码,或者除非您绝对希望此设计在某些其他情况下能够单独显示表单作为顶级非托管在任何地方。

If you simply need a container to put your other UI controls you can use a UserControl. 如果您只需要一个容器来放置其他UI控件,则可以使用UserControl。

in fact everything down to the Win32 level translates to handles and message pumps / WindowProc etc, even a simple TextBox will have an handle and will receive and consume messages taking care of its client and non client area and so on. 实际上,所有到Win32级别的东西都转换为句柄和消息泵/ WindowProc等,即使是简单的TextBox也将具有句柄,并且将在照顾其客户区和非客户区的情况下接收和使用消息,等等。

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

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