Solved

copy to clipboard

Posted on 2001-06-25
12
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 107
sameEnds challenge 3 187
sum67 challenge 35 116
Modbus - whats the maximum I can store in one register? 4 82
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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