简体   繁体   English

EM_SETHANDLE, EM_GETHANDLE 在没有 DS_LOCALEDIT 的情况下工作

[英]EM_SETHANDLE, EM_GETHANDLE works without DS_LOCALEDIT

I made a program similar to Notepad using Visual Studio Community 2017 on Windows 10. It uses edit control created with CreateWindow with the following styles:我在 Windows 10 上使用 Visual Studio Community 2017 制作了一个类似于记事本的程序。它使用使用 CreateWindow 创建的具有以下样式的编辑控件:

WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL 
| WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL

As you see, there's no DS_LOCALEDIT .如您所见,没有DS_LOCALEDIT However, using EM_SETHANDLE or EM_GETHANDLE to access the buffer within the edit control seems to work flawlessly.但是,使用 EM_SETHANDLE 或 EM_GETHANDLE 访问编辑控件中的缓冲区似乎可以完美地工作。 The following is a snippet of code that does initial buffer allocation for edit control that is supposed to have been created with DS_LOCALEDIT :以下是一段代码,它为应该使用DS_LOCALEDIT创建的编辑控件执行初始缓冲区分配:

HLOCAL hEditMem = ::LocalAlloc(LPTR, sizeof(wchar_t) * 51);
wchar_t* pszEdit = reinterpret_cast<wchar_t*>(::LocalLock(hEditMem));
const std::wstring strData(L"Hello");
std::char_traits<wchar_t>::copy(pszEdit, strData.c_str(), strData.size());
::SendMessageW(hwndEdit, EM_SETHANDLE, reinterpret_cast<WPARAM>(hEditMem), 0);
::SendMessageW(hwndEdit, EM_SETMODIFY, TRUE, 0);

The documentation here clearly states that: 此处的文档明确指出:

An application that uses the default allocation behavior (that is, does not use the
DS_LOCALEDIT style must not send EM_SETHANDLE and EM_GETHANDLE messages to the edit
control.

May be someone in Microsoft inconspicuously made the DS_LOCALEDIT no longer necessary as of Windows 10 or VS 2017 ?从 Windows 10 或 VS 2017 开始,Microsoft 中的某个人可能在不显眼的DS_LOCALEDIT不再需要DS_LOCALEDIT吗?

Since no answer from anybody, I decided to answer my own ...由于没有任何人的回答,我决定回答我自己的......

Using heuristic approach, I came to the following conclusions, which are unlike what's written on MSDN docs:使用启发式方法,我得出了以下结论,这与 MSDN 文档中所写的不同:

  1. DS_LOCALEDIT is not a prerequisite for EM_SETHANDLE or EM_GETHANDLE, which means that the EM_XXXHANDLE can be used even when you're not managing the buffer customarily. DS_LOCALEDIT 不是 EM_SETHANDLE 或 EM_GETHANDLE 的先决条件,这意味着即使您不习惯地管理缓冲区,也可以使用 EM_XXXHANDLE。
  2. EM_GETHANDLE seems a far better choice than the WM_GETTEXT when all you need to do is just refer to a portion of text in edit control.当您需要做的只是引用编辑控件中的一部分文本时,EM_GETHANDLE 似乎是比 WM_GETTEXT 更好的选择。
  3. DS_LOCAEDIT seems to be obsolete, as I was able to set my own custom buffer without it. DS_LOCAEDIT 似乎已过时,因为我可以在没有它的情况下设置自己的自定义缓冲区。 (As a side note, all I needed to do to increase the custom buffer, in response to EN_MAXTEXT, was to send EM_SETLIMITTEXT message with new size as parameter) (作为旁注,我需要做的就是增加自定义缓冲区,以响应 EN_MAXTEXT,发送具有新大小作为参数的 EM_SETLIMITTEXT 消息)

To me the second one was particularly important when implementing text find/replace using FindText and ReplaceText ;对我来说,第二个在使用FindTextReplaceText实现文本查找/替换时特别重要; you don't want to copy all the text from the edit control, using WM_GETTEXT, just to search for certain keyword, would you?您不想使用 WM_GETTEXT 从编辑控件复制所有文本,只是为了搜索某个关键字,对吗?

I still haven't tested if plain old new can substitute LocalAlloc when setting custom buffer with EM_SETHANDLE.我还没有,如果普通的旧测试new可以替代LocalAlloc设置自定义与EM_SETHANDLE缓冲区时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM