• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
只是向上走
采菊东篱下,悠然见南山。
博客园    首页    新随笔    联系   管理    订阅  订阅
怎样在IDE的MTAThread中使用剪贴板功能

 

#region 剪切/复制,粘贴
        
/// <summary>
        
/// 写文件到剪贴板
        
/// </summary>
        
/// <param name="cut">如果是剪切则为True,如果为复制False</param>        
        void CopyToClipboard(bool cut)
        {
            List
<ZSFile> files = this.blvDirAndFileList.SelectedFiles;           
            
if (files != null)
            {
                clipboardData 
= new DataObject(typeof(List<ZSFile>).ToString(), files);

                MemoryStream memo 
= new MemoryStream(4);
                
byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
                memo.Write(bytes, 
0, bytes.Length);

                clipboardData.SetData(
"Preferred DropEffect", memo);

                Thread staThread 
= new Thread(new ThreadStart(this.SetClipboard));
                staThread.SetApartmentState(ApartmentState.STA);
                staThread.Start();
                staThread.Join();
                staThread.Abort();
            }
        }
        
private IDataObject clipboardData;
        
public void SetClipboard()
        {
            
if (clipboardData != null)
            {
                Clipboard.SetDataObject(clipboardData,
true);
            }
            
        }
        
public void GetClipboardContent()
        {            
            clipboardData 
= Clipboard.GetDataObject();
        }
        
/// <summary>
        
/// 粘贴
        
/// </summary>
        private void tsmiPaste_Click(object sender, EventArgs e)
        {
            Thread sta1Thread 
= new Thread(new ThreadStart(this.GetClipboardContent));
            sta1Thread.SetApartmentState(ApartmentState.STA);
            sta1Thread.Start();
            sta1Thread.Join();
            staThread.Abort();

            
//IDataObject clipboardData = Clipboard.GetDataObject();
            if (!clipboardData.GetDataPresent(typeof(List<ZSFile>).ToString()))
                
return;

            List
<ZSFile>  files = (List<ZSFile>)clipboardData.GetData(typeof(List<ZSFile>).ToString());
            MemoryStream stream 
= (MemoryStream)clipboardData.GetData("Preferred DropEffect", true);
            
//int flag = stream.ReadByte();
            
//if (flag != 2 && flag != 5)
            
//    return;
            
//bool cut = (flag == 2);
            
//foreach (string file in files)
            
//{
            
//    string dest = homeFolder + "\\" + Path.GetFileName(file);
            
//    try
            
//    {
            
//        if (cut)
            
//            File.Move(file, dest);
            
//        else
            
//            File.Copy(file, dest, false);
            
//    }
            
//    catch (IOException ex)
            
//    {
            
//        MessageBox.Show(this, "Failed to perform the specified operation:\n\n" + ex.Message, "File operation failed", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            
//    }
            
//}

            
//RefreshView();
        }

        
/// <summary>
        
/// 复制
        
/// </summary>
        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            
            CopyToClipboard(
false);
        }

        
/// <summary>
        
/// 剪切
        
/// </summary>
        private void tsmiCut_Click(object sender, EventArgs e)
        {
            CopyToClipboard(
true);
        }
#endregion

 

 

posted on 2009-06-19 09:03  jes.shaw  阅读(424)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3