弹出单词计数窗口(对复制文本).c#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows.Forms;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        // Name:                Word Count
        // Description:    Pops up a message box that shows some statistics about the text on the clipboard, such as word count, number of characters, number of lines, etc.


        // which message layout do we want to display?
        // feel free to add your own layouts below
        int intLayout = 1;    // possible values are 1 or 2

        // create an array of our objects
        // you can add more here as needed, but there may be limited space to display the results in the message box that pops
        objWordCount[] arrWordCount = new objWordCount[2];
        arrWordCount[0] = new objWordCount("Text", text);
        arrWordCount[1] = new objWordCount("No HTML Tags", HttpUtility.HtmlDecode(Regex.Replace(text, @"<[\w\W]+?>", string.Empty)));

        // string used to store our word count message
        string strMessage = string.Empty;

        // different layouts can be added here
        // depending on your OS, you may have to tweak the layouts a little bit
        switch(intLayout)
        {
            default:

                // this is the default layout

                // create our word count message
                for(int i = 0; i < arrWordCount.Length; i++)
                {
                    strMessage +=
                        (arrWordCount[i].title.ToUpper() + " ").PadRight(50, '\x2013') + "\r\n" +
                        "Words " + ("  " + arrWordCount[i].words.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters " + ("  " + arrWordCount[i].chars.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no spaces) " + ("  " + arrWordCount[i].charsNoSpaces.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no breaks) " + ("  " + arrWordCount[i].charsNoBreaks.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Characters (no breaks or spaces) " + ("  " + arrWordCount[i].charsNoSpacesBreaks.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Lines " + ("  " + arrWordCount[i].lines.ToString()).PadLeft(12, '_') + "\r\n" +
                        "Average characters per word \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].charsNoSpacesBreaks / (double)arrWordCount[i].words)))).PadLeft(12, '_') + "\r\n" +
                        "Average characters per line \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].charsNoBreaks / (double)arrWordCount[i].lines)))).PadLeft(12, '_') + "\r\n" +
                        "Average words per line \x2009" + ("  " + (String.Format("{0:0.0}",((double)arrWordCount[i].words / (double)arrWordCount[i].lines)))).PadLeft(12, '_') + "\r\n" +
                        "\r\n\r\n";
                }

                break;

            case 2:

                // this layout has a problem if there are more then two word count objects, use the default layout instead

                // set the length of our titles to a maximum of 12 characters (we can't have them being too long in this layout)
                for(int i = 0; i < arrWordCount.Length; i++)
                {
                    if(arrWordCount[i].title.Length > 12) arrWordCount[i].title = arrWordCount[i].title.Substring(0, 12);
                }

                // create our word count message
                strMessage = string.Empty;
                strMessage += "Clipboard Word Count";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (arrWordCount[i].title).PadLeft(12, '\x2002');
                strMessage += "\r\n" + new string('\x203E', 60) + "\r\nWords";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].words.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nCharacters";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].chars.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no spaces)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoSpaces.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no breaks)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoBreaks.ToString()).PadLeft(12, '_');
                strMessage += "\r\nCharacters (no spaces or breaks)";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].charsNoSpacesBreaks.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nLines";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + (" " + arrWordCount[i].lines.ToString()).PadLeft(12, '_');
                strMessage += "\r\n\r\nAverage characters per word";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].charsNoSpacesBreaks / (double)arrWordCount[i].words))).PadLeft(12, '_');
                strMessage += "\r\nAverage characters per line";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].charsNoBreaks / (double)arrWordCount[i].lines))).PadLeft(12, '_');
                strMessage += "\r\nAverage words per line";
                for(int i = 0; i < arrWordCount.Length; i++) strMessage += new string(' ', 5) + "\x2009" + (" " + String.Format("{0:0.0}",((double)arrWordCount[i].words /(double)arrWordCount[i].lines))).PadLeft(12, '_');

                break;
        }

        // display our word count message box
        MessageBox.Show (
            strMessage.Trim(),
            "Clipboard Word Count".PadRight(100, '\x2002'),
            MessageBoxButtons.OK,
            MessageBoxIcon.None,
            MessageBoxDefaultButton.Button1,
            MessageBoxOptions.RightAlign
            );

        // we don't want to alter the original text on the clipboard, so return the same text as we received
        return text;
    }

}

public class objWordCount
{
    public string title;
    public int pages;
    public int words;
    public int chars;
    public int charsNoSpaces;
    public int charsNoBreaks;
    public int charsNoSpacesBreaks;
    public int lines;

    public objWordCount(string strTitle, string strInput)
    {
        title               = strTitle;
        pages               = new Regex(@"\f", RegexOptions.Multiline).Matches(strInput).Count + 1;  // based on form feed characters, not very common these days
        words               = new Regex(@"\b\w+\b", RegexOptions.Multiline).Matches(strInput).Count;
        chars               = strInput.Length;
        charsNoSpaces       = Regex.Replace(strInput, @"[ \t\v]", string.Empty).Length;
        charsNoBreaks       = Regex.Replace(strInput, @"[\f\r\n]", string.Empty).Length;
        charsNoSpacesBreaks = Regex.Replace(strInput, @"[\s]", string.Empty).Length;
        lines               = new Regex(@"[\n\r]{1,2}", RegexOptions.Multiline).Matches(strInput).Count + 1;
    }
}