Solved

copy to clipboard

Posted on 2001-06-25
12
178 Views
Last Modified: 2013-11-20
i need to allow copying the content of a list view control (with 2 columns), the problem is that when i paste them on a notepad instead of 2 columns i get one huge line with strange fonts instead of the "new line" charachter.

here's the code:
void CPointViewBar::On3DListCtrlCopyToClipboard()
{
     CString strSource;
         
     for(int i=0; i<m_point_list_ctrl.GetItemCount(); i++){
          for(int j=0; j<2; j++)
          {
               strSource+=m_point_list_ctrl.GetItemText(i,j);
               strSource+="\t";
          }
          strSource+="\n";
     }

     ((CSpectraAnalisysGuiApp*)AfxGetApp())->CopyToClipboard(strSource);
}

void CSpectraAnalisysGuiApp::CopyToClipboard(CString strSource)
{
     if(OpenClipboard(NULL))
     {
          HGLOBAL clipbuffer;
          char * buffer;
          EmptyClipboard();
          clipbuffer = GlobalAlloc(GMEM_DDESHARE, strSource.GetLength() + 1);
          buffer = (char*)GlobalLock(clipbuffer);
          strcpy(buffer, LPCSTR(strSource));
          GlobalUnlock(clipbuffer);
          SetClipboardData(CF_TEXT, clipbuffer);
          CloseClipboard();
     }
}

what changes do i need to make in order to be able to paste in 2 columns...?
0
Comment
Question by:sedgwick
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 42

Author Comment

by:sedgwick
ID: 6226784
and why does it take so much time to concatenate the strings? (there are 45000 items in the list control)
0
 
LVL 22

Expert Comment

by:ambience
ID: 6226816
strSource+="\n";
try changing that to strSource+="\r\n";

you have 45000 item with two columns per item you are sending 90000 LVM_GETITEMTEXT to the control ,with the control copying the values back and then you concatenating them , and do you think this shuold not take some time ?


   
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6226931
I'm sure you know what you're doing :) but why do you need 45,000 items in the listview?  Perhaps it's just for testing (in which case the timing won't be as much of a problem in production), but if not, the user won't be sticking around long enough to find out what's at the bottom.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:Pacman
ID: 6227060
it takes so long because the string buffer must be reallocated from time to time. Therefore a new, bigger buffer is allocated and the complete string is copied to it. The old buffer is deleted. This takes time.

Solution: calculate or estimate the maximum size and tell it tou your CString-object.

regards Pacman
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6227077
>> what changes do i need to make in order to be able to paste in 2 columns...?

I don't understand your question. Are the columns the problem or the lines ?
You said that the newline makes trouble.
That's because a line usually ends with carriage return PLUS linefeed. You copied only a linefeed to clipboard.

Change your code to this and it should work fine:

strSource+="\r\n";
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6227209
there r 2 columns because they r represent coordinates.
each item is from type float (4 digits after floating point).

ambience & pacman: thanks for the info, i didn't know that "\r" was essential for this matter.


>>but why do you need 45,000 items in the listview?  
IainHere: i'm displaying graphs which basically is a spectrum, while each one of the items is a coordinate in the spectrum.
since the graph is editable each coordinate also displayed in the list control, in oredr to allow manual editing.


Pacman: how can i estimate the max size of the full concatenated string?
is it better (or more efficient) to work with short strings instead of one huge string?
how do perform a massive string allocation?

0
 
LVL 42

Author Comment

by:sedgwick
ID: 6227274
NOTE: the only reason that i need to copy the content of the list control to the clipboard is beacuse there is an engine which perform calculations on this data and run in a web site, this engine know how to read 2 columns of data which represent the coordinates of the spectrum.
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6227318
>> how can i estimate the max size of the full concatenated string?

what about this:
nMaxSize = nNumberOfListBoxEntries * nMaxCharPerEntry;
?
0
 
LVL 9

Accepted Solution

by:
Pacman earned 50 total points
ID: 6227335
or do it in several passes:

1. go through listbox and sum the lengths of the entries
  (including tab and cr/lf and 0) => nWholeSize

2. alloc big buffer:
   char *pBuffer = new char[nWholeSize];

3. go through listbox and copy the strings to pBuffer

4. Copy pBuffer to the clipboard
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6228010
"NOTE: the only reason that i need to copy the content of the list control to the clipboard is beacuse
there is an engine which perform calculations on this data and run in a web site, this engine know how
to read 2 columns of data which represent the coordinates of the spectrum."

I don't fully understand the above, so forgive me if this is useless: If you're displaying the info on a graph, why not allow the user to edit the graph using a different (more user friendly?) way, such as clicking on a point and the info for that point (and maybe a few around it) being displayed in a listview / textedit?  Surely no-one is prepared to look through 45,000 points to edit the one they're after?

I think you're saying that you need to paste the results into a web page?  Would it be better to do that by parsing the information stored in the graph into the string?  Do you have any control over the web site?
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6230997
IainHere:
there's no connection between the web engine which calculate the data from my stand-alone program and the spectrum, since this program is internaly (for the "lab-rats" usage only).
what they did so far is save the results of the spectrum in a text file and copy its content to the right place in the web site for the calculation part.
the copy to clipboard is only for an easy usage.
anyhow, i really appreciate your help but i think i've done with this issue.
i realized that the results of the spectrum is nothing more than 100 peaks tops. which mean that there is no need for massive string allocation and all that stuff.
using CString concatenation is more tan enough.
thanks anyway and keep up the ggood work (and comments)
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6231003
thanks Pacman, u deserve your pts
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 92
Named range not carried over 10 63
string initialization in java 11 113
sum67 challenge 35 97
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question