Click to See Complete Forum and Search --> : Copy Results to Clipboard
shadowx360
October 26th, 2008, 03:39 PM
Well, here is my code to make a pseudo-random number calculator of my own, and I want to copy the resulting number to the clipboard. Here is my code:
#include <windows.h>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <stdio.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main()
{
double rn,x; //declare variables
srand(time(0)); //seed time
x=rand(); // simplify by using x instead of rand()
rn=-(x*tan(cos((x*x*x)-x))+1/(x*x)+1/x)+x; //my equation
cout << "R#G:" << setprecision (15)<<rn; //let the user see the #
system("pause"); //pause the screen
}
So, basically, I would like to send the value of "rn" over to the clipboard buffer. Any suggestions?
golanshahar
October 26th, 2008, 04:01 PM
Using the Clipboard (http://msdn.microsoft.com/en-us/library/ms649016(VS.85).aspx)
Cheers
shadowx360
October 26th, 2008, 04:19 PM
Using the Clipboard (http://msdn.microsoft.com/en-us/library/ms649016(VS.85).aspx)
Cheers
Woah, that's like 50 lines just to copy one number over to the clipboard. Scary.
0xC0000005
October 26th, 2008, 05:25 PM
Programming the Windows API is never simple unless you use a very high-level language. Here's a much shorter example of copying a string to the clipboard. It should be easy enough to modify for your purposes, or just copy 'rn' to a string and call this function.
void ToClipboard(LPCTSTR text)
{
// Open and empty the clipboard
VERIFY(::OpenClipboard(0));
VERIFY(::EmptyClipboard());
// Calculate number of bytes to be written (includes '\0' terminator)
const int nBytes = ( _tcslen(text) + 1 ) * sizeof(TCHAR);
// Allocate global memory
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
// Get a pointer to the memory
LPVOID pMem = ::GlobalLock(hMem);
// Copy string to memory
::CopyMemory(pMem, text, nBytes);
::GlobalUnlock(hMem);
// Copy memory to clipboard
#if defined(_UNICODE)
::SetClipboardData(CF_UNICODETEXT, hMem);
#else
::SetClipboardData(CF_TEXT, hMem);
#endif
// Close the clipboard
VERIFY(::CloseClipboard());
}
shadowx360
October 26th, 2008, 06:41 PM
Programming the Windows API is never simple unless you use a very high-level language. Here's a much shorter example of copying a string to the clipboard. It should be easy enough to modify for your purposes, or just copy 'rn' to a string and call this function.
void ToClipboard(LPCTSTR text)
{
// Open and empty the clipboard
VERIFY(::OpenClipboard(0));
VERIFY(::EmptyClipboard());
// Calculate number of bytes to be written (includes '\0' terminator)
const int nBytes = ( _tcslen(text) + 1 ) * sizeof(TCHAR);
// Allocate global memory
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
// Get a pointer to the memory
LPVOID pMem = ::GlobalLock(hMem);
// Copy string to memory
::CopyMemory(pMem, text, nBytes);
::GlobalUnlock(hMem);
// Copy memory to clipboard
#if defined(_UNICODE)
::SetClipboardData(CF_UNICODETEXT, hMem);
#else
::SetClipboardData(CF_TEXT, hMem);
#endif
// Close the clipboard
VERIFY(::CloseClipboard());
}
Very good answer. I love it. Only problem: I am not good with type conversions, and I can't copy rn (double) to LPCTSTR. Otherwise, this works perfectly.
0xC0000005
October 27th, 2008, 08:03 AM
Since you are using std c++ you may want to copy the double to ostringstream and then call ToClipboard() using the string object contained in ostringstream.
Possibly a more simple solution, if you only want to copy a double, is to modify the function to take a double and precision instead of a string.
void ToClipboard(double x, int precision)
{
// Format the double in a text buffer
TCHAR text[128];
_stprintf(text, _T("%.*f"), precision, x);
// Open and empty the clipboard
VERIFY(::OpenClipboard(0));
VERIFY(::EmptyClipboard());
// Calculate number of bytes to be written (includes '\0' terminator)
const int nBytes = ( _tcslen(text) + 1 ) * sizeof(TCHAR);
// Allocate global memory
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
// Get a pointer to the memory
LPVOID pMem = ::GlobalLock(hMem);
// Copy string to memory
::CopyMemory(pMem, text, nBytes);
::GlobalUnlock(hMem);
// Copy memory to clipboard
#if defined(_UNICODE)
::SetClipboardData(CF_UNICODETEXT, hMem);
#else
::SetClipboardData(CF_TEXT, hMem);
#endif
// Close the clipboard
VERIFY(::CloseClipboard());
}
shadowx360
October 27th, 2008, 06:21 PM
Since you are using std c++ you may want to copy the double to ostringstream and then call ToClipboard() using the string object contained in ostringstream.
Possibly a more simple solution, if you only want to copy a double, is to modify the function to take a double and precision instead of a string.
void ToClipboard(double x, int precision)
{
// Format the double in a text buffer
TCHAR text[128];
_stprintf(text, _T("%.*f"), precision, x);
// Open and empty the clipboard
VERIFY(::OpenClipboard(0));
VERIFY(::EmptyClipboard());
// Calculate number of bytes to be written (includes '\0' terminator)
const int nBytes = ( _tcslen(text) + 1 ) * sizeof(TCHAR);
// Allocate global memory
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
// Get a pointer to the memory
LPVOID pMem = ::GlobalLock(hMem);
// Copy string to memory
::CopyMemory(pMem, text, nBytes);
::GlobalUnlock(hMem);
// Copy memory to clipboard
#if defined(_UNICODE)
::SetClipboardData(CF_UNICODETEXT, hMem);
#else
::SetClipboardData(CF_TEXT, hMem);
#endif
// Close the clipboard
VERIFY(::CloseClipboard());
}
Thanks for all your help, ima rate this answer as high as it goes, same for the first one.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.