C language > Beginner questions

Select text for clipboard

(1/3) > >>

DonnyDave:
I'm trying to replicate the standard Win10 Cut/Copy/Paste.
In the code below all of pString goes into the clipboard.
How do I select part of on screen text using  left mouse/drag ?
I've searched Petzold and Google and found nothing useful.

  char* pGlobal;
  char* hGlobal;
  char pString[] = "Testing, Testing 123";
  char* pStringptr;
  int i;
 
hGlobal = GlobalAlloc(GHND | GMEM_SHARE, 50);
  if(hGlobal == 0)
    ErrorMessage("1");

  pGlobal = GlobalLock(hGlobal);
  if(pGlobal == 0)
    ErrorMessage("2");

  pStringptr = pString;
  for(i=0 ; i < 20 ; i++)
    *pGlobal++ = *pStringptr++;  //  Copy text to global mem.

  GlobalUnlock(hGlobal);
  OpenClipboard(hwnd);
  EmptyClipboard();

  SetClipboardData(CF_TEXT, hGlobal);
  CloseClipboard();

John Z:
Hi DonnyDave,

Welcome to the forum.

To try to answer your question a clarification is needed.

--- Quote from: DonnyDave on March 05, 2024, 11:43:27 AM ---How do I select part of on screen text using  left mouse/drag ?

--- End quote ---

Can you clarify this?  What text from the 'screen', from some user open program, from the windows display, from your own windows program text display?

for example for a edit box you might use

--- Code: ---  lHWND = GetDlgItem(gHWND, idCtrl);
  long long int tsize = SendMessageW(lHWND, WM_GETTEXTLENGTH, 0, 0);
  if (tsize > 0)
  {
    wchar_t *p_buffer = malloc((tsize * 4 * sizeof(wchar_t)) + 20);
    RetVal = SendMessageW(lHWND, WM_GETTEXT, tsize + 1, (LPARAM)p_buffer);

--- End code ---
Now text is in p_buffer which can then be sent to the clipboard. 

You can also limit selection by asking for or setting selected text by SendMessage..to the control for example

--- Code: ---SendMessage(lHWND, EM_GETSEL, (WPARAM)&Cstart, (LPARAM)&Cend);

--- End code ---



John

DonnyDave:
Hi John,
"Text from screen" could be any text on anything ( eg. https://forum.pellesc.de/index.php?action=post;topic=11150.0;last_msg=39148 )
LH Mouse anywhere in any text,
LH Mouse drag to anywhere else in the text, (selected portion goes to inverse video),
Ctrl C to copy selected to clipboard.

Dave

John Z:
Hi DonnyDave,

Checking for understanding  -
You are envisioning that a program you wrote is running on a windows system.  This programs purpose is to allow the highlighting of any text displayed on the window screen from anything that is open and displaying some text in its own program?  It does this by first allowing the highlighting of the text using the mouse, then Ctrl+C is executed to copy to the clipboard.....

So -   Several huge hurdles to overcome, imo.

As an initial thing there is the fact that everything shown on the windows screen is only a picture composed of pixels.  There is actually no 'text' available on the screen there are only pictures of text shown.  It is the underlying program that retains/creates the text in 'characters' that are then transformed into pixels and drawn in the programs window display space. This is unlike days of old where the screen was actually a character map of say 24 x 80 that was actually filled with the 'character' to be displayed ie an 'A' in row 1 column 1 was a value of 65 which is capital A.  The underlying screen map could be accessed by any program.

So for your program to use the mouse to highlight some text in my program window you would need to have access to the underlying programs storage of the text being displayed so that character could be retrieved instead of a picture of the characters.

The next hurdle is the once the mouse is over a programs windows space that program controls what the mouse can do.  If hovering over my program and I don't have a routine to do highlighting, or changed the action of the mouse buttons, then there is no way for your program to do that highlighting without developing a hook or other method to intercept the mouse and do your task before sending the mouse to the underlying program. It is the same with the keyboard btw.  When over a programs window the keystrokes go to that window, so there would need to be a hook for that too...

There are intricate ways of doing some of the above but they are very involved. 

It would be a lot easier to program a screen capture program (there are many already) then send the screen picture through an OCR program to recover the 'text' as characters.

Hopefully I've not misunderstood your intention and just went on and on for nothing  ;D

John Z

DonnyDave:
John,
That's correct, I want to incorporate into a win10 prog. I'm about to write (using Pelles C).
Dave

Navigation

[0] Message Index

[#] Next page

Go to full version