摘要:[C#.NET][VB.NET][VB6] 監視剪貼簿內容
.Net Framwork中可以利用System.Windows.Forms命名空間的Clipboard 類別來達到像Office的剪貼簿監視,以下是小小的範例。
C#.NET 2005:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if(Clipboard.ContainsText()) richTextBox1.Text=Clipboard.GetText();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if(Clipboard.ContainsText()) richTextBox1.Text=Clipboard.GetText();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
}
}
範例下載:C#監視剪貼簿.rar
VB.NET 2005:
Public Class Form1
Dim i
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
i = 0
Clipboard.Clear()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim strTem As String
If Clipboard.ContainsText Then '判斷是剪貼簿否有內容
i = i + 1
strTem = Clipboard.GetText() '貼到變數
If i = 1 Then
RichTextBox1.Text = "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
Else
RichTextBox1.Text = RichTextBox1.Text & vbCrLf & "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
End If
Clipboard.Clear()
End If
End Sub
End Class
Dim i
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
i = 0
Clipboard.Clear()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim strTem As String
If Clipboard.ContainsText Then '判斷是剪貼簿否有內容
i = i + 1
strTem = Clipboard.GetText() '貼到變數
If i = 1 Then
RichTextBox1.Text = "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
Else
RichTextBox1.Text = RichTextBox1.Text & vbCrLf & "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
End If
Clipboard.Clear()
End If
End Sub
End Class
執行畫面結果如下
範例下載:VB.NET監視剪貼簿.rar
VB6:當然也不能少了老牌的它
Option Explicit
Dim i
Private Sub Form_Activate()
i = 0
Clipboard.Clear
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim strTem As String
If Clipboard.GetFormat(vbCFText) Then
i = i + 1
strTem = Clipboard.GetText(vbCFText)
If i = 1 Then
Text1.Text = "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
Else
Text1.Text = Text1.Text & vbCrLf & "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
End If
Clipboard.Clear
End If
End Sub
Dim i
Private Sub Form_Activate()
i = 0
Clipboard.Clear
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim strTem As String
If Clipboard.GetFormat(vbCFText) Then
i = i + 1
strTem = Clipboard.GetText(vbCFText)
If i = 1 Then
Text1.Text = "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
Else
Text1.Text = Text1.Text & vbCrLf & "第" & i & "次複製" & vbCrLf & strTem & vbCrLf
End If
Clipboard.Clear
End If
End Sub
執行結果畫面如下
範例下載:VB6監視剪貼簿.rar
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET