现在的位置: 首页 > 综合 > 正文

C# 模拟Visual Studio 2005工具箱

2014年02月15日 ⁄ 综合 ⁄ 共 3630字 ⁄ 字号 评论关闭
        做得比较粗糙,大概意思和Visual Studio的工具箱差不多。当然,实现的方法有很多,仁者见仁智者见智了。结合了DockPanel,效果还可以。控件用到button两个:btnControls、btnLine,listview两个:listViewControl、listViewLine,imagelist两个:imageListControl、imageListLine,几个图片:ExpandedBlockStart、ExpandedBlockStart。代码如下:

  1. using System;
  2. using System.Windows.Forms;
  3. using WeifenLuo.WinFormsUI.Docking;
  4. namespace Practice
  5. {
  6.     public partial class ToolBox : DockContent
  7.     {
  8.         public ToolBox()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         private bool listViewControlOpen = true//listViewControl展开状态
  13.         private bool listViewLineOpen = true//listViewLine展开状态
  14.         protected override void OnLoad(EventArgs e)
  15.         {
  16.             base.OnLoad(e);
  17.             //设置停靠位置
  18.             listViewControl.Dock = DockStyle.Top;
  19.             listViewControl.SendToBack();
  20.             btnControls.Dock = DockStyle.Top;
  21.             btnControls.SendToBack();
  22.             btnLine.Dock = DockStyle.Top;
  23.             listViewLine.Dock = DockStyle.Top;
  24.             listViewControl.Items.Add("RECTANGLE", 0);
  25.             listViewControl.Items.Add("TRIANGLE", 1);
  26.             listViewLine.Items.Add("LINE", 0);
  27.             btnControls.Image = Properties.Resources.ExpandedBlockStart;
  28.             btnLine.Image = Properties.Resources.ExpandedBlockStart;
  29.         }
  30.         public void btnControls_Click(object sender, EventArgs e)
  31.         {
  32.             if (listViewControlOpen == true)
  33.             {
  34.                 listViewControl.Visible = false;
  35.                 listViewControlOpen = false;
  36.                 btnControls.Image = Properties.Resources.ContractedBlock;
  37.             }
  38.             else
  39.             {
  40.                 listViewControl.Visible = true;
  41.                 listViewControlOpen = true;
  42.                 btnControls.Image = Properties.Resources.ExpandedBlockStart;
  43.             }
  44.         }
  45.         public void btnLine_Click(object sender, EventArgs e)
  46.         {
  47.             if (listViewLineOpen == true)
  48.             {
  49.                 listViewLine.Visible = false;
  50.                 listViewLineOpen = false;
  51.                 btnLine.Image = Properties.Resources.ContractedBlock;
  52.             }
  53.             else
  54.             {
  55.                 listViewLine.Visible = true;
  56.                 listViewLineOpen = true;
  57.                 btnLine.Image = Properties.Resources.ExpandedBlockStart;
  58.             }
  59.         }
  60.         protected override void OnClick(EventArgs e)
  61.         {
  62.             base.OnClick(e);
  63.             this.Focus();
  64.         }
  65.         private void listViewControl_ItemDrag(object sender, ItemDragEventArgs e)
  66.         {
  67.             this.Focus();
  68.             string ctrlName = listViewControl.FocusedItem.Text;
  69.             DoDragDrop(ctrlName, DragDropEffects.Move);
  70.         }
  71.         private void listViewLine_ItemDrag(object sender, ItemDragEventArgs e)
  72.         {
  73.             this.Focus();
  74.             string ctrlName = listViewLine.FocusedItem.Text;
  75.             DoDragDrop(ctrlName, DragDropEffects.Move);
  76.         }
  77.         protected override void OnResize(EventArgs e)
  78.         {
  79.             base.OnResize(e);
  80.             if (listViewControl.Items.Count != 0)
  81.             {
  82.                 ChangeSize(listViewControl);
  83.                 ChangeSize(listViewLine);
  84.             }
  85.         }
  86.         private void ChangeSize(ListView ctrl)
  87.         {
  88.             int rows;
  89.             int itemsCount = ctrl.Items.Count;
  90.             int itemWidth = ctrl.Items[0].Bounds.Width + 1;
  91.             int itemHeight = ctrl.Items[0].Bounds.Height;
  92.             int rowItems = ctrl.Width / itemWidth;
  93.             if (rowItems < 1)
  94.                 rowItems = 1;
  95.             rows = (itemsCount + rowItems - 1) / rowItems;
  96.             ctrl.Height = (int)((itemHeight + 23) * rows);
  97.         }
  98.     }
  99. }

抱歉!评论已关闭.