...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

RichEditコントロールからテキストを取得する

DFLのRichTextBox用にせっかく書いたけど不要だった・・・ここで供養する。

// この辺のインポートが必要だけど競合するので選択インポートする。
private import dfl.internal.winapi;
private import core.sys.windows.richedit : GETTEXTEX, GETTEXTLENGTHEX, GTL_CLOSE;
final @property Dstring getText() // getter
{
    GETTEXTLENGTHEX getLength;
    getLength.codepage = 1200; // Unicode
    getLength.flags = GTL_CLOSE;
    int textLength = cast(int)sendMessage(handle, EM_GETTEXTLENGTHEX, cast(WPARAM)&getLength, 0);

    GETTEXTEX getText;
    getText.cb = textLength + cast(uint)wchar.sizeof; // Add Utf16 null terminater.

    // dmd同梱のrichedit.dには定義されていなくて古いもよう。
    // Flags for the GETEXTEX data structure
    enum GT_DEFAULT = 0;
    enum GT_USECRLF = 1;
    enum GT_SELECTION = 2;
    enum GT_RAWTEXT = 4;
    enum GT_NOHIDDENTEXT = 8;
    getText.flags = GT_DEFAULT; // GT_RAWTEXT | GT_SELECTION;
    getText.codepage = 1200; // Unicode
    getText.lpDefaultChar = null;
    getText.lpUsedDefChar = null;

    ubyte[] buf = new ubyte[getText.cb];
    int copiedLength = cast(int)sendMessage(handle, EM_GETTEXTEX, cast(WPARAM)&getText, cast(LPARAM)buf.ptr);
    return fromUnicode(cast(wchar*)buf.ptr, copiedLength);
}

参考文献