使用`sendmessage()`发送表情符号。

huangapple go评论46阅读模式
英文:

C send emojis with sendmessage()

问题

我想使用sendmessage()函数向浏览器(如Chrome或Microsoft Edge)发送表情符号。

当我定义一个带有表情符号的字符串,并使用fputs()将其写入一个.txt文件时,输出的格式如下所示:❤❤ ++ 😘😘

字符串:snprintf(buffer, sizeof(buffer), "\U00002764\U00002764 ++ \U0001F618\U0001F618");
缓冲数组使用char数据类型。

使用sendmessage()函数时,我没有得到预期的输出。尝试发送maximizewindow命令有效。以下是我使用的代码片段:

for (int i = 0; i < strlen(buffer); i++)
{
    SendMessage(hwnd, WM_UNICHAR, (WPARAM)0, (LPARAM)buffer[i]);
}

我还尝试过PostMessage()PostMessageW()

英文:

I want send emojis to a browser like chrome or Microsoft edge with the sendmessage() function.
When i defined a string with the emojis and write it with fputs() to a .txt file the output is the expected in format of ❤❤ ++ 😘😘.

The string: snprintf(buffer, sizeof(buffer), "\U00002764\U00002764 ++ \U0001F618\U0001F618");
The buffer array use the char datatype.

With the sendmessage() function i do not get the expected output. Trying to send a maximizewindow command works. Following is my codesnipped i use:

for (int i = 0; i < strlen(buffer); i++)
{
	SendMessage(hwnd, WM_UNICHAR, (WPARAM)0, (LPARAM)buffer[i]);
}

I also tryed PostMessage() and PostMessageW().

答案1

得分: 0

我找不到任何方法在几个小时后使用SendMessage()WM_UNICHAR。但是对我来说,我找到了一个很好的替代方案,可以使用SendInput()。这些函数支持UTF32字符,并在我测试的每个窗口中都能正常工作。接下来,我将发布我的代码以帮助其他人。

short si_ret;
INPUT pInputs;
pInputs.type = INPUT_KEYBOARD;
pInputs.ki.wVk = 0; // 在dwFlags为零时使用普通字符,否则在Unicode上将wVk设置为零
pInputs.ki.wScan = (WORD)0; // 使用L'❤'来发送UTF-32字符,或匹配的值(参见WEB或wprintf)
pInputs.ki.dwFlags = KEYEVENTF_UNICODE;
wchar_t text[] = L"❤❤😘😘";

for (unsigned int x = 0; x < wcslen(text); x++)
{
    pInputs.ki.wScan = (WORD)text[x];
    if (!(si_ret = SendInput(1, &pInputs, sizeof(pInputs)))) { printf("SendInput do not work [%i]\r\n", GetLastError()); return 0; }
    else { printf("SendInput ok [%i]\r\n", si_ret); }
}

希望这有助于您。

英文:

I cant find any way to use SendMessage() with WM_UNICHAR after hours. But for me i found a good alternative solution with SendInput(). These function supports UTF32 characters and works in every window i tested. Following i will post my code to help others.

short si_ret;
INPUT pInputs;
pInputs.type		=	INPUT_KEYBOARD;
pInputs.ki.wVk		=	0; //Use here normal characters when dwFlags zero, otherwise on unicode set wVk to zero
pInputs.ki.wScan	=	(WORD)0; //Use L&#39;❤&#39; to send UTF-32 characters or the matching value (see WEB or wprintf)
pInputs.ki.dwFlags	=	KEYEVENTF_UNICODE;
wchar_t text[] = L&quot;❤❤&#128536;&#128536;&quot;;
		
for(unsigned int x = 0; x &lt; wcslen(text); x++)
{
	pInputs.ki.wScan	=	(WORD)text[x];
	if(!(si_ret = SendInput(1, &amp;pInputs, sizeof(pInputs)))) { printf(&quot;SendInput do not work [%i]\r\n&quot;, GetLastError()); return 0; }
	else { printf(&quot;SendInput ok [%i]\r\n&quot;, si_ret); }
}

huangapple
  • 本文由 发表于 2023年6月29日 04:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定