【问题标题】:Control position offset in C# WinForms?在 C# WinForms 中控制位置偏移?
【发布时间】:2018-09-09 09:52:51
【问题描述】:

在 C# WinForms(或 Win32 API)中是否有一种方法可以偏移窗体的子控件的原点 {0,0} 坐标而不添加控件作为父控件并且不扩展窗口边框?

这就是我的意思:

Win32 API 是否有类似 SetChildOffset() 函数的东西?我希望窗口边框保持不变。

【问题讨论】:

  • 您的意思是要更改ClientRectangle
  • 为什么不直接在需要的地方添加控件呢?使用面板是否也有任何费用?请说明您的问题的目的,以便我们帮助您提出合理的解决方案。

标签: c# windows winforms winapi


【解决方案1】:

没有 API 可以做到这一点。而是使用这个:

private void SetChildOffset(int offset) {
    //get all immediate children of form
    var children = this.Controls.OfType<Control>();

    foreach( Control child in children ) {

        child.Location = new Point( child.Location.X + offset, child.Location.Y + offset );

    }

}

【讨论】:

  • 我可能还会看到Rectangle.Offset() 方法。 SetChildOffset() 方法可以接受Point 作为参数(在本例中为Button.Location),定义Rectangle bounds == ChildControl.Bounds 然后调用bounds.Offset([Point]) 来转换边界位置。不能直接在 Control.Bounds 属性上调用。
  • 不是我的方法 :) 这只是预定义的方法。注释的意思是不能用单个int 值来偏移位置。你需要XY 两个职位。 Offset() 方法的工作方式如下:Point Offset = [ReferenceControl].Location; Rectangle bounds = [Control].Bounds; bounds.Offset(Offset); [Control].Bounds = bounds;。我不会发布另一个答案。没必要。
  • @Jimi Aaa!是的,我明白了。当然它应该是Point 而不是int。我什至没有想到!不错的收获
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 2011-03-12
相关资源
最近更新 更多