NO

Author Topic: Select text for clipboard  (Read 10384 times)

Offline John Z

  • Member
  • *
  • Posts: 950
Re: Select text for clipboard
« Reply #15 on: November 25, 2024, 02:40:25 PM »
Hi DonnyDave,

Great news.  Yes using an Edit window is much easier than using a main window and writing with TextOut.
The Edit control is like a normal text editor albeit with some significant restrictions. Yet it does provide a
means to grab the text back out in total or as user selected text.

Good to not have given up...

John Z

Online HellOfMice

  • Member
  • *
  • Posts: 300
  • Never be pleased, always improve
Re: Select text for clipboard
« Reply #16 on: November 25, 2024, 08:44:14 PM »

With that you can copy text to the clipboard

Code: [Select]

#include <windows.h>
#include <windowsx.h>
#include <stdalign.h>

BOOL WINAPI CopyToClipboard(HWND __hWnd,LPSTR __lpszText2Copy)
{
   alignas(LPSTR) LPTSTR   _lptstrCopy ;
   alignas(HANDLE) HGLOBAL   _hglbCopy ;
   register int      _cch, _iLen ;

   _iLen = lstrlen(__lpszText2Copy) ;
   if(_iLen == 0)
      return (FALSE) ;

   if(!OpenClipboard(__hWnd))
      return (FALSE) ;

   EmptyClipboard() ;

   cch = _iLen + 256 ;

   _hglbCopy = GlobalAlloc(GMEM_MOVEABLE,(_cch + 1) * sizeof(TCHAR)) ;
   if(_hglbCopy == NULL)
   {
      CloseClipboard() ;
      return (FALSE) ;
   }

   _lptstrCopy = GlobalLock(_hglbCopy) ;
   memcpy(_lptstrCopy,__lpszText2Copy,_cch * sizeof(TCHAR)) ;
   _lptstrCopy[_cch] = (TCHAR) 0 ;
   GlobalUnlock(_hglbCopy) ;

   SetClipboardData(CF_TEXT,_hglbCopy) ;
   CloseClipboard() ;

   return (TRUE) ;
}
« Last Edit: November 26, 2024, 03:45:25 AM by HellOfMice »
--------------------------------
Kenavo

Online HellOfMice

  • Member
  • *
  • Posts: 300
  • Never be pleased, always improve
Re: Select text for clipboard
« Reply #17 on: November 26, 2024, 04:21:27 AM »
That is better
Code: [Select]
#include <windows.h>
#include <windowsx.h>
#include <stdalign.h>


// Input: The handle of a window wanting to be the clipboard owner
//           The string to copy to the clipboard


// Output : TRUE => SUCCESS
//              0 => NULL input pointer or empty string (*__lpszText2Copy2Clipboard = '\0')
//              Negative value => Cannot allocate memory or clipboard operation not possible
//              When the function returns the negative value it is the GetLastError() code
//              To get the real GetLastError() do -ErrorCode.

BOOL WINAPI CopyToClipboard(HWND __hWnd,LPSTR __lpszText2Copy2Clipboard)
{
    register               int          _iStringLength ;
    register               LPTSTR   _lptStringForClipboard ;
    alignas(HANDLE) HGLOBAL _hglbCopy ;

    if(!__lpszText2Copy2Clipboard)                                          // Don't allow NULL pointer
        return (FALSE) ;
       
    _iStringLength = lstrlen(__lpszText2Copy2Clipboard) ;        // How many char to copy to clipboard
    if(_iStringLength == 0)                                                     // Don't allow empty string
        return (FALSE) ;

    if(!OpenClipboard(__hWnd))                                              // If it is not possible to open
        return (-GetLastError()) ;                                              // the clipboard Bye, return a negative error code

    EmptyClipboard() ;                                                           // Empty the clipboard, it already can contains CF_TEXT format

    _iStringLength += 256 ;                                                   // Think to reserve a little space

   _hglbCopy = GlobalAlloc(GHND,_iStringLength) ;                 // Try to allocate memory filled with '0'
    if(_hglbCopy == NULL)                                                     // If not possible Bye Bye
    {
        CloseClipboard() ;                                                        // Close the clipboard already open
        return (-GetLastError()) ;                                              // Return a value telling the operation failed
    }

//  As the buffer is greater that the string and is filled of '\0'
//  one can use lstrcpy that is the reason that sizeof(TCHAR) has been removed

    _lptStringForClipboard = GlobalLock(_hglbCopy) ;                             // Lock the handle got to have a real pointer
    lstrcpy(_lptStringForClipboard,__lpszText2Copy2Clipboard) ;             // Copy the input string into the pointer
    GlobalUnlock(_hglbCopy) ;                                                            // Lock the memory so the pointer got is not valid afer this line

    SetClipboardData(CF_TEXT,_hglbCopy) ;                                      // Copy the datas to the clipboard telling it is TEXT
    CloseClipboard() ;                                                                     // Close the clipboard

    return (TRUE) ;                                                                       // Tell the operation was successful
}

https://learn.microsoft.com/fr-fr/windows/win32/api/winbase/nf-winbase-globalalloc
https://learn.microsoft.com/fr-fr/windows/win32/api/winbase/nf-winbase-globallock
https://learn.microsoft.com/fr-fr/windows/win32/api/winbase/nf-winbase-globalfree
https://learn.microsoft.com/fr-fr/windows/win32/api/winuser/nf-winuser-openclipboard
https://learn.microsoft.com/fr-fr/windows/win32/api/winuser/nf-winuser-closeclipboard
https://learn.microsoft.com/fr-fr/windows/win32/api/winuser/nf-winuser-setclipboarddata
https://learn.microsoft.com/fr-fr/windows/win32/api/winuser/nf-winuser-emptyclipboard
« Last Edit: November 26, 2024, 04:37:42 AM by HellOfMice »
--------------------------------
Kenavo