{"id":28463,"date":"2007-01-10T10:00:13","date_gmt":"2007-01-10T10:00:13","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/01\/10\/how-do-i-load-an-entire-file-into-a-rich-text-control\/"},"modified":"2007-01-10T10:00:13","modified_gmt":"2007-01-10T10:00:13","slug":"how-do-i-load-an-entire-file-into-a-rich-text-control","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070110-13\/?p=28463","title":{"rendered":"How do I load an entire file into a rich text control?"},"content":{"rendered":"<p>\nTo load an entire file into a rich text control, you can use\nthe <code>EM_STREAMIN<\/code> message,\nwhich accepts an <code>IStream<\/code> of data all at once.\nOnce you find the message, it&#8217;s pretty straightforward how to use it,\nbut I&#8217;ll write out the code anyway;\n<\/p>\n<pre>\nDWORD CALLBACK EditStreamCallback(DWORD_PTR dwCookie, LPBYTE lpBuff,\n                                  LONG cb, PLONG pcb)\n{\n HANDLE hFile = (HANDLE)dwCookie;\n return !ReadFile(hFile, lpBuff, cb, (DWORD *)pcb, NULL);\n}\nBOOL FillRichEditFromFile(HWND hwnd, LPCTSTR pszFile)\n{\n BOOL fSuccess = FALSE;\n HANDLE hFile = CreateFile(pszFile, GENERIC_READ, FILE_SHARE_READ,\n                           0, OPEN_EXISTING,\n                           FILE_FLAG_SEQUENTIAL_SCAN, NULL);\n if (hFile != INVALID_HANDLE_VALUE) {\n  EDITSTREAM es = { (DWORD_PTR)hFile, 0, EditStreamCallback };\n  if (SendMessage(hwnd, EM_STREAMIN, SF_RTF, (LPARAM)&amp;es) &amp;&amp;\n      es.dwError == 0) {\n   fSuccess = TRUE;\n  }\n  CloseHandle(hFile);\n }\n return fSuccess;\n}\n<\/pre>\n<p>\nYou pretty much follow your nose.\nThe <code>EM_STREAMIN<\/code> message wants you\nto tell it the format of the stream (<code>SF_RTF<\/code>)\nand provide a pointer to an <code>EDITSTREAM<\/code> structure\nthat controls the input.\nSince we want to read from a file, we open a file for reading\nand use it as the <code>dwCookie<\/code> for\nour <code>EditStreamCallback<\/code>.\nThe only tricky part is getting the return value correct for the\ncallback.\nFor some reason, the rich edit control wants zero on success and\nnonzero on failure, so we need to flip the sense of the\n<code>ReadFile<\/code> return value accordingly.\nAside from that, there&#8217;s nothing particularly interesting going on.\n<\/p>\n<p>\n&#8220;But I tried this, and only the first line of the file gets read in.\nWhat am I doing wrong?&#8221;\n<\/p>\n<\/p>\n<p>Ah, a classic rookie mistake.\nYou forgot to set the <code>ES_MULTILINE<\/code> style when you created\nthe rich edit control.\n<\/p>\n<p>\nDon&#8217;t worry, I made this mistake, too.\n<\/p>\n<p>\n&#8220;What if my data is in some other format than a file?&#8221;\n<\/p>\n<p>\nAs long as you can write a function that produces the next few bytes\nof data,\nyou can stream it into a rich edit control.\nFor example, here&#8217;s a version that loads an arbitrary <code>IStream<\/code>\ninto a rich edit control:\n<\/p>\n<pre>\nDWORD CALLBACK EditStreamCallback(DWORD_PTR dwCookie, LPBYTE lpBuff,\n                                  LONG cb, PLONG pcb)\n{\n IStream *pstm = (IStream *)dwCookie;\n return FAILED(pstm-&gt;Read(lpBuff, cb, (ULONG*)pcb));\n}\nBOOL FillRichEditFromStream(HWND hwnd, IStream *pstm)\n{\n BOOL fSuccess = FALSE;\n EDITSTREAM es = { (DWORD_PTR)pstm, 0, EditStreamCallback };\n if (SendMessage(hwnd, EM_STREAMIN, SF_RTF, (LPARAM)&amp;es) &amp;&amp;\n     es.dwError == 0) {\n  fSuccess = TRUE;\n }\n return fSuccess;\n}\n<\/pre>\n<p>\nThere&#8217;s still a bug in this code, however,\nand it&#8217;s not where you expect it.\nWe&#8217;ll take another look next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To load an entire file into a rich text control, you can use the EM_STREAMIN message, which accepts an IStream of data all at once. Once you find the message, it&#8217;s pretty straightforward how to use it, but I&#8217;ll write out the code anyway; DWORD CALLBACK EditStreamCallback(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, PLONG pcb) { [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-28463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>To load an entire file into a rich text control, you can use the EM_STREAMIN message, which accepts an IStream of data all at once. Once you find the message, it&#8217;s pretty straightforward how to use it, but I&#8217;ll write out the code anyway; DWORD CALLBACK EditStreamCallback(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, PLONG pcb) { [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=28463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28463\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=28463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=28463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=28463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}