⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordcountdialog.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  WordCountDialog.cs 
//  Copyright (C) 2000 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

using SharpDevelop.Gui;
using SharpDevelop.Gui.Window;
using SharpDevelop.Tool.Data;
using SharpDevelop.Tool.Function;

namespace SharpDevelop.Gui.Dialogs {
	
	/// <summary>
	///    Summary description for Win32Form1.
	/// </summary>
	public class WordCountDialog : System.Windows.Forms.Form 
	{
		
		Container components;
		
		ColumnHeader lineHeader;
		ColumnHeader wordHeader;
		ColumnHeader charHeader;
		ColumnHeader fileHeader;
		
		Button button1;
		Button cancelButton;
		Button startButton;
		Label label1;
		ComboBox comboBox1;
		ListView listView1;
		
		MainWindow main;
		
	    internal class Report
	    {
	    	string name;
	    	long chars;
	    	long words;
	    	long lines;
	    	
	    	public Report(string name, long chars, long words, long lines)
	    	{
	    		this.name  = name;
	    		this.chars = chars;
	    		this.words = words;
	    		this.lines = lines;
	    	}
	    	
	    	public ListViewItem ToListItem()
	    	{
	    		return new ListViewItem(new string[] { Path.GetFileName(name), chars.ToString(), words.ToString(), lines.ToString()});
	    	}
	    	
	    	public static Report operator+(Report r, Report s)
	    	{
	    		Report tmp = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), s.chars, s.words, s.lines);
	    		tmp.chars += r.chars;
	    		tmp.words += r.words;
	    		tmp.lines += r.lines;
	    		return tmp;
	    	}
	    }
	    
		Report GetReport(string filename)
		{
			long numLines = 0;
			long numWords = 0;
			long numChars = 0;
			
            FileStream istream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(istream);
            string line = sr.ReadLine();
            while (line != null) {
            	++numLines;
                numChars += line.Length;
                string[] words = line.Split(null); 
                numWords += words.Length;
            	line = sr.ReadLine();
            }
			
            sr.Close();
	    	return new Report(filename, numChars, numWords, numLines);
		}
	    
		void startEvent(object sender, System.EventArgs e)
	    {
			listView1.BeginUpdate();
	    	listView1.Items.Clear();
	    	switch (comboBox1.SelectedIndex) {
	    		case 0: {// current file 
	    			ContentWindow window = main.ActiveContentWindow;
	    			if (window != null) {
	    				if (window.HasTextArea) {
	    					if (window.Untitled) {
								MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.SaveTheFileWarning"), Resource.GetString("Global.WarningText"),
											MessageBoxButtons.OK, MessageBoxIcon.Warning);
	    					} else {
	    						if (FSTypeUtility.TestFileExists(window.FileName)) {
			    					listView1.Items.Add(GetReport(window.FileName).ToListItem());
	    						}
	    					}
	    				}
	    			} 
	    			break;
	    		}
	    		case 1: {// all open files
	    			if (main.MdiChildren.Length > 0) {
	    				string tmp = "";
	    				
		    			Report all = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), 0, 0, 0);
						foreach (ContentWindow window in main.MdiChildren) {
	    					if (window.Untitled) {
								MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.SaveAllFileWarning"), Resource.GetString("Global.WarningText"),
											MessageBoxButtons.OK, MessageBoxIcon.Warning);
								continue;
	    					} else {
	    						if (window.HasTextArea && FSTypeUtility.TestFileExists(window.FileName)) {
									Report r = GetReport(window.FileName);
									tmp += r.ToString();
									all += r;
				    				listView1.Items.Add(r.ToListItem());
	    						}
	    					}
						}
						listView1.Items.Add(new ListViewItem(""));
	    				listView1.Items.Add(all.ToListItem());
	    			}
	    			break;
	    		}
	    		case 2: {// whole project
	    			if (!main.ProjectMode) {
	    				MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.MustBeInProtectedModeWarning"), Resource.GetString("Global.ErrorText"), MessageBoxButtons.OK, MessageBoxIcon.Error);
	    				break;
	    			}
	    			string tmp = "";
	    			Report all = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), 0, 0, 0);
					foreach (string name in main.ProjectBrowser.Project.Files) 
					if (Path.GetExtension(name) == ".cs") {
						Report r = GetReport(name);
						tmp += r.ToString();
						all += r;
		    			listView1.Items.Add(r.ToListItem());
					}
					listView1.Items.Add(new ListViewItem(""));
	    			listView1.Items.Add(all.ToListItem());
	    			break;
	    		}
	    	}
			listView1.EndUpdate();
	    }
	    
	    // TODO : Sorting
		void SortEvt(object sender, ColumnClickEventArgs e)
		{
			listView1.BeginUpdate();
			switch (e.Column) {
				case 0:  // files
					break;
				case 1:  // chars
					break;
				case 2:  // words
					break;
				case 3:  // lines
					break;
			}
			listView1.EndUpdate();
		}
		
		public WordCountDialog(MainWindow main)
		{
			InitializeComponent();
			this.main = main;
			AcceptButton = startButton;
			CancelButton = cancelButton;
			startButton.Click += new System.EventHandler(startEvent);
			listView1.ColumnClick += new ColumnClickEventHandler(SortEvt);
			Owner = main;
			ShowInTaskbar = false;
			Icon = null;
			StartPosition = FormStartPosition.CenterParent;
		}
		
		/// <summary>
		///    Clean up any resources being used
		/// </summary>
		public override void Dispose() {
			base.Dispose();
			components.Dispose();
		}
		
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.listView1 = new System.Windows.Forms.ListView();
			this.charHeader = new System.Windows.Forms.ColumnHeader();
			this.lineHeader = new System.Windows.Forms.ColumnHeader();
			this.label1 = new System.Windows.Forms.Label();
			this.wordHeader = new System.Windows.Forms.ColumnHeader();
			this.cancelButton = new System.Windows.Forms.Button();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.startButton = new System.Windows.Forms.Button();
			this.fileHeader = new System.Windows.Forms.ColumnHeader();
			this.button1 = new System.Windows.Forms.Button();
			
			listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Clickable;
			listView1.MultiSelect = false;
			listView1.Size = new System.Drawing.Size(368, 256);
			listView1.FullRowSelect = true;
			listView1.View = System.Windows.Forms.View.Details;
			listView1.ForeColor = System.Drawing.SystemColors.WindowText;
			listView1.GridLines = true;
			listView1.TabIndex = 0;
			listView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left;
			listView1.Location = new System.Drawing.Point(8, 40);
			listView1.Columns.Add(fileHeader);
			listView1.Columns.Add(charHeader);
			listView1.Columns.Add(wordHeader);
			listView1.Columns.Add(lineHeader);
				
			charHeader.Text = Resource.GetString("Dialog.WordCountDialog.CharsText");
			charHeader.Width = 60;
			charHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			
			lineHeader.Text = Resource.GetString("Dialog.WordCountDialog.LinesText");
			lineHeader.Width = 60;
			lineHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			
			label1.Location = new System.Drawing.Point(8, 8);
			label1.Text = Resource.GetString("Dialog.WordCountDialog.Label1Text");
			
			label1.Size = new System.Drawing.Size(80, 16);
			label1.TabIndex = 2;
			
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.Text = Resource.GetString("Dialog.WordCountDialog.DialogName");
			this.MaximizeBox = false;
			//@design this.TrayLargeIcon = true;
			this.ShowInTaskbar = false;
			this.FormBorderStyle = FormBorderStyle.Sizable;
			//@design this.TrayHeight = 0;
//			this.TopMost = true;
			this.MinimizeBox = false;
			this.ClientSize = new System.Drawing.Size(456 + 10, 301);
			
			wordHeader.Text = Resource.GetString("Dialog.WordCountDialog.WordsText");
			wordHeader.Width = 60;
			wordHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			
			cancelButton.Location = new System.Drawing.Point(384, 40);
			cancelButton.Size = new System.Drawing.Size(74, 24);
			cancelButton.TabIndex = 4;
			cancelButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			cancelButton.Text = Resource.GetString("Global.CancelButtonText");
			cancelButton.DialogResult = DialogResult.Cancel;
			
			comboBox1.Location = new System.Drawing.Point(96, 8);
			comboBox1.Text = "";
			comboBox1.Size = new System.Drawing.Size(176, 21);
			comboBox1.TabIndex = 1;
			comboBox1.Items.Add(Resource.GetString("Global.Location.currentfile"));
			comboBox1.Items.Add(Resource.GetString("Global.Location.allopenfiles"));
			comboBox1.Items.Add(Resource.GetString("Global.Location.wholeproject"));
			comboBox1.ValueMember = comboBox1.Items[0].ToString();
			
			comboBox1.SelectedIndex = 0;
			startButton.Location = new System.Drawing.Point(384, 8);
			startButton.Size = new System.Drawing.Size(74, 24);
			startButton.TabIndex = 3;
			startButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			startButton.Text = Resource.GetString("Dialog.WordCountDialog.StartButton"); 
			
			fileHeader.Text = Resource.GetString("Dialog.WordCountDialog.FileText"); 
			fileHeader.Width = 177;
			fileHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
			
			button1.Location = new System.Drawing.Point(384, 80);
			button1.Size = new System.Drawing.Size(74, 24);
			button1.TabIndex = 5;
			button1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			button1.Text = Resource.GetString("Global.HelpButtonText");
			
			this.Controls.Add(button1);
			this.Controls.Add(cancelButton);
			this.Controls.Add(startButton);
			this.Controls.Add(label1);
			this.Controls.Add(comboBox1);
			this.Controls.Add(listView1);
			
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -