如何在X中将文本复制到剪贴板

时间:2022-11-09 10:44:15

I was writing a pretty small command line program in Linux and I was thinking that it would be much nicer if it had the ability to copy text directly to the clipboard(not the middle click selection). I looked into it but I couldn't really find a source explaining how to do that with just X11 (without Qt or something). Could someone explain to me how this is done?

我正在Linux中编写一个非常小的命令行程序,我认为如果它能够将文本直接复制到剪贴板(而不是中间点击选择)会更好。我调查了它,但我真的找不到一个来源解释如何用X11(没有Qt或其他东西)来做到这一点。有人可以向我解释这是怎么做到的吗?

1 个解决方案

#1


1  

Use this:

void CMyListView::OnEditCopy()
{
   if ( !OpenClipboard() )
   {
      AfxMessageBox( _T("Cannot open the Clipboard") );
      return;
   }
   // Remove the current Clipboard contents
   if( !EmptyClipboard() )
   {
      AfxMessageBox( _T("Cannot empty the Clipboard") );
      return;
   }
   // Get the currently selected data
   HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, 64);
   strcpy_s((char*)hGlob, 64, "Current selection\r\n");
   // For the appropriate data formats...
   if ( ::SetClipboardData( CF_TEXT, hGlob ) == NULL )
   {
      CString msg;
      msg.Format(_T("Unable to set Clipboard data, error: %d"), GetLastError());
      AfxMessageBox( msg );
      CloseClipboard();
      GlobalFree(hGlob);
      return;
   }
   CloseClipboard();
}

#1


1  

Use this:

void CMyListView::OnEditCopy()
{
   if ( !OpenClipboard() )
   {
      AfxMessageBox( _T("Cannot open the Clipboard") );
      return;
   }
   // Remove the current Clipboard contents
   if( !EmptyClipboard() )
   {
      AfxMessageBox( _T("Cannot empty the Clipboard") );
      return;
   }
   // Get the currently selected data
   HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, 64);
   strcpy_s((char*)hGlob, 64, "Current selection\r\n");
   // For the appropriate data formats...
   if ( ::SetClipboardData( CF_TEXT, hGlob ) == NULL )
   {
      CString msg;
      msg.Format(_T("Unable to set Clipboard data, error: %d"), GetLastError());
      AfxMessageBox( msg );
      CloseClipboard();
      GlobalFree(hGlob);
      return;
   }
   CloseClipboard();
}