2014-01-15 36 views
0

这里的按钮是代码:为什么在偏差位置

Dim nBtn As New Button 
    PictureBox1.Controls.Add(nBtn) 
    nBtn.Text = "Click Me" 
    nBtn.Location = Cursor.Position ' or mouseposition = same result 

enter image description here

这就是它的外观..其约2英寸远离那些我本来点击它。我的代码甚至没有抵消它的位置,所以这是怎么发生的?
这是SplitContainer右侧的图片框。

+0

您可以创建一个简短的*完整*程序来演示问题吗? –

回答

1

Control.Location是指相对于对照的容器(或更确切地说,它的左上点)的位置

Cursor.Position(和Control.MousePosition)指的是相对于屏幕的左上角的位置

因此,如果您的控件的容器恰好位于屏幕的左上角,那么您的代码将起作用。否则,它会像你看到的那样被抵消。您应该看看Control.PointToScreenControl.PointToClient方法是否可以帮助您;该文档并不完全清楚,但他们可能正是你所需要的。例如:

nBtn.Location = PictureBox1.PointToClient(Cursor.Position)