copyImageToClipboard

 
Hello 

I make an image by WindowScreenShot and I want to copy this image to clipboard and paste in other programs (like telegram desktop or paint)

I saw very good code for CopyTextToClipboard in this link : https://www.mql5.com/en/forum/153674#comment_5020915

I'm trying to use copyImageToClipboard function in my code but I dont now how can I use this function in mql4 and I can not find anything 

could you help me please ?
Copy string to clipboard
Copy string to clipboard
  • 2014.11.01
  • www.mql5.com
Hello, for to copy text to clipboard, I tried the solution of ricx on https://forum.mql4...
 
prg_mt4:
Hello 

I make an image by WindowScreenShot and I want to copy this image to clipboard and paste in other programs (like telegram desktop or paint)

I saw very good code for CopyTextToClipboard in this link : https://www.mql5.com/en/forum/153674#comment_5020915

I'm trying to use copyImageToClipboard function in my code but I dont now how can I use this function in mql4 and I can not find anything 

could you help me please ?

See below. Same principle as copying text to the clipboard, but slightly more complicated as a result of needing to create a temporary screenshot file and then read it back from disk.


#import "kernel32.dll"
   int GlobalAlloc(int Flags, uint Size);
   int GlobalLock(int hMem);
   int GlobalUnlock(int hMem);
   int GlobalFree(int hMem);
   void RtlMoveMemory(int, uint&[], uint);
#import

#import "user32.dll"
   int OpenClipboard(int hOwnerWindow);
   int EmptyClipboard();
   int CloseClipboard();
   int SetClipboardData(int Format, int hMem);
#import

#define GMEM_MOVEABLE   2
#define CF_DIB          8
#define SZBITMAPHEADER  14


bool CopyScreenshotToClipboard()
{
   bool bReturnvalue = false;

   // Temporary file   
   string strFile = StringConcatenate(ChartID(), GetTickCount(), ".bmp");

   // Take screenshot
   if (!ChartScreenShot(0, strFile, (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS), (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS))) {
      // Screenshot failed

   } else {
      // Open file 
      int h = FileOpen(strFile, FILE_BIN | FILE_READ);
      if (h == INVALID_HANDLE) {
         // File open failed

      } else {
         // Get file size
         uint szFile = (uint)FileSize(h);
         
         // Try grabbing ownership of the clipboard 
         if (OpenClipboard(0) == 0) {
            // Failed to open the clipboard

         } else {
            // Try emptying the clipboard
            if (EmptyClipboard() == 0) {
               // Failed to empty the clipboard

            } else {
               // Try allocating a block of global memory to hold the text 
               int hMem = GlobalAlloc(GMEM_MOVEABLE, szFile - SZBITMAPHEADER);         
               if (hMem == 0) {
                  // Memory allocation failed
               } else {

                  // Lock the memory
                  int ptrMem = GlobalLock(hMem);
                  if (ptrMem == 0) {               
                     // Memory lock failed                  
                     GlobalFree(hMem);
                  } else {

                     // Skip past the file header
                     FileSeek(h, SZBITMAPHEADER, SEEK_SET);

                     // Read the file, minus the header, into an array
                     uint arrData[];
                     ArrayResize(arrData, (szFile - SZBITMAPHEADER) / 4);
                     FileReadArray(h, arrData);                     

                     // Copy the array into the memory block, and then release control of the memory
                     RtlMoveMemory(ptrMem, arrData, szFile - SZBITMAPHEADER);
                     GlobalUnlock(hMem);  

                     // Try setting the clipboard contents using the global memory
                     if (SetClipboardData(CF_DIB, hMem) != 0) {
                        // Okay
                        bReturnvalue = true;   

                     } else {
                        // Failed to set the clipboard using the global memory
                        GlobalFree(hMem);
                     }               
                  }
               }
            }
         }
      
         FileClose(h);
      }
   
      FileDelete(strFile);
   }

   return bReturnvalue;
}
 

Thank you My friend, 

it works fine!

 
don't forget to CloseClipboard() after SetClipboardData() to make it working
 

Hello 

is there any way to copy a file to clipboard ?

 

Thanks you.

I want to copy PNG file to clipboard , can you help me how to implement it?