Solved

CRgn::GetRegionData

Posted on 1998-10-29
10
373 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now