所有栏目 | 云社区 美国云服务器[国内云主机商]
你的位置:首页 > 云社区 » 正文

c#判断控件位置?

发布时间:2020-04-15 16:50:36

资讯分类:控件  判断  位置  这是  输出  格式化
c#判断控件位置?

这是printf的格式化参数,表示输出一个整型数值,输出宽度为4,且右对齐,如: printf( "%4d c#判断控件位置?

我以前做过一个贪吃蛇,蛇不能碰到障碍物,我想和你需要的情况类似

要用到控件的几个位置属性,Location(顶点坐标),width,heigth,left(Location.x),right(Location.x+width),top(Location.Y),Bottom(Location.Y+Heigth)

先拖一个Panel1

加个方法

private bool IsTouch(Control c)

{

foreach (Control con in this.panel1.Controls)

{//如果在这个范围内,表示此控件会存在覆盖

if (c.Right >= con.Left && c.Left <= con.Right && c.Bottom >= con.Top && c.Top <= con.Bottom)

return true;

}

return false;

}

如下是测试代码:

Button b = new Button();

b.Location = new Point(20,50);//左上角顶点坐标

b.Width = 50;//宽

b.Height = 50;//高

b.Text = "Test";

if (!IsTouch(b))//如果不会覆盖,添加到Panel

{

this.panel1.Controls.Add(b);

}

留言与评论(共有 0 条评论)
   
验证码:
Top