博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

C#复制、粘贴文本信息到剪贴板

Posted on 2011-11-17 09:52  codingsilence  阅读(265)  评论(0编辑  收藏  举报

 

复制:
y m4n mA-T/mL(c"q3Sq0private void button1_Click(object sender, System.EventArgs e) {
2\9eu6\6SM0  // Takes the selected text from a text box and puts it on the clipboard.
u:ixpoxYj1@0  if(textBox1.SelectedText != ”")ITPUB个人空间;sq-_\&_!K+]uV,zT
  Clipboard.SetDataObject(textBox1.SelectedText);
1hP A&Lj+D U0  }

粘贴:
E XI)udUZ0private void button2_Click(object sender, System.EventArgs e) {ITPUB个人空间;W|5G1ENs'S
  // Declares an IDataObject to hold the data returned from the clipboard.ITPUB个人空间 LQX j;SX
  // Retrieves the data from the clipboard.ITPUB个人空间 PY:k i0K)C8[6KF7Q
  IDataObject iData = Clipboard.GetDataObject();
.OGWc#p0
$w?&o3OI#deY0  // Determines whether the data is in a format you can use.
+t AE1B Q,T?kP0  if(iData.GetDataPresent(DataFormats.Text)) {
9D/N S"W*Tu%Ct0  // Yes it is, so display it in a text box.
8te icY p$A0  textBox2.Text = (String)iData.GetData(DataFormats.Text);
I;O?e2t5ul h-_"o0  }ITPUB个人空间/D3D.MY!d;~
}
5o?$@YT h.\0主要通过调用Clipborad的API完成。