[已解决]设置相对于控件的窗口位置 [英] [SOLVED] Set Window Position Relative to Control

查看:91
本文介绍了[已解决]设置相对于控件的窗口位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写WinForms应用,我想在其中显示/隐藏相对于父窗体的控件位置的特定位置的无模式窗口.无法在框架中找到任何可以通过Google进行此操作的内容,我开始四处摸索,然后想出了以下代码(效果很好):

I''m writing a WinForms app where I want to show/hide a modeless window at a specific location relative to a control''s position of the parent form. Not being able to find anything in the framework that let me do this via google, I started muddling around, and I came up with the following code (which works just fine):

// label1 is the control under which I want to position the modeless form 

// Determine the location of the client rectange in relation to the 
// parent window's location (the clientRectangle.Location property 
// is always (0,0), so we have to calculate this ourselves).
int   clientX         = (int)((this.Size.Width - this.ClientSize.Width) / 2);
int   clientY         = this.Size.Height - clientX - this.ClientSize.Height;
// create the modelsess window and set its location
Form  modelessForm    = new Form();
modelessForm.Location = new Point(this.Location.X + clientX + this.label1.Location.X, 
				  this.Location.Y + clientY + this.label1.Location.Y);



我的问题是-框架中是否有类/方法可以执行我想做的事情?

旁注:我也考虑过调用API方法DPtoLPLPtoDP,但这需要更多代码,即使那样,我也无法使它起作用.



My question is this - Is there a class/method in the framework that does what I''m trying to do?

Side Note: I also considered invoking the API methods DPtoLP and LPtoDP, but that required a lot more code, and even then, I couldn''t get it to work.

推荐答案

您可以使用方法PointToClientPointToScreen来执行此操作.

http://msdn.microsoft.com/en-us/library/ms229598.aspx [ ^ ]

祝你好运!
You can use the methods PointToClient and PointToScreen to do this.

http://msdn.microsoft.com/en-us/library/ms229598.aspx[^]

Good luck!


我不认为有这样的API,因为它完全是多余的,会弄乱有用的方法并促进不良的设计.是的,将窗口放置在相对于任何控件的特定位置是非常糟糕的设计.如果您对此类事情有正当理由或动机,则应与他人分享,否则,谁有兴趣提供帮助?

您简单地在计算中犯了一个错误.例如,为什么ClientY是通过ClientX计算的?应该以相同的方式完成.看起来您假设从客户端的左上方到顶部的非客户端区域大小是相同的,但这是不正确的.另外,您假设label1的直接父对象是它的形式,但是为什么呢?如果要添加带有填充等的父面板怎么办? (顺便说一句,如果它是一种表单,那么您将使用糟糕的可支持性差的布局技术.)它会固定您的位置吗?

总体来说,这看起来像是一种不良样式+基本计算错误.
I don''t think there is such API, because it is completely redundant would mess-up useful methods and promote bad design. Yes, putting a windows in a specific location relative to any control is pretty bad design. If you have a justification or motivation for such thing, you should share it, otherwise, who would be interested to help?

You simple made a mistake in calculation. Why, for example, ClientY is calculated through ClientX? It should be done in the same way. It looks like you assume the non-client area size from left and from top of the client is the same, but this is not true. Also, you assume that the immediate parent of label1 is its form, but why? What if you want to add some parent panels with padding, etc.? (By the way, if it''s a form, you use bad poorly supportable layout techniques.) Will it fix your position?

Overall, looks like a bad style + elementary calculation errors.


John,类似的方法可以工作:

John, something like will work:

private void button1_Click(object sender, EventArgs e)
{
  var dialog = new About();
  dialog.Location = this.PointToScreen(button1.Location);
  dialog.StartPosition = FormStartPosition.Manual;
  dialog.Show();
}



那里重要的代码行是将StartPosition 设置为Manual.



The important line of code there is setting StartPosition to Manual.


这篇关于[已解决]设置相对于控件的窗口位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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