最近看到很多人對C#運行期控件設計的問題於是寫了這個Memo希望對大家有用
實現了控件自由拖動
實現了控件的拖動創建右鍵刪除等類似IDE的控件創建當然更多功能靠大家自己完善
實現屬性框與控件的綁定可以在運行期修改控件的Text
以下是部分代碼
view plaincopy to clipboardprint?
private void button_Click(object sender EventArgs e)
{
//控件框的顯示與隱藏
if (panelVisible == true)
{
buttonText = + 控件框;
panelVisible = false;
}
else
{
buttonText = 控件框;
panelVisible = true;
}
}
view plaincopy to clipboardprint?
private void button_MouseDown(object sender MouseEventArgs e)
{
//判斷鼠標左鍵按下
if (eButton == MouseButtonsLeft)
{
Button btn = (Button)(sender);
//初始化拖放操作
btnDoDragDrop(btn DragDropEffectsCopy);
}
}
private void panel_DragDrop(object sender DragEventArgs e)
{
//開始拖動
Button btn = (Button)(eDataGetData(SystemWindowsFormsButton));
Button btn_new = new Button();
btn_newContextMenuStrip = contextMenuStrip;
btn_newName = btn_newText = btnText + + name;
btn_newLeft = PointToClient(MousePosition)XpanelLeft;
btn_newTop = PointToClient(MousePosition)Y panelTop;
//加載事件
btn_newClick += new SystemEventHandler(thisbutton_Click);
btn_newMouseLeave += new SystemEventHandler(thisbutton_MouseLeave);
btn_newMouseDown += new SystemWindowsFormsMouseEventHandler(thisbutton_MouseDown);
btn_newMouseMove += new SystemWindowsFormsMouseEventHandler(thisbutton_MouseMove);
btn_newParent = panel;
name++;
}
view plaincopy to clipboardprint?
private void panel_DragEnter(object sender DragEventArgs e)
{
eEffect = DragDropEffectsCopy;
}
private void button_Click(object sender EventArgs e)
{
groupBoxText = (sender as Button)Name + 屬性;
textBoxText = (sender as Button)Text;
}
private void button_MouseDown(object sender MouseEventArgs e)
{
//判斷鼠標
左鍵按下
if (eButton == MouseButtonsLeft)
{
Button btn = (Button)(sender);
//初始化拖放操作
btnDoDragDrop(btn DragDropEffectsCopy);
}
}
view plaincopy to clipboardprint?
private void toolStripMenuItem_Click(object sender EventArgs e)
{
//釋放控件
btnflagDispose();
}
private void textBox_KeyPress(object sender KeyPressEventArgs e)
{
btnflagText = textBoxText;
}
private void textBox_KeyDown(object sender KeyEventArgs e)
{
//響應回車
if (eKeyValue == )
btnflagText = textBoxText;
}
private void button_Click(object sender EventArgs e)
{
//控件框的顯示與隱藏
if (groupBoxVisible == true)
{
buttonText = + 屬性窗口;
groupBoxVisible = false;
}
else
{
buttonText = 屬性窗口;
groupBoxVisible = true;
}
}
實現的效果圖如下
更多功能當然需要大家自己擴展假如你有興趣
From:http://tw.wingwit.com/Article/program/net/201311/11926.html