?
Solved

CRgn::GetRegionData

Posted on 1998-10-29
10
Medium Priority
?
426 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 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