Solved

copy to clipboard

Posted on 2001-06-25
12
164 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now