Memo2 α版

Memo2 α版を作成します。

前田稔の超初心者のプログラム入門

プログラムの説明

  1. TreeView の背景色を設定します。
    Title(TextBox) のフォントサイズと背景色を設定します。
    Title が隠れないように this.ClientSize の直後に置いて下さい。
    設定の効果は、ページ先頭の画像を参照して下さい。
            // treeView1
            this.treeView1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.treeView1.ContextMenuStrip = this.contextMenuStrip2;
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.LabelEdit = true;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(257, 486);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterLabelEdit +=
                new System.Windows.Forms.NodeLabelEditEventHandler(this.treeView1_AfterLabelEdit);
            this.treeView1.AfterSelect +=
                new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
    
            // Title
            this.Title.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
            this.Title.Font = new System.Drawing.Font("MS UI Gothic", 11F,
                System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.Title.Location = new System.Drawing.Point(257, 25);
            this.Title.Name = "Title";
            this.Title.Size = new System.Drawing.Size(935, 29);
            this.Title.TabIndex = 3;
    
            // MyForm
            this.ClientSize = new System.Drawing.Size(1178, 544);
            this.Controls.Add(this.Title);
    
  2. RitchTextBox で TAB キーを使います。
    CSForm.cs のデザイン画面から RitchTextBox を選択してして下さい。
    プロパティから Accepts Tab を True に設定して下さい。
  3. Memo2 に専用のアイコンを設定します。
    アイコン画像を作成してプロジェクトのフォルダーに格納します。
    ソリューションブラウザから Properties を右クリックして[開く(O)]を実行します。
    プロパティウインドウの[アイコン(C)]は(既定のアイコン)になっています。
    (既定のアイコン)の右端をクリックしてアイコンを選択します。
    コンパイルすると、作成された実行プログラム ...\bin\Debug\CSForm.exe にアイコンが設定されています。
    この段階では、デザイン表示や実行プログラムのアイコンは既定値のままです。

  4. プロジェクトのデザイン画面から MyForm を選択してプロパティを表示します。
    プロパティをスクロールして Icon の位置に設定してプロパティから右端をクリックしてアイコンを選択します。
    アイコンの設定は 専用 icon を設定 を参照して下さい。

Title(Node) 指定

  1. Memo2 ファイルに Title(Node) を付け加えてダブルクリックで起動します。
    ファイルが大きくなると、どこに情報が記録されているかわからなくなります。
    そのような時に Memo のリンクから Title を指定して開けると便利です。
    そこで「/」以降に Node のタイトルを「親/子/孫/・・・」と順に指定して直接 Node を開きます。
    "c:\tmp\test2.mem" がファイル名で "/Memo/data" が Node のタイトルです。
    ユニークな Node の時は、孫だけ指定することも出来ます。
    ダブルクリックで起動すると、ファイルをオープン後に指定した Node が開きます。
    "c:\tmp\test2.mem/Memo/data"
    "C:\DATA\Memo\C#.mem/C#/99App/06Memo2"
    
  2. ダブルクリックでパラメータを持った Memo ファイルを起動します。
    m_sou にファイル名を取得して、既定のプログラムから起動します。
    このとき "http:" で始まる URL やパラメータを持つ Memo ファイルは、ファイル名が見つからないので起動出来ません。
    "http:" を調べて既定のブラウザから起動します。
    次に m_sou の拡張子(.mem)を調べて、Memo ファイルであることを確かめます。
    Process.Start(MEMO_Path, m_sou) で m_sou をパラメータとして Memo2 を起動します。
    MEMO_Path は Memo2 の実行プログラムが格納されているパスです。
        string      MEMO_Path = @"c:\bin\memo2.exe";
    
        private void richTextBox1_DoubleClick(object sender, MouseEventArgs e)
        {
            int pos, rt, i;
            pos = richTextBox1.SelectionStart;
            for (; pos > 0; pos--)
            {
                for (i = 0; i < m_sep.Length && richTextBox1.Text[pos] != m_sep[i]; i++) ;
                if (i < m_sep.Length) break;
            }
            if (richTextBox1.Text[pos] == '"' || richTextBox1.Text[pos] == '\'')
            {
                for (rt = pos + 2; rt < richTextBox1.Text.Length; rt++)
                { if (richTextBox1.Text[pos] == richTextBox1.Text[rt])  break; }
            }
            else
            {
                for (rt = pos + 1; rt < richTextBox1.Text.Length; rt++)
                {
                    for (i = 0; i < m_sep.Length && richTextBox1.Text[rt] != m_sep[i]; i++) ;
                    if (i < m_sep.Length) break;
                }
            }
            if (pos >= 0) pos++;
            m_sou = richTextBox1.Text.Substring(pos, rt - pos);
            this.Text = "[" + m_sou + "]";
            Clipboard.SetDataObject(m_sou, true);
    
            if (File.Exists(m_sou))
            {   System.Diagnostics.Process.Start(m_sou);
                return;
            }
            if (String.Compare(m_sou.Substring(0, 5), "http:", true) == 0)
            {   System.Diagnostics.Process.Start(m_sou);
                return;
            }
            
            // m_sou の拡張子を調べる  "c:\tmp\test2.mem/Memo/data"
            pos = m_sou.IndexOf('.');
            if (pos<0)  return;
            if (String.Compare(m_sou.Substring(pos, 4), ".mem", true) != 0) return;
            System.Diagnostics.Process.Start(MEMO_Path, m_sou);
        }
    
  3. パラメータを持ったファイルを受け付ける MyForm では、m_sou に設定して MyForm_Load() を呼び出します。
        public MyForm()
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(this.FormResize);
            this.Load += new System.EventHandler(MyForm_Load);
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
        }
        public MyForm(string str)
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(this.FormResize);
            this.Load += new System.EventHandler(MyForm_Load);
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
            m_sou = str;
        }
    
  4. MyForm_Load() 関数では m_sou に '/' が含まれるか調べて、含まれない時はそのまま起動します。
    '/' が含まれるときは '/' で切り分けて str[] に格納します。
    str2[] は str[] の前に★を付け加えた文字列です。
    タイトル先頭の★はタイトルが目立つようにした印で、付加したり削除したりするので、どちらでもマッチするようにします。
    str[0] がパラメータを外したファイル名です。
    パラメータを順に Node のタイトル(t_ttl) で検索して、該当する Node を開きます。
        // アプリケーションの初期化
        private void MyForm_Load(object sender, System.EventArgs e)
        {
            Width = 1200;
            Height = 600;
            if (File.Exists(XML_File))  //ファイルの有無をチェック
            {
                System.Xml.Serialization.XmlSerializer serializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(XmlClass));
                System.IO.FileStream fs =
                    new System.IO.FileStream(XML_File, System.IO.FileMode.Open);
                XmlClass obj = (XmlClass)serializer.Deserialize(fs);
                fs.Close();
                int point = (int)obj.point;
                m_font = new Font(obj.name, point);
                m_file = obj.file;
            }
            else m_font = new Font("MS 明朝", 12);
            richTextBox1.Font = m_font;
            richTextBox1.LanguageOption = RichTextBoxLanguageOptions.UIFonts;
            treeView1.Font = m_font;
            button1.Hide();
            textBox1.Hide();
            textBox2.Hide();
    
            if (m_sou=="Source")    m_sou = m_file;
            if (m_sou.IndexOf('/')<0)
            {   ReadFile(m_sou);
                m_file = m_sou;
                m_Idx = 0;
                treeView1.TopNode.Expand();
                richTextBox1.Text = (string)t_txt[0];
                this.Text = m_file;
                return;
            }
    
            // m_sou を解析
            string[] str;
            string[] str2;
            int      i,pt;
            str = m_sou.Split(new char[] {'/', '-'});
            ReadFile(str[0]);
            m_file = str[0];
            str2 = (string[])str.Clone();
            for (i = 1; i < str.GetLength(0); i++) str2[i] = "★" + str2[i];   
            m_Idx = 0;
            pt = 1;
            for(i=0; i<t_ttl.Count; i++)
            {
                if ((string)t_ttl[i]==str[pt] || (string)t_ttl[i]==str2[pt])
                {
                    m_Idx = i;
                    pt++;
                    if (pt>=str.GetLength(0))  break;
                }
            }
            treeView1.SelectedNode = (TreeNode)t_node[m_Idx];
            richTextBox1.Text = (string)t_txt[m_Idx];
            this.Text = m_sou;
        }
    
  5. このままでは、初めて Memo2 アプリを起動すると正常に動作しません。
    理由は c:\tmp\ に memo.xml を書き出すことや、最初に起動される Test.mem を c:\tmp\ に置いているからです。
    ED_Path や MEMO_Path の定義も関係するのですが、これらは使わなければ取り合えず問題ないでしょう。 (^_^;)
    "C:\\tmp" のフォルダーを調べて、作成されていないときはメッセージを表示します。
        // アプリケーションの初期化
        private void MyForm_Load(object sender, System.EventArgs e)
        {
            Width = 1200;
            Height = 600;
    
            if (!System.IO.Directory.Exists("C:\\tmp"))
            {   MessageBox.Show("C:\\tmp\\ のフォルダーを作成して下さい");
            }
            if (File.Exists(XML_File))  //ファイルの有無をチェック
            {
                ・・・
            }
    
  6. ReadFile() 関数では、渡された file が見つからないときは「新規データ」を作成します。
        //  memo2.mem を切り分けて ArrayList に格納する
        private void ReadFile(string file)
        {
            StreamReader reader;
            string str;
            int    pt, wk;
    
            if (!File.Exists(file))     //ファイルの有無をチェック
            {
                MessageBox.Show("ファイルが見つかりません(新規データを作成)", file);
                CreateData();
                return;
            }
                ・・・
    
    
  7. CreateData() 関数の作成に伴って NewFile() 関数も修正します。
        private void NewFile(object sender, EventArgs e)
        {
            DialogResult rc;
            CheckText();
            if (m_UP)
            {
                rc = MessageBox.Show("保存しないで実行?", "選択", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rc == DialogResult.No)  return;
            }
            CreateData();
        }
    
  8. 新規 Memo File を作成する CreateData() 関数です。
    これで初めての人でも Memo2 を使うことが出来るのでは無いでしょうか?
        private void CreateData()
        {
            string str = DateTime.Now.ToString();
            str = str.Substring(0,10) + ":" + str.Substring(0,10);
            t_lev.Clear();
            t_ttl.Clear();
            t_ymd.Clear();
            t_txt.Clear();
            t_lev.Add(2);
            t_ttl.Add("新規ファイル");
            t_ymd.Add(str);
            t_txt.Add("☆新規データ\nTest Data Type Input");
            t_lev.Add(2);
            t_ttl.Add("ごみ箱");
            t_ymd.Add(str);
            t_txt.Add("ごみ箱\n");
            t_node.Clear();
            treeView1.Nodes.Clear();
            m_Idx = 0;
            Set_TVFunc(treeView1, 2);
            m_Idx = 0;
            richTextBox1.Text = (string)t_txt[0];
            treeView1.ExpandAll();
            m_UP = false;
            richTextBox1.Modified = false;
            m_file = "c:\\tmp\\New.mem";
            this.Text = m_file;
        }
    

[Next Chapter ↓] Memo2 β版
[Previous Chapter ↑] User Menu

超初心者のプログラム入門(C# Frame Work)