イージークリップ のコード

イージークリップのコードは、次の通りです。読んでもらって、安心して使ってもらえれば、と思います。また、何かの参考にでもなれば幸せです。

Form1.vb と Form2.vb とに別れます。

まず、Form1.vb です。

Imports System.ComponentModel

Public Class Form1
    Const CHECKLINES = 18    '同じものがないか、チェックする行数,1ページ分
    Const LISTLINES = 180    '最大行数(10ページ+1)
    Const CrLfReplaceWords = "<aabbccaabbccaabbcc>" '改行を維持するために、これで保存、読み込みのときに戻す
    Const TEXT_FILE_NAME = "listtext.txt"
    Const WINDOWPOS_FILE_NAME = "winpos.txt"
    Const RIGHTMOUSEDOWN = 1

    Dim lb_index As Integer = -1
    Dim pass_timer As Integer
    Public form2isopen As Boolean

    Private Sub CutOff(ll_num As Integer)
        Dim n, c As Integer

        c = ListBox1.Items.Count
        If c > ll_num Then
            For n = c - 1 To ll_num Step -1
                ListBox1.Items.RemoveAt(n)
            Next
        End If
    End Sub

    Sub WriteList(tfname As String, lb As ListBox)
        Dim sw As New System.IO.StreamWriter(tfname, False) 'Falseは上書き、ファイルがなければ作ってくれる
        Dim n As Integer

        If lb.Items.Count = 0 Then
            Exit Sub
        End If
        For n = 0 To lb.Items.Count - 1 '
            sw.WriteLine(CrLfReplace(lb.Items(n)))
        Next
        sw.Close()
    End Sub

    Private Function CrLfReplace(str As String) As String 'CrLfReplaceWords
        CrLfReplace = str.Replace(vbCrLf, CrLfReplaceWords)
    End Function

    Private Function CrLfBackReplace(Str As String) As String 'CrLfReplaceWords
        CrLfBackReplace = Str.Replace(CrLfReplaceWords, vbCrLf)
    End Function

    Sub SetList(tfname As String, lb As ListBox)
        If (Not (System.IO.File.Exists(tfname))) Then 'ファイルがなければパス
            Exit Sub
        End If

        Dim sr As New System.IO.StreamReader(tfname)

        Do While sr.EndOfStream = False
            lb.Items.Add(CrLfBackReplace(sr.ReadLine()))
        Loop
        sr.Close()
    End Sub

    Sub WriteWinPos(wpfname As String, fm As Form)
        If (fm.Left + fm.Top) < -100 Then 'windowsで閉じられたら書かない
            Exit Sub
        End If

        Dim sw As New System.IO.StreamWriter(wpfname, False) 'Falseは上書き、ファイルがなければ作ってくれる

        sw.WriteLine(fm.Left)
        sw.WriteLine(fm.Top)
        sw.Close()
    End Sub

    Sub SetWinPos(wpfname As String, fm As Form)
        If (Not (System.IO.File.Exists(wpfname))) Then 'ファイルがなければパス
            Exit Sub
        End If

        Dim sr As New System.IO.StreamReader(wpfname)
        Dim l, t As Integer

        t = sr.ReadLine()
        l = sr.ReadLine()
        sr.Close()
        fm.Location = New Point(t, l)
    End Sub

    Sub MoveUpList(lbindex As Integer, lb As ListBox)
        Dim targetlist, upperlist As String

        If lbindex = 0 Then
            Exit Sub
        End If
        targetlist = lb.Items(lbindex).ToString()
        upperlist = lb.Items(lbindex - 1).ToString()
        lb.Items.Item(lbindex - 1) = targetlist
        lb.Items.Item(lbindex) = upperlist
        lb.SelectedIndex = lbindex - 1
    End Sub

    Sub MoveDowList(lbindex As Integer, lb As ListBox)
        Dim targetlist, lowerlist As String

        If lb.Items.Count - 1 = lbindex Then
            Exit Sub
        End If
        targetlist = lb.Items(lbindex).ToString()
        lowerlist = lb.Items(lbindex + 1).ToString()
        lb.Items.Item(lbindex) = lowerlist
        lb.Items.Item(lbindex + 1) = targetlist
        lb.SelectedIndex = lbindex + 1
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetWinPos(WINDOWPOS_FILE_NAME, Me)
        Me.TopMost = True
        Me.WindowState = FormWindowState.Minimized '起動時は、タスクバーに
        SetList(TEXT_FILE_NAME, ListBox1)
    End Sub

    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        WriteList(TEXT_FILE_NAME, ListBox1)
        WriteWinPos(WINDOWPOS_FILE_NAME, Me)
        If Form2.Visible = True Then
            Form2.Close()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim clp As String, m As Integer
        Static prev_clp As String

        If pass_timer = RIGHTMOUSEDOWN Then
            Exit Sub
        End If
        clp = Clipboard.GetText()
        If prev_clp = clp Then Exit Sub
        m = CHECKLINES
        If Len(clp) > 0 Then
            If ListBox1.Items.Count = 0 Then
                ListBox1.Items.Add(clp)
                GoTo exittimer
            ElseIf ListBox1.Items.Count < CHECKLINES Then
                m = ListBox1.Items.Count
            End If
            For n = 0 To m - 1 '同じものがないか、m(CHECKLINES)個チェック
                If clp = ListBox1.Items(n) Then
                    ListBox1.SelectedIndex = n
                    GoTo exittimer 'あればそのまま
                End If
            Next
            ListBox1.Items.Insert(0, clp)   '同じものがないと、先頭に挿入する
            ListBox1.SelectedIndex = 0      '入りましたよ、と選択する
        End If
        CutOff(LISTLINES)   '最大行数を超えれば、その分を削除
exittimer:
        prev_clp = clp
        Exit Sub
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "最前面化" Then
            Me.TopMost = True
            Button1.Text = "通常化"
        Else
            Me.TopMost = False
            Button1.Text = "最前面化"
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub ListBox1Click()
        Dim p_lbcliptext As String

        p_lbcliptext = ListBox1.SelectedItem
        Clipboard.SetText(p_lbcliptext)
    End Sub
    Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
        If e.Button = MouseButtons.Right Then
            pass_timer = RIGHTMOUSEDOWN
            If ListBox1.IndexFromPoint(e.Location) <> ListBox.NoMatches Then
                lb_index = ListBox1.IndexFromPoint(e.Location) '削除ToolStripMenuItem_Click用
                ListBox1.SelectedIndex = lb_index
            End If
        ElseIf e.Button = MouseButtons.Left Then
            ListBox1Click()
        End If
    End Sub

    Private Sub 削除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 削除ToolStripMenuItem.Click
        If lb_index < 0 Then 'ListBox1_MouseDown を経由しない場合
            lb_index = ListBox1.SelectedIndex
            If lb_index < 0 Then Exit Sub '無選択でdeleteキーが押された場合
        End If
        Clipboard.Clear() '削除するためにクリックしたものが上に追加されないように
        ListBox1.Items.RemoveAt(lb_index)
        lb_index = -1 '初期化
        pass_timer = 0
    End Sub

    Private Sub 終了ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 終了ToolStripMenuItem.Click
        pass_timer = 0
        Me.Close()
    End Sub

    Private Sub クリアToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles クリアToolStripMenuItem.Click
        If MsgBox("本当に全部を削除しますか?", vbOKCancel) = vbCancel Then
            pass_timer = 0
            Exit Sub
        End If
        ListBox1.Items.Clear()
        Clipboard.Clear()
        pass_timer = 0
    End Sub

    Private Sub お決まりを開くToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles お決まりを開くToolStripMenuItem.Click
        pass_timer = 0
        Form2.Show()
        form2isopen = True
    End Sub

    Private Sub お決まりへ追加ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles お決まりへ追加ToolStripMenuItem.Click
        pass_timer = 0
        Form2.Show()
        form2isopen = True
        Form2.ListBox1.Items.Insert(0, ListBox1.SelectedItem.ToString)
    End Sub

    Private Sub キャンセルToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles キャンセルToolStripMenuItem.Click
        pass_timer = 0
    End Sub
End Class

次は、Form2.vb です。

Imports System.ComponentModel

Public Class Form2
    Const TEXT_FILE_NAME = "okimaritext.txt"
    Const WINDOWPOS_FILE_NAME = "okimariwinpos.txt"

    Dim lb_index As Integer = -1

    Private Sub ReduceList() '同じものを削除
        Dim lcount, n, m As Integer
        Dim ltext As String

        lcount = ListBox1.Items.Count
        For n = lcount - 1 To 1 Step -1
            ltext = ListBox1.Items(n).ToString
            For m = n - 1 To 0 Step -1
                If ltext = ListBox1.Items(m).ToString Then
                    ListBox1.Items.RemoveAt(n)
                    Exit For
                End If
            Next
        Next
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
        Form1.SetWinPos(WINDOWPOS_FILE_NAME, Me)
        Form1.SetList(TEXT_FILE_NAME, ListBox1)
    End Sub

    Private Sub Form2_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        ReduceList()
        Form1.WriteWinPos(WINDOWPOS_FILE_NAME, Me)
        Form1.WriteList(TEXT_FILE_NAME, ListBox1)
    End Sub

    Private Sub Form2_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        Form1.form2isopen = False
    End Sub

    Private Sub ListBox1Click()
        Dim p_lbcliptext As String

        p_lbcliptext = ListBox1.SelectedItem
        Clipboard.SetText(p_lbcliptext)
    End Sub
    Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
        If e.Button = MouseButtons.Right Then
            If ListBox1.IndexFromPoint(e.Location) <> ListBox.NoMatches Then
                'lb_index = ListBox1.IndexFromPoint(e.Location) '削除ToolStripMenuItem_Click用
                ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.Location)
            End If
        ElseIf e.Button = MouseButtons.Left Then
            ListBox1Click()
        End If
    End Sub

    Private Sub 削除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 削除ToolStripMenuItem.Click
        If ListBox1.SelectedItem = Nothing Then Exit Sub '無選択でdeleteキーが押された場合
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
    End Sub

    Private Sub クリアToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles クリアToolStripMenuItem.Click
        If MsgBox("本当に全部を削除しますか?", vbOKCancel) = vbCancel Then
            Exit Sub
        End If
        ListBox1.Items.Clear()
        Clipboard.Clear()
    End Sub

    Private Sub 閉じるToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 閉じるToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub 上に移動するToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 上に移動するToolStripMenuItem.Click
        If ListBox1.SelectedItem = Nothing Then
            Exit Sub
        End If
        Form1.MoveUpList(ListBox1.SelectedIndex, ListBox1)
    End Sub

    Private Sub 下に移動するToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 下に移動するToolStripMenuItem.Click
        If ListBox1.SelectedItem = Nothing Then
            Exit Sub
        End If
        Form1.MoveDowList(ListBox1.SelectedIndex, ListBox1)
    End Sub

End Class

以上です。