加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#Windows窗体的装饰

发布时间:2020-12-16 01:34:12 所属栏目:百科 来源:网络整理
导读:我的WinForms应用程序中有一个画布(面板控件),用户可以在其中拖动文本框,标签等内容.但我想让它们更容易更精确地对齐对象.我已经读过它并且Adorners似乎是要走的路?但是,显然它只适用于 WPF.不幸的是,WPF对我来说不是一个选择. 我想要完成的是每次用户在画
我的WinForms应用程序中有一个画布(面板控件),用户可以在其中拖动文本框,标签等内容.但我想让它们更容易更精确地对齐对象.我已经读过它并且Adorners似乎是要走的路?但是,显然它只适用于 WPF.不幸的是,WPF对我来说不是一个选择.

我想要完成的是每次用户在画布中拖动对象时都会弹出行…它们是如何在Windows窗体设计器视图中工作的.

我会感激任何帮助.

谢谢.

解决方法

谢谢大家的答案.

我已经设法提出了自己的解决方案.代码在下面.还没有“线”,但是我会继续它…

Label l = (Label)sender;
foreach (Control control in Canvas.Controls)
{
    if (l.Location.X > control.Location.X + control.Size.Width && l.Location.X < control.Location.X + control.Size.Width + 5)
        l.Location = new Point(control.Location.X + control.Size.Width + 5,l.Location.Y);
    else if (l.Location.X < control.Location.X - l.Size.Width && l.Location.X > control.Location.X - l.Size.Width - 5)
        l.Location = new Point(control.Location.X - l.Size.Width - 5,l.Location.Y);
    else if (l.Location.Y > control.Location.Y + control.Size.Height && l.Location.Y < control.Location.Y + control.Size.Height + 5)
        l.Location = new Point(l.Location.X,control.Location.Y + control.Size.Height + 5);
    else if (l.Location.Y < control.Location.Y - control.Size.Height && l.Location.Y > control.Location.Y - control.Size.Height - 5)
        l.Location = new Point(l.Location.X,l.Location.Y - 5);

    this.Update();
}

上面的代码必须放在Control_MouseMove事件中,当然,你仍然需要自己的代码来实际移动控件.

上面的代码将捕捉您将5像素拖动到最近控件的右侧,左侧,顶部或底部的控件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读