Solved

CRgn::GetRegionData

Posted on 1998-10-29
10
379 Views
Last Modified: 2013-11-20
I'm looking to find the proper syntax to retrieve the region data from CRgn::GetRegionData and how to make use of that data with CRgn::CreateFromData. I have tried this suppling the proper parameter but the variable LPRGNDATA needs to be carefully "nurtured", I'm not sure how to initialize and handle this structure.

I will give an A to whomever supplys the answer that also stores and retrieves the data from a file.
0
Comment
Question by:mite51
  • 7
  • 3
10 Comments
 
LVL 12

Expert Comment

by:migel
ID: 1324017
I think that it`s must work:

void StoreRegion(CRgn& rgn)
{
RGNDATA* pData = NULL;
BYTE* pBytes = NULL;
int nBytes = rgn.GetRegionData(NULL, 0);

nBytes += sizeof[RGNDATA]; // I`m not shure (?!), but for safety...
pBytes = new BYTE[nBytes ];

if (!pBytes) // not enough memory!
  return;
pData  = (RGNDATA*)pBytes;

if (rgn.GetRegionData(pData, nBytes) == ERROR)
  {
   delete[] pBytes;
   return; // something wrong
  }
// ok we have data now
// store data
StoreRegionData(pData, nBytes);
delete[] pBytes;
return;
}

BOOL CreateRegionFromFile(CRgn& rgn)
{
RGNDATA* pData = NULL;
int nBytes = ReadRegionData(&pData);
if (!nBytes || !pData)
  return FALSE;

BOOL bRes = rgn.CreateFromData( NULL, nBytes, pData );
BYTE* pBytes = (BYTES*)pData;
delete[] pBytes;
return TRUE;
}

void StoreRegionData(RGNDATA* pData, int nBytes)
{
CFile myFile;
if (myFile.Open("Test.rgn", CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
 {
  // write mark that Region data stored here!
    char szBuffer[4] = "rgn";
    myFile.Write(szBuffer, sizeof(szBuffer));
    // store number of bytes
    myFile.Write((LPSTR)&nBytes, sizeof(nBytes));
   // and data
    myFile.Write( pData, nBytes);
 }
myFile.Close();
}

int ReadRegionData(RGNDATA** pData)
{
CFile myFile;
*pData = NULL;

if (myFile.Open("Test.rgn", CFile::modeRead|CFile::typeBinary))
 {
  // write mark that Region data stored here!
    char szBuffer[4] = "";
    myFile.Read(szBuffer, sizeof(szBuffer));
    if (!lstrcmp(szBuffer, "rgn"))
       {
        int nBytes = 0;
        myFile.Read((LPSTR)&nBytes, sizeof(nBytes));
        *pData = new BYTE[nBytes];
        if (*pData)
          {
            myFile.Read( pData, nBytes);
            myFile.Close();
            return nBytes;
          }
       }
 }
myFile.Close();
return 0;
}


0
 
LVL 1

Author Comment

by:mite51
ID: 1324018
That was good, but it didn't seem to work (i had to fix some syntax to get it to compile). I don't think its too much to ask for you to debug it. I have the program I'm using this on my web site. In that program the main window class is CTestDlg, the function SetupBk sets up a CRgn for the window based on a color.
It should load the CRgn data from a file, if the file does not exist then it creates the file. If you get it to work I will give you the points and post your name for the work you did.

Jason
the site -- http://www.geocities.com/ResearchTriangle/Lab/5376/Spells/TransDialog/index.html
0
 
LVL 1

Author Comment

by:mite51
ID: 1324019
hello?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 12

Expert Comment

by:migel
ID: 1324020
Excuse me. I am not compile my examples (I give only idea, not worked program), so ... ;-).
What you mean when say: <...if the file does not exist then it creates the file...>? What region must  be created (simple or more complex)?
0
 
LVL 1

Author Comment

by:mite51
ID: 1324021
I could make psuedo code too. The reason I posted this question for 400 points was because I couldn't get the proper systax, I'm not looking for theory, I need a working example. I'm sorry, but if you can't get the above code to work I can't give you the points.
0
 
LVL 1

Author Comment

by:mite51
ID: 1324022
I'm not sure exactly what wrong but it seems to have a problem allocating the memory and the region is pretty complex so I think may be a source or problems.
0
 
LVL 1

Author Comment

by:mite51
ID: 1324023
I got it to work after screwing with it for a while. I'm going to post the code on my site if you want to take a look. Incase you want to know, it didn't like assiging the memory to a double pointer, so I made it global (ya ya) so I never had to pass the double pointer. I'll make it into a CRgn derived class later.
0
 
LVL 1

Author Comment

by:mite51
ID: 1324024
you can reanswer the question and I'll give you the points
0
 
LVL 12

Accepted Solution

by:
migel earned 400 total points
ID: 1324025
Hi! Sorry, I had a problem with internet connection, so i can`t send checked code for you.
(I think that global pointer is not good idea, but if it`s work...)
Here is my checked code:

#include "stdafx.h"

void StoreRegionData(RGNDATA* pData, int nBytes);
int ReadRegionData(RGNDATA** pData);

void StoreRegion(CRgn& rgn)
     {
     RGNDATA* pData = NULL;
     BYTE* pBytes = NULL;
     int nBytes = rgn.GetRegionData(NULL, 0);

     nBytes += sizeof(RGNDATA); // I`m not shure (?!), but for safety...
     pBytes = new BYTE[nBytes ];

     if (!pBytes) // not enough memory!
       return;
     pData  = (RGNDATA*)pBytes;

     if (rgn.GetRegionData(pData, nBytes) == ERROR)
       {
        delete[] pBytes;
        return; // something wrong
       }
     // ok we have data now
     // store data
     StoreRegionData(pData, nBytes);
     delete[] pBytes;
     return;
     }

BOOL CreateRegionFromFile(CRgn& rgn)
     {
     RGNDATA* pData = NULL;
     int nBytes = ReadRegionData(&pData);
     if (!nBytes || !pData)
       return FALSE;

     BOOL bRes = rgn.CreateFromData( NULL, nBytes, pData );
     BYTE* pBytes = (BYTE*) pData;
     delete[] pBytes;
     return TRUE;
     }

     void StoreRegionData(RGNDATA* pData, int nBytes)
     {
     CFile myFile;
     if (myFile.Open("Test.rgn", CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
      {
       // write mark that Region data stored here!
         char szBuffer[4] = "rgn";
         myFile.Write(szBuffer, sizeof(szBuffer));
         // store number of bytes
         myFile.Write((LPSTR)&nBytes, sizeof(nBytes));
        // and data
         myFile.Write( pData, nBytes);
      }
     myFile.Close();
     }

int ReadRegionData(RGNDATA** pData)
     {
     CFile myFile;
     *pData = NULL;

     if (myFile.Open("Test.rgn", CFile::modeRead|CFile::typeBinary))
      {
       // write mark that Region data stored here!
         char szBuffer[4] = "";
         myFile.Read(szBuffer, sizeof(szBuffer));
         if (!lstrcmp(szBuffer, "rgn"))
            {
             int nBytes = 0;
             myFile.Read((LPSTR)&nBytes, sizeof(nBytes));
             *pData = (RGNDATA*)new BYTE[nBytes];
             if (*pData)
               {
                 myFile.Read( pData, nBytes);
                 myFile.Close();
                 return nBytes;
               }
            }
      }
     myFile.Close();
     return 0;
     }
0
 
LVL 1

Author Comment

by:mite51
ID: 1324026
You get lots of points now :)


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question