如何从richedit控件中流出宽字符串?

时间:2013-05-17 10:06:10

标签: winapi richedit widestring

这是我的尝试,我不知道为什么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;
}

1 个答案:

答案 0 :(得分:0)

SendMessageW(richeditWindow,EM_STREAMOUT,(WPARAM)SF_TEXTIZED | SF_UNICODE ,(LPARAM)&amp; eds);