{"id":7,"date":"2007-04-16T17:55:05","date_gmt":"2007-04-16T16:55:05","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/2007\/04\/16\/how-to-save-and-load-a-windows-region-with-the-win32-api\/"},"modified":"2007-04-16T17:55:05","modified_gmt":"2007-04-16T16:55:05","slug":"how-to-save-and-load-a-windows-region-with-the-win32-api","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2007\/04\/16\/how-to-save-and-load-a-windows-region-with-the-win32-api\/","title":{"rendered":"How to Save and Load a Windows Region with the Win32 API"},"content":{"rendered":"<p>In my previous post I explained how to save a Windows region to a file and how to load it again using MFC. Some people have problems translating MFC code to standard Win32 API code, so I wrote a followup article for CodeGuru explaining exactly the same thing but\u00a0with Win32 API code instead of MFC.<\/p>\n<p>The article explains in details how to save a Windows region to a file using <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms536649.aspx\">GetRegionData<\/a> and how to load and re-create this saved region with <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms536637.aspx\">ExtCreateRegion<\/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\u00a0\u00a0 \/\/ Create some elliptic region as demonstration \r\n\u00a0\u00a0 RECT rc; \r\n\u00a0\u00a0 GetClientRect(g_hWnd, &amp;rc); \r\n\u00a0\u00a0 HRGN hRgn = ::CreateEllipticRgn(0, 0, rc.right, rc.bottom);   \r\n\r\n\u00a0\u00a0 \/\/ Get the size in bytes of our created region \r\n\u00a0\u00a0 int iSize = ::GetRegionData(hRgn, sizeof(RGNDATA), NULL);   \r\n\r\n\u00a0\u00a0 \/\/ Allocate memory to hold the region data \r\n\u00a0\u00a0 RGNDATA* pData = (RGNDATA*)calloc(iSize, 1); \r\n\u00a0\u00a0 pData-&gt;rdh.dwSize = iSize;   \r\n\r\n \u00a0 \/\/ Get the region data \r\n\u00a0\u00a0 int iSize2 = ::GetRegionData(hRgn, iSize, pData); \r\n\u00a0\u00a0 \/\/ Sanity check \r\n\u00a0\u00a0 if (iSize != iSize2) \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ::MessageBox(g_hWnd, L\"Something wrong with GetRegionData...\", \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 L\"Error\", MB_ICONERROR);   \r\n\r\n\u00a0\u00a0 \/\/ Save region data to a file \r\n\u00a0\u00a0 FILE* f = fopen(\"test_region.rgn\", \"wb\"); \r\n\u00a0\u00a0 fwrite((char*)pData, sizeof(char), iSize, f); \r\n\u00a0\u00a0 fclose(f);   \r\n\r\n\u00a0\u00a0 \/\/ Free allocated memory \r\n\u00a0\u00a0 free(pData);   \r\n\r\n\u00a0\u00a0 \/\/ Delete our region \r\n\u00a0\u00a0 ::DeleteObject(hRgn); \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   FILE* f = fopen(\"test_region.rgn\", \"rb\");   \r\n\r\n   \/\/ Get size of the file \r\n   fseek(f, 0, SEEK_END); \r\n   int iSize = ftell(f); \r\n   fseek(f, 0, SEEK_SET);            \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   fread((char*)pData, sizeof(char), iSize, f); \r\n   fclose(f);            \r\n\r\n   \/\/ Create region from loaded region data \r\n   HRGN hRgn = ::ExtCreateRegion(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(g_hWnd, hRgn, 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\/g-m\/bitmap\/usingregions\/article.php\/c13547\/\">How to Save and Load a Windows Region with the Win32 API<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I explained how to save a Windows region to a file and how to load it again using MFC. Some people have problems translating MFC code to standard Win32 API code, so I wrote a followup article for CodeGuru explaining exactly the same thing but\u00a0with Win32 API code instead of MFC. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-c","category-win32-api"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/7","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=7"}],"version-history":[{"count":0,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}