Solved

CRgn::GetRegionData

Posted on 1998-10-29
10
374 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now