{"id":6,"date":"2007-04-16T17:52:41","date_gmt":"2007-04-16T16:52:41","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/2007\/04\/16\/how-to-save-and-load-a-windows-region-with-mfc\/"},"modified":"2007-04-16T17:52:41","modified_gmt":"2007-04-16T16:52:41","slug":"how-to-save-and-load-a-windows-region-with-mfc","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2007\/04\/16\/how-to-save-and-load-a-windows-region-with-mfc\/","title":{"rendered":"How to Save and Load a Windows Region with MFC"},"content":{"rendered":"<p>I wrote an article for CodeGuru that explains in details how to save a Windows region to a file using <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/c6f4fdaa(VS.71).aspx\">CRgn::GetRegionData<\/a> and how to load and re-create this saved region with <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/dh2exf30(VS.71).aspx\">CRgn::CreateFromData<\/a> using MFC.<\/p>\n<p>There are 2 functions in the article: <strong>SaveRegion<\/strong> and <strong>LoadRegion<\/strong>. <!--more-->For demonstration purposes, the SaveRegion function will first create an elliptic region and will then save this region to a file. The LoadRegion function will read the data back from the file and create a Windows region from this data. For making it visually clear that the LoadRegion function has loaded the correct region, the region is set as the window region.<\/p>\n<p>The SaveRegion function looks like:<\/p>\n<blockquote>\n<pre><code>\/\/ lang cpp \r\nvoid SaveRegion() \r\n{ \r\n   \/\/ Create some elliptic region as demonstration \r\n   CRect rc; \r\n   GetWindowRect(rc); \r\n   CRgn rgn; \r\n   rgn.CreateEllipticRgn(0, 0, rc.Width(), rc.Height()); \r\n\r\n   \/\/ Get the size in bytes of our created region \r\n   int iSize = rgn.GetRegionData(NULL, sizeof(RGNDATA)); \r\n\r\n   \/\/ Allocate memory to hold the region data \r\n   RGNDATA* pData = (RGNDATA*)calloc(iSize, 1); \r\n   pData-&gt;rdh.dwSize = iSize; \r\n\r\n   \/\/ Get the region data \r\n   int iSize2 = rgn.GetRegionData(pData, iSize); \r\n   \/\/ Sanity check \r\n   if (iSize != iSize2) \r\n      AfxMessageBox(_T(\"Something wrong with GetRegionData...\")); \r\n\r\n   \/\/ Save region data to a file \r\n   CFile f(_T(\"test_region.rgn\"), CFile::modeCreate | CFile::modeWrite); \r\n   f.Write(pData, iSize); \r\n   f.Close(); \r\n\r\n   \/\/ Free allocated memory \r\n   free(pData); \r\n}<\/code><\/pre>\n<\/blockquote>\n<p>The LoadRegion function looks like:<\/p>\n<blockquote>\n<pre><code>\/\/ lang cpp \r\nvoid LoadRegion() \r\n{ \r\n   \/\/ Open file to read region data from \r\n   CFile f(_T(\"test_region.rgn\"), CFile::modeRead); \r\n\r\n   \/\/ Get size of the file \r\n   int iSize = f.GetLength(); \r\n\r\n   \/\/ Allocate memory to hold the region data \r\n   RGNDATA* pData = (RGNDATA*)calloc(iSize, 1); \r\n\r\n   \/\/ Read region data from file \r\n   f.Read(pData, iSize); \r\n   f.Close(); \r\n\r\n   \/\/ Create region from loaded region data \r\n   CRgn rgn; \r\n   rgn.CreateFromData(NULL, iSize, pData); \r\n\r\n   \/\/ As a demonstration, set the loaded region as window region \r\n   \/\/ so it is visually clear that it got loaded correctly. \r\n   SetWindowRgn(rgn,TRUE); \r\n\r\n   \/\/ Free allocated memory \r\n   free(pData); \r\n}<\/code><\/pre>\n<\/blockquote>\n<p>For a more detailed explanation of all the steps in the above 2 functions, check out my article at CodeGuru: <a target=\"_blank\" href=\"http:\/\/www.codeguru.com\/cpp\/cpp\/cpp_mfc\/general\/article.php\/c13551\/\">How to Save and Load a Windows Region with MFC<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote an article for CodeGuru that explains in details how to save a Windows region to a file using CRgn::GetRegionData and how to load and re-create this saved region with CRgn::CreateFromData using MFC. There are 2 functions in the article: SaveRegion and LoadRegion.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-c","category-mfc"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}