When opening the Windows clipboard history (by pressing Win+V, or the emoji panel by pressing Win+.) it opens at the location of the caret (cursor, although people seem to confuse that with mouse pointer often) no matter what application I am using in many applications.
How does it do this?
I've tried detecting the cursor position using
HWND hwnd = GetForegroundWindow();
DWORD dwThreadId = GetWindowThreadProcessId(hwnd, nullptr);
AttachThreadInput(GetCurrentThreadId(), dwThreadId, true);
POINT point;
GetCaretPos(&point);
ClientToScreen(GetFocus(), &point);
AttachThreadInput(GetCurrentThreadId(), dwThreadId, false);
std::cout << "X: " << point.x << ", Y: " << point.y << "\n";
but this only works in applications with a "standard" Win32-based textbox, such as Notepad, and the coordinates this finds for some reason drift away from the actual location of the caret by an amount proportional to where in the text (how far away from the top left corner of the textbox) the caret is.
Besides that last point about the drifting of the coordinates, in applications that use a custom text input control, such as VSCode, it doesn't work at all and simply returns (0, 0).
So how does the Win+V panel do it?