1.代码块:
//记录位置
Point po = Point.Empty;
//生成的控件
Label lbl = null;
Label txt = null;
// 接口方法
RegionlStaffQueryManager rsqm = new RegionlStaffQueryManager();//获取生成控件的Count
List<AreaParameterBusiness> list = new List<AreaParameterBusiness>();//实体
//默认加载Load
private void newAreaPicQuery_Load(object sender, EventArgs e)
{
CreateControls();
this.gbfill.AllowDrop = true;
}
/// <summary>
/// 区域名称初始化
/// </summary>
private void CreateControls()
{
int x = 10;
int y = 20;
list = rsqm.AreaConfigInfo();
for (int i = 0; i < list.Count; i++)
{
string lbltext = list[i].AreaName;
string lblname = "lbl" + list[i].SystemID;
lbl = new Label();
lbl.Left = x;
lbl.Top = y;
lbl.Font = new Font("楷书", 14, FontStyle.Bold);
lbl.Name = lblname;
lbl.Text = lbltext;
lbl.ContextMenuStrip = menu;
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
lbl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
lbl.DoubleClick += lbl_DoubleClick;
x += lbl.Width + 5;
if (lbl.Width > gbleft.Width - x)
{
x = 10;
y += lbl.Height + 10;
}
this.gbleft.Controls.Add(lbl);
}
}
/// <summary>
/// 双击产生区域文本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void lbl_DoubleClick(object sender, EventArgs e)
{
string checkname = ((Label)sender).Name;
string checktext = null;
string TextName = ((Label)sender).Text;
Control con = ExtensionClass.FindControl(gbfill, checkname);
if (con == null)
{
list = rsqm.AreaConfigInfo();
for (int i = 0; i < list.Count; i++)
{
if (TextName.Equals(list[i].AreaName))
{
checktext = "区域名称:" + list[i].AreaName + "\r\n" + "区域人数:" + list[i].TheTotal;
}
}
txt = new Label();
txt.AutoSize = false;
txt.BackColor = Color.Transparent;
txt.ForeColor = Color.Aqua;
txt.Name = checkname;
txt.Text = checktext;
//窗体重绘,具有边框颜色
//txt.Paint += txt_Paint;
txt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//读取配置文件
txt.Location = new Point(5, 10);
txt.Height = Convert.ToInt32(AppConfig.height);
txt.Width = Convert.ToInt32(AppConfig.width);
txt.AllowDrop = true;
txt.MouseUp += txt_MouseUp;
txt.MouseDown += txt_MouseDown;
this.gbfill.Controls.Add(txt);
}
else
{
MessageBox.Show("已经存在");
}
}
//鼠标按下
private void txt_MouseDown(object sender, MouseEventArgs e)
{
txt = (Label)sender;
}
//鼠标释放
private void txt_MouseUp(object sender, MouseEventArgs e)
{
txt.Location = getPointToForm(new Point(e.Location.X - gbfill.Location.X, e.Location.Y));
txt.DoDragDrop(txt, DragDropEffects.Move);
}
private Point getPointToForm(Point p)
{
return this.PointToClient(txt.PointToScreen(p));
}
2.效果图: