如何确定控件是否在Form之外? [英] How to determine if control is outside of Form?

查看:22
本文介绍了如何确定控件是否在Form之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定动态添加的控件是否在表单之外.

一开始,我认为可以通过获取表单的高度和动态添加控件的位置来计算它.

但我注意到 Control.LocationForm.Height 没有什么"共同点.

我不认为我真的了解高度和位置之间的相关性.

例如:
我想如果你的表单的高度为 500,我把控件放在表单的底部,它应该给出 Location: X, 500(X 是宽度,这里不相关).但这不正确,它显示给我例如:X、465.我错过了什么吗?

所以我需要能够识别控件是否在表单之外,即使它只是一个像素.

我在 SO 上发现了几个类似的问题,但不幸的是,这并没有真正给我我需要的答案.

那么,有没有办法做到这一点?可以计算吗?

解决方案

表单的Height还包括标题栏和边框的高度.您可以使用表单的

I am trying to determine if a dynamically added control is outside of the form.

At first, I thought it might be possible to calculate it by getting the height of the form, and location of the dynamically added control.

But I noticed that the Control.Location and Form.Height have "nothing" in common.

I don't think I really understand what the correlation is between Height and Location.

For example:
I thought that if your form has a height of 500, and I put the control at the bottom of the form, it should give the Location: X, 500 (X is width, not relevant here). But this is not correct, it shows me for example: X, 465. Am I missing something?

So I need to be able to recognize if the control is outside of the form, even if it's just one pixel.

I've found several similar questions here on SO, yet this does not really give me the answer that I need, unfortunately.

So, is there any way to do this? Is it possible to calculate it?

解决方案

The Height of the form also includes the height of the title bar and borders. You can use the ClientSize of the form:

From the documentation on MSDN:

The size of the client area of the form is the size of the form excluding the borders and the title bar. The client area of a form is the area within a form where controls can be placed. You can use this property to get the proper dimensions when performing graphics operations or when sizing and positioning controls on the form. To get the size of the entire form, use the Size property or use the individual properties Height and Width.

The position of the control is relative to its container, so (0,0) is the left upper corner inside the form.

这篇关于如何确定控件是否在Form之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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