Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 10k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. namespace ch3_4
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.TreeView treeView1;
  16. private System.Windows.Forms.ImageList imageList1;
  17. private System.Windows.Forms.GroupBox groupBox1;
  18. private System.Windows.Forms.CheckBox checkBox1;
  19. private System.Windows.Forms.CheckBox checkBox2;
  20. private System.Windows.Forms.CheckBox checkBox3;
  21. private System.Windows.Forms.CheckBox checkBox4;
  22. private System.Windows.Forms.CheckBox checkBox5;
  23. private System.Windows.Forms.CheckBox checkBox6;
  24. private System.Windows.Forms.CheckBox checkBox7;
  25. private System.ComponentModel.IContainer components;
  26. public Form1()
  27. {
  28. //
  29. // Windows 窗体设计器支持所必需的
  30. //
  31. InitializeComponent();
  32. //初始化TreeView的根节点
  33. InitialTreeView();
  34. //
  35. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  36. //
  37. }
  38. /// <summary>
  39. /// 清理所有正在使用的资源。
  40. /// </summary>
  41. protected override void Dispose( bool disposing )
  42. {
  43. if( disposing )
  44. {
  45. if (components != null) 
  46. {
  47. components.Dispose();
  48. }
  49. }
  50. base.Dispose( disposing );
  51. }
  52.            
  53. private void InitialTreeView()
  54. {
  55.     //将驱动器字符串数组设为空
  56. string[] drivers=null;
  57. //检索此计算机上逻辑驱动器的名称
  58. drivers=Directory.GetLogicalDrives();
  59. int i=0;
  60. //初始化每一个逻辑驱动器
  61. while ( i<drivers.GetLength(0))
  62. {
  63. this.treeView1.Nodes.Add(new TreeNode(drivers[i],1,1));
  64. string path=drivers[i];
  65. string[] dirs=null;
  66. try
  67. {
  68. //获得指定驱动器中第一级目录的名称
  69. dirs=Directory.GetDirectories(path);
  70. }
  71. catch(Exception  error)
  72. {
  73. //错误处理为空,即忽略
  74. }
  75. if (dirs!=null)
  76. {
  77. //为每一个代表驱动器的根节点添加子节点
  78. for (int j=0;j<dirs.Length;j++)
  79. {
  80. //获得节点的去掉路径后的目录名
  81. TreeNode node = new TreeNode(dirs[j].ToString().Substring(dirs[j].ToString().LastIndexOf("\")+1));
  82. //设置不选定状态下的图标
  83. node.ImageIndex =2;
  84. //设置打开状态下的图标
  85. node.SelectedImageIndex =0;
  86. //添加节点
  87. this.treeView1.Nodes[i].Nodes.Add(node);
  88. }
  89. }
  90. //继续下一个循环
  91. i++; 
  92. }
  93. }
  94. #region Windows Form Designer generated code
  95. /// <summary>
  96. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  97. /// 此方法的内容。
  98. /// </summary>
  99. private void InitializeComponent()
  100. {
  101. this.components = new System.ComponentModel.Container();
  102. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  103. this.treeView1 = new System.Windows.Forms.TreeView();
  104. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  105. this.groupBox1 = new System.Windows.Forms.GroupBox();
  106. this.checkBox1 = new System.Windows.Forms.CheckBox();
  107. this.checkBox2 = new System.Windows.Forms.CheckBox();
  108. this.checkBox3 = new System.Windows.Forms.CheckBox();
  109. this.checkBox4 = new System.Windows.Forms.CheckBox();
  110. this.checkBox5 = new System.Windows.Forms.CheckBox();
  111. this.checkBox6 = new System.Windows.Forms.CheckBox();
  112. this.checkBox7 = new System.Windows.Forms.CheckBox();
  113. this.groupBox1.SuspendLayout();
  114. this.SuspendLayout();
  115. // 
  116. // treeView1
  117. // 
  118. this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
  119. this.treeView1.ImageList = this.imageList1;
  120. this.treeView1.Name = "treeView1";
  121. this.treeView1.Size = new System.Drawing.Size(292, 341);
  122. this.treeView1.TabIndex = 0;
  123. this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
  124. // 
  125. // imageList1
  126. // 
  127. this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  128. this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
  129. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  130. this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
  131. // 
  132. // groupBox1
  133. // 
  134. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  135. this.checkBox7,
  136. this.checkBox6,
  137. this.checkBox5,
  138. this.checkBox4,
  139. this.checkBox3,
  140. this.checkBox2,
  141. this.checkBox1});
  142. this.groupBox1.Location = new System.Drawing.Point(320, 24);
  143. this.groupBox1.Name = "groupBox1";
  144. this.groupBox1.Size = new System.Drawing.Size(200, 304);
  145. this.groupBox1.TabIndex = 1;
  146. this.groupBox1.TabStop = false;
  147. this.groupBox1.Text = "样式";
  148. // 
  149. // checkBox1
  150. // 
  151. this.checkBox1.Location = new System.Drawing.Point(40, 32);
  152. this.checkBox1.Name = "checkBox1";
  153. this.checkBox1.TabIndex = 0;
  154. this.checkBox1.Text = "排序";
  155. this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
  156. // 
  157. // checkBox2
  158. // 
  159. this.checkBox2.Checked = true;
  160. this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked;
  161. this.checkBox2.Location = new System.Drawing.Point(40, 69);
  162. this.checkBox2.Name = "checkBox2";
  163. this.checkBox2.Size = new System.Drawing.Size(128, 24);
  164. this.checkBox2.TabIndex = 1;
  165. this.checkBox2.Text = "显示节点间线条";
  166. this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
  167. // 
  168. // checkBox3
  169. // 
  170. this.checkBox3.Checked = true;
  171. this.checkBox3.CheckState = System.Windows.Forms.CheckState.Checked;
  172. this.checkBox3.Location = new System.Drawing.Point(40, 106);
  173. this.checkBox3.Name = "checkBox3";
  174. this.checkBox3.Size = new System.Drawing.Size(136, 24);
  175. this.checkBox3.TabIndex = 2;
  176. this.checkBox3.Text = "显示根节点间线条";
  177. this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);
  178. // 
  179. // checkBox4
  180. // 
  181. this.checkBox4.Location = new System.Drawing.Point(40, 143);
  182. this.checkBox4.Name = "checkBox4";
  183. this.checkBox4.TabIndex = 3;
  184. this.checkBox4.Text = "显示复选框";
  185. this.checkBox4.CheckedChanged += new System.EventHandler(this.checkBox4_CheckedChanged);
  186. // 
  187. // checkBox5
  188. // 
  189. this.checkBox5.Checked = true;
  190. this.checkBox5.CheckState = System.Windows.Forms.CheckState.Checked;
  191. this.checkBox5.Location = new System.Drawing.Point(40, 180);
  192. this.checkBox5.Name = "checkBox5";
  193. this.checkBox5.TabIndex = 4;
  194. this.checkBox5.Text = "显示+ - 按钮";
  195. this.checkBox5.CheckedChanged += new System.EventHandler(this.checkBox5_CheckedChanged);
  196. // 
  197. // checkBox6
  198. // 
  199. this.checkBox6.Checked = true;
  200. this.checkBox6.CheckState = System.Windows.Forms.CheckState.Checked;
  201. this.checkBox6.Location = new System.Drawing.Point(40, 217);
  202. this.checkBox6.Name = "checkBox6";
  203. this.checkBox6.TabIndex = 5;
  204. this.checkBox6.Text = "隐藏选择";
  205. this.checkBox6.CheckedChanged += new System.EventHandler(this.checkBox6_CheckedChanged);
  206. // 
  207. // checkBox7
  208. // 
  209. this.checkBox7.Location = new System.Drawing.Point(40, 254);
  210. this.checkBox7.Name = "checkBox7";
  211. this.checkBox7.TabIndex = 6;
  212. this.checkBox7.Text = "鼠标变色";
  213. this.checkBox7.CheckedChanged += new System.EventHandler(this.checkBox7_CheckedChanged);
  214. // 
  215. // Form1
  216. // 
  217. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  218. this.ClientSize = new System.Drawing.Size(552, 341);
  219. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  220.   this.groupBox1,
  221.   this.treeView1});
  222. this.Name = "Form1";
  223. this.Text = "查看磁盘与目录";
  224. this.groupBox1.ResumeLayout(false);
  225. this.ResumeLayout(false);
  226. }
  227. #endregion
  228. /// <summary>
  229. /// 应用程序的主入口点。
  230. /// </summary>
  231. [STAThread]
  232. static void Main() 
  233. {
  234. Application.Run(new Form1());
  235. }
  236. private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
  237. {
  238.             //获取发出信息的节点
  239. TreeNode node=e.Node;
  240. //设置它为treeView的当前节点
  241. this.treeView1.SelectedNode=node;
  242. string path=node.FullPath;
  243. string[] dirs=null;
  244. try
  245. {
  246. //获得当前节点所有的子节点
  247. dirs=Directory.GetDirectories(path);
  248. }
  249. catch(Exception  error)
  250. {
  251. //错误处理为空,即忽略
  252. }
  253.             
  254. if (dirs!=null)
  255. {
  256. //为每一个当前节点的字节点添加子节点
  257. for (int j=0;j<dirs.Length;j++)
  258. {
  259. //如果当前节点的子节点数为0,则为它添加子节点
  260. //否则说明已经添加过或则没有子目录
  261. if (node.Nodes[j].Nodes.Count==0)
  262. {
  263. //调用函数为子节点添加子节点
  264. addDirectroy(node.Nodes[j],dirs[j]);
  265. }
  266. }
  267. }
  268. }
  269. private void addDirectroy(TreeNode node,string path)
  270. {
  271. string[] dirs=null;
  272. try
  273. {
  274. dirs=Directory.GetDirectories(path);
  275. }
  276. catch(Exception  e)
  277. {
  278. //错误处理为空,即忽略
  279. }
  280. for (int j=0;j<dirs.Length;j++)
  281. {
  282. //添加新的子节点
  283. TreeNode nodeNew= new TreeNode(dirs[j].ToString().Substring(dirs[j].ToString().LastIndexOf("\")+1));
  284. //设置不选定状态下的图标
  285. nodeNew.ImageIndex =2;
  286. //设置打开状态下的图标
  287. nodeNew.SelectedImageIndex =0;
  288. node.Nodes.Add(nodeNew);
  289. }
  290. }
  291. private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
  292. {
  293. this.treeView1.Sorted = this.checkBox1.Checked;
  294. }
  295. private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
  296. {
  297. this.treeView1.ShowLines=this.checkBox2.Checked;
  298. }
  299. private void checkBox3_CheckedChanged(object sender, System.EventArgs e)
  300. {
  301. this.treeView1.ShowRootLines=this.checkBox3.Checked;
  302. }
  303. private void checkBox4_CheckedChanged(object sender, System.EventArgs e)
  304. {
  305. this.treeView1.CheckBoxes=this.checkBox4.Checked;
  306. }
  307.         private void checkBox5_CheckedChanged(object sender, System.EventArgs e)
  308. {
  309. this.treeView1.ShowPlusMinus=this.checkBox5.Checked;
  310. }
  311. private void checkBox6_CheckedChanged(object sender, System.EventArgs e)
  312. {
  313. this.treeView1.HideSelection=this.checkBox6.Checked;
  314. }
  315. private void checkBox7_CheckedChanged(object sender, System.EventArgs e)
  316. {
  317. this.treeView1.HotTracking=this.checkBox7.Checked;
  318. }
  319. }
  320. }