2013-03-20 8 views
0

신선한 CHARFORMAT2W를 만들고 문제없이 재생하지만 리치 츠 컨트롤에서 서식을 덮어 쓰면 구조가 손상되어 다시 적용 할 수없는 것처럼 보입니다. 그러나 오류는 발생하지 않습니다.EM_GETCHARFORMAT 결과가있는 CHARFORMAT2 구조체를 덮어 쓰면 서식이 표시되지 않습니다.

#include <iostream> 
#include <windows.h> 
#include <richedit.h> 

int main() { 
    using namespace std; 
    LoadLibrary("Msftedit.dll"); 
    HWND richeditWindow = CreateWindowExW (
    WS_EX_TOPMOST, 
    L"RICHEDIT50W", 
    L"window text", 
    WS_SYSMENU | WS_VSCROLL | ES_MULTILINE | ES_NOHIDESEL | WS_VISIBLE, 
    50, 50, 500, 500, 
    NULL, NULL, NULL, NULL 
); 


    GETTEXTLENGTHEX gtl; 
    gtl.flags = GTL_NUMCHARS; 
    gtl.codepage = 1200; 
    int text_len = SendMessageW(richeditWindow, EM_GETTEXTLENGTHEX, (WPARAM)&gtl, (LPARAM)NULL); 
    CHARRANGE cr = {text_len,text_len}; 
    SendMessageW(richeditWindow, EM_EXSETSEL, 0, (LPARAM)&cr); 
    static CHARFORMAT2W cf; 
    memset(&cf, 0, sizeof cf); 
    cf.cbSize = sizeof cf; 
    cf.dwMask = CFM_COLOR | CFM_BACKCOLOR; 
    SetLastError(0); 
    // disabling this line causes text to be colored 
    SendMessageW(richeditWindow, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); 
    if (GetLastError()) { 
    printf("EM_GETCHARFORMAT failed: %ld", GetLastError()); 
    } 
    cf.crTextColor = RGB(255,0,0); 
    cf.crBackColor = RGB(233,233,0); 
    if (!SendMessageW(richeditWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf)) { 
    printf("EM_SETCHARFORMAT failed: %ld", GetLastError()); 
    } 
    SendMessageW(richeditWindow, EM_REPLACESEL, FALSE, (LPARAM) L"... some more text, should be colored"); 


    MSG msg; 
    while(GetMessageW(&msg, richeditWindow, 0, 0)) { 
    TranslateMessage(&msg); 
    DispatchMessageW(&msg); 
    } 
} 
+1

'EM_GETCHARFORMAT'는 검색 할 매개 변수를 알려주기 위해'dwMask'를 설정해야합니다. –

+0

@JonathanPotter 메시지 앞에 dwMask를 넣었지만 여전히 작동하지 않습니다. – rsk82

답변

1

EM_GETCHARFORMAT의 작동 방식을 잘못 해석했습니다. 전달한 구조체의 dwMask 값에 응답하지 않습니다. 대신 구조체를 가능한 한 많이 채 웁니다. documentation는 다음과 같이 말합니다 :

dwMask 멤버는 ​​전체 선택 항목에서 일관된 속성을 지정합니다.

은 이것이 의미하는 것은 풍부한 편집 컨트롤이 일치 속성을 지정 값으로 dwMask에 할당하는 것입니다.

따라서 이후 호출을 EM_SETCHARFORMAT으로 호출하기 전에 구조체를 완전히 다시 초기화해야합니다.