c++控制台程序 如何复制文本到剪贴板

2025-03-13 22:34:16
推荐回答(3个)
回答1:

你右键,选标记,然后,回车键,就可以粘贴到记事本里了。


你这意思啊~

下面的代码示例使用 Clipboard 方法将数据置于系统剪贴板中,并从中检索这些数据。这段代码假定 button1、button2、textBox1 和 textBox2 已经创建,并已置于窗体上。

button1_Click 方法调用 SetDataObject,从文本框中提取选定的文本,然后将其置于系统剪贴板中。

button2_Click 方法调用 GetDataObject,从系统剪贴板中检索数据。这段代码使用 IDataObject 和 DataFormats 提取已返回的数据,并在 textBox2 中显示该数据。

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Takes the selected text from a text box and puts it on the clipboard.
      if ( !textBox1->SelectedText->Equals( "" ) )
      {
         Clipboard::SetDataObject( textBox1->SelectedText );
      }
      else
      {
         textBox2->Text = "No text selected in textBox1";
      }
   }
   void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Declares an IDataObject to hold the data returned from the clipboard.
      // Retrieves the data from the clipboard.
      IDataObject^ iData = Clipboard::GetDataObject();
      
      // Determines whether the data is in a format you can use.
      if ( iData->GetDataPresent( DataFormats::Text ) )
      {
         // Yes it is, so display it in a text box.
         textBox2->Text = (String^)(iData->GetData( DataFormats::Text ));
      }
      else
      {
         // No it is not.
         textBox2->Text = "Could not retrieve data off the clipboard.";
      }
   }

参考:https://msdn.microsoft.com/zh-cn/library/system.windows.forms.clipboard(v=vs.80).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

回答2:

人家问C++的,还特别强调MFC都不要……你确答了个C#的……

回答3:

你右键,选标记,然后,回车键,就可以粘贴到记事本里了。
你这意思啊~
下面的代码示例使用 Clipboard 方法将数据置于系统剪贴板中,并从中检索这些数据。这段代码假定 button1、button2、textBox1 和 textBox2 已经创建,并已置于窗体上。
button1_Click 方法调用 SetDataObject,从文本框中提取选定的文本,然后将其置于系统剪贴板中。
button2_Click 方法调用 GetDataObject,从系统剪贴板中检索数据。这段代码使用 IDataObject 和 DataFormats 提取已返回的数据,并在 textBox2 中显示该数据。
12345678910111213141516171819202122232425262728293031private: void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Takes the selected text from a text box and puts it on the clipboard. if ( !textBox1->SelectedText->Equals( "" ) ) { Clipboard::SetDataObject( textBox1->SelectedText ); } else { textBox2->Text = "No text selected in textBox1"; } } void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Declares an IDataObject to hold the data returned from the clipboard. // Retrieves the data from the clipboard. IDataObject^ iData = Clipboard::GetDataObject(); // Determines whether the data is in a format you can use. if ( iData->GetDataPresent( DataFormats::Text ) ) { // Yes it is, so display it in a text box. textBox2->Text = (String^)(iData->GetData( DataFormats::Text )); } else { // No it is not. textBox2->Text = "Could not retrieve data off the clipboard."; } }
参考:https://msdn.microsoft.com/zh-cn/library/system.windows.forms.clipboard(v=vs.80).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1