//事件
this.label1.DoubleClick += new System.EventHandler(this.label_DoubleClick);
#region 双击label复制文本并在三秒后自动关闭
private void StartKiller()
{
Timer timer = new Timer();
timer.Interval = 3000; //3秒启动
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止Timer
((Timer)sender).Stop();
}
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
private void KillMessageBox()
{
//按照MessageBox的标题,找到MessageBox的窗口
IntPtr ptr = FindWindow(null, "提示");
if (ptr != IntPtr.Zero)
{
//找到则关闭MessageBox窗口
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
private void label_DoubleClick(object sender, EventArgs e)
{
System.Windows.Forms.Label lab = (System.Windows.Forms.Label)sender;
string a = lab.Text;
Clipboard.SetText(a);
StartKiller();
MessageBox.Show("'" + a + "'" + "已复制到剪贴板!", "提示", MessageBoxButtons.OK);
}
#endregion