这是我的尝试,我不知道为什么EM_STREAMOUT
在遇到非 ascii 字符时返回问号。
此代码从窗口复制文本并将其再次放回,但这次仅作为问号。
#include <windows.h>
#include <iostream>
#include <richedit.h>
#include <string>
static DWORD CALLBACK EditStreamOutCallback(unsigned long dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb)
{
using namespace std;
wstring* ps = (wstring*) dwCookie;
*pcb = cb;
for (long i = 0; i < cb; i++) {
ps->append(1,pbBuff[i]);
}
return 0;
}
int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR, int /* nCmdShow*/) {
using namespace std;
LoadLibraryW(L"Msftedit.dll");
HWND richeditWindow = CreateWindowExW (
WS_EX_TOPMOST,
L"RICHEDIT50W",
L"施氏食獅史",
WS_SYSMENU | WS_VSCROLL | ES_MULTILINE | ES_NOHIDESEL | WS_VISIBLE,
50, 50, 500, 500,
NULL, NULL, NULL, NULL
);
wstring s;
EDITSTREAM eds;
eds.dwCookie = (unsigned long) &s;
eds.dwError = 0;
eds.pfnCallback = EditStreamOutCallback;
SendMessageW(richeditWindow, EM_STREAMOUT, (WPARAM) SF_TEXTIZED, (LPARAM) &eds);
SendMessageW(richeditWindow, EM_REPLACESEL, FALSE, (LPARAM) s.c_str());
MSG msg;
while(GetMessageW(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}