1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| #include <Windows.h> #include <atlbase.h> #include <Richedit.h> #include <TextServ.h> #include <memory>
class TextHost : public ITextHost { public: HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject) override {
if (ppvObject == nullptr) { return E_POINTER; }
if ((riid == IID_IUnknown) || (riid == IID_ITextHost)) { *ppvObject = this; return S_OK; }
*ppvObject = nullptr; return E_NOINTERFACE; }
ULONG __stdcall AddRef(void) override { return 0; }
ULONG __stdcall Release(void) override { return 0; }
HDC TxGetDC() override { return nullptr; }
INT TxReleaseDC(HDC hdc) override { return 0; }
BOOL TxShowScrollBar(INT fnBar, BOOL fShow) override { return FALSE; }
BOOL TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags) override { return FALSE; }
BOOL TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw) override { return FALSE; }
BOOL TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw) override { return FALSE; }
void TxInvalidateRect(LPCRECT prc, BOOL fMode) override {
}
void TxViewChange(BOOL fUpdate) override {
}
BOOL TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight) override { return FALSE; }
BOOL TxShowCaret(BOOL fShow) override { return FALSE; }
BOOL TxSetCaretPos(INT x, INT y) override { return FALSE; }
BOOL TxSetTimer(UINT idTimer, UINT uTimeout) override { return FALSE; }
void TxKillTimer(UINT idTimer) override {
}
void TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll) override {
}
void TxSetCapture(BOOL fCapture) override {
}
void TxSetFocus() override {
}
void TxSetCursor(HCURSOR hcur, BOOL fText) override {
}
BOOL TxScreenToClient(LPPOINT lppt) override { return FALSE; }
BOOL TxClientToScreen(LPPOINT lppt) override { return FALSE; }
HRESULT TxActivate(LONG * plOldState) override { return E_NOTIMPL; }
HRESULT TxDeactivate(LONG lNewState) override { return E_NOTIMPL; }
HRESULT TxGetClientRect(LPRECT prc) override { return E_NOTIMPL; }
HRESULT TxGetViewInset(LPRECT prc) override { return E_NOTIMPL; }
HRESULT TxGetCharFormat(const CHARFORMATW **ppCF) override {
if (char_format_ == nullptr) { char_format_ = std::make_unique<CHARFORMATW>(); char_format_->cbSize = sizeof(CHARFORMATW); }
*ppCF = char_format_.get(); return S_OK; }
HRESULT TxGetParaFormat(const PARAFORMAT **ppPF) override {
if (para_format_ == nullptr) { para_format_ = std::make_unique<PARAFORMAT>(); para_format_->cbSize = sizeof PARAFORMAT; }
*ppPF = para_format_.get(); return S_OK; }
COLORREF TxGetSysColor(int nIndex) override { return GetSysColor(nIndex); }
HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle) override { return E_NOTIMPL; }
HRESULT TxGetMaxLength(DWORD *plength) override { return E_NOTIMPL; }
HRESULT TxGetScrollBars(DWORD *pdwScrollBar) override { return E_NOTIMPL; }
HRESULT TxGetPasswordChar(_Out_ TCHAR *pch) override { return E_NOTIMPL; }
HRESULT TxGetAcceleratorPos(LONG *pcp) override { return E_NOTIMPL; }
HRESULT TxGetExtent(LPSIZEL lpExtent) override { return E_NOTIMPL; }
HRESULT OnTxCharFormatChange(const CHARFORMATW * pCF) override { return E_NOTIMPL; }
HRESULT OnTxParaFormatChange(const PARAFORMAT * pPF) override { return E_NOTIMPL; }
HRESULT TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits) override { *pdwBits = 0; return S_OK; }
HRESULT TxNotify(DWORD iNotify, void *pv) override { return E_NOTIMPL; }
HIMC TxImmGetContext() override { return nullptr; }
void TxImmReleaseContext(HIMC himc) override {
}
HRESULT TxGetSelectionBarWidth(LONG *lSelBarWidth) override { *lSelBarWidth = 0; return S_OK; }
private: std::unique_ptr<CHARFORMATW> char_format_; std::unique_ptr<PARAFORMAT> para_format_; };
EXTERN_C const IID IID_ITextServices = { 0x8d33f740, 0xcf58, 0x11ce, { 0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5 } };
EXTERN_C const IID IID_ITextHost = { 0xc5bdd8d0, 0xd26e, 0x11ce, { 0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5 } };
std::shared_ptr<TextHost> g_text_host; CComPtr<ITextServices> g_text_service;
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE, HINSTANCE, char*, int) {
WNDCLASSEX default_class = { 0 }; default_class.cbSize = sizeof(default_class); default_class.style = CS_HREDRAW | CS_VREDRAW; default_class.lpfnWndProc = WindowProcedure; default_class.cbClsExtra = 0; default_class.cbWndExtra = 0; default_class.hInstance = nullptr; default_class.hIcon = nullptr; default_class.hCursor = LoadCursor(nullptr, IDI_APPLICATION); default_class.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1); default_class.lpszMenuName = nullptr; default_class.lpszClassName = L"WindowlessRichEdit"; default_class.hIconSm = nullptr;
RegisterClassEx(&default_class);
HWND window_handle = CreateWindowEx( 0, L"WindowlessRichEdit", nullptr, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, nullptr, nullptr );
MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
return 0; }
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) { case WM_CREATE: {
g_text_host = std::make_shared<TextHost>();
HMODULE module_handle = LoadLibrary(L"riched20.dll");
typedef HRESULT(_stdcall*CreateTextServicesFunction)(IUnknown*, ITextHost*, IUnknown**); CreateTextServicesFunction create_function = reinterpret_cast<CreateTextServicesFunction>(GetProcAddress(module_handle, "CreateTextServices"));
const IID* iid_text_service = reinterpret_cast<IID*>(GetProcAddress(module_handle, "IID_ITextServices"));
CComPtr<IUnknown> unknown; create_function(nullptr, g_text_host.get(), &unknown); unknown->QueryInterface(*iid_text_service, reinterpret_cast<void**>(&g_text_service));
g_text_service->TxSetText(L"Windowless RichEdit"); return 0; }
case WM_PAINT: {
PAINTSTRUCT paint_struct; HDC hdc = BeginPaint(hwnd, &paint_struct);
RECT rect; GetClientRect(hwnd, &rect);
g_text_service->TxDraw( DVASPECT_CONTENT, 0, nullptr, nullptr, hdc, nullptr, reinterpret_cast<LPCRECTL>(&rect), nullptr, nullptr, nullptr, 0, 0 );
EndPaint(hwnd, &paint_struct); return 0; }
default: return CallWindowProc(DefWindowProc, hwnd, message, wParam, lParam); } }
|