最近看到很多人对C#运行期控件设计的问题于是写了这个Memo希望对大家有用
实现了控件自由拖动
实现了控件的拖动创建右键删除等类似IDE的控件创建当然更多功能靠大家自己完善
实现属性框与控件的绑定可以在运行期修改控件的Text
以下是部分代码
view plaincopy to clipboardprint?
privatevoidbutton_Click(objectsenderEventArgse)
{
//控件框的显示与隐藏
if(panelVisible==true)
{
buttonText=+控件框;
panelVisible=false;
}
else
{
buttonText=控件框;
panelVisible=true;
}
}
view plaincopy to clipboardprint?
privatevoidbutton_MouseDown(objectsenderMouseEventArgse)
{
//判断鼠标左键按下
if(eButton==MouseButtonsLeft)
{
Buttonbtn=(Button)(sender);
//初始化拖放操作
btnDoDragDrop(btnDragDropEffectsCopy);
}
}
privatevoidpanel_DragDrop(objectsenderDragEventArgse)
{
//开始拖动
Buttonbtn=(Button)(eDataGetData(SystemWindowsFormsButton));
Buttonbtn_new=newButton();
btn_newContextMenuStrip=contextMenuStrip;
btn_newName=btn_newText=btnText++name;
btn_newLeft=PointToClient(MousePosition)XpanelLeft;
btn_newTop=PointToClient(MousePosition)YpanelTop;
//加载事件
btn_newClick+=newSystemEventHandler(thisbutton_Click);
btn_newMouseLeave+=newSystemEventHandler(thisbutton_MouseLeave);
btn_newMouseDown+=newSystemWindowsFormsMouseEventHandler(thisbutton_MouseDown);
btn_newMouseMove+=newSystemWindowsFormsMouseEventHandler(thisbutton_MouseMove);
btn_newParent=panel;
name++;
}
view plaincopy to clipboardprint?
privatevoidpanel_DragEnter(objectsenderDragEventArgse)
{
eEffect=DragDropEffectsCopy;
}
privatevoidbutton_Click(objectsenderEventArgse)
{
groupBoxText=(senderasButton)Name+属性;
textBoxText=(senderasButton)Text;
}
privatevoidbutton_MouseDown(objectsenderMouseEventArgse)
{
//判断鼠标
左键按下
if(eButton==MouseButtonsLeft)
{
Buttonbtn=(Button)(sender);
//初始化拖放操作
btnDoDragDrop(btnDragDropEffectsCopy);
}
}
view plaincopy to clipboardprint?
privatevoidtoolStripMenuItem_Click(objectsenderEventArgse)
{
//释放控件
btnflagDispose();
}
privatevoidtextBox_KeyPress(objectsenderKeyPressEventArgse)
{
btnflagText=textBoxText;
}
privatevoidtextBox_KeyDown(objectsenderKeyEventArgse)
{
//响应回车
if(eKeyValue==)
btnflagText=textBoxText;
}
privatevoidbutton_Click(objectsenderEventArgse)
{
//控件框的显示与隐藏
if(groupBoxVisible==true)
{
buttonText=+属性窗口;
groupBoxVisible=false;
}
else
{
buttonText=属性窗口;
groupBoxVisible=true;
}
}
实现的效果图如下
更多功能当然需要大家自己扩展假如你有兴趣