Figure 2   RandRect

RandRect.h

////////////////////////////////////////////////////////////////
// RANDRECT 1998 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// See RANDRECT.CPP for Description of program.
// 
#include "resource.h"       // main symbols

class CMyApp : public CWinApp {
public:
    DECLARE_DYNAMIC(CMyApp)
    CMyApp();

    //{{AFX_VIRTUAL(CMyApp)
    public:
    virtual BOOL OnIdle(LONG lCount);
    virtual BOOL InitInstance();
    //}}AFX_VIRTUAL

protected:
    //{{AFX_MSG(CMyApp)
    afx_msg void OnAppAbout();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};
RandRect.cpp
////////////////////////////////////////////////////////////////
// RANDRECT 1998 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// RANDRECT illustrates how to use CS_OWNDC in an MFC app.
// Compiles with Visual C++ 5.0 or later, under Windows 95.

#include "StdAfx.h"
#include "RandRect.h"
#include "MainFrm.h"
#include "Doc.h"
#include "View.h"
#include "TraceWin.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
    //{{AFX_MSG_MAP(CMyApp)
    ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
    //}}AFX_MSG_MAP
    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

IMPLEMENT_DYNAMIC(CMyApp, CWinApp)

CMyApp::CMyApp()
{
}

CMyApp theApp;

BOOL CMyApp::InitInstance()
{
//  SetRegistryKey("MSJ");      // Save settings in registry "MSJ", not INI file
//  LoadStdProfileSettings();   // Load standard INI file options (including MRU)

    PxlTraceInit(); // initialize TraceWin tracing (see TraceWin.h)

    CSingleDocTemplate* pDocTemplate;
    pDocTemplate = new CSingleDocTemplate(
        IDR_MAINFRAME,
        RUNTIME_CLASS(CMyDoc),
        RUNTIME_CLASS(CMainFrame),       // main SDI frame window
        RUNTIME_CLASS(CMyView));
    AddDocTemplate(pDocTemplate);

    // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);

    // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
        return FALSE;

    return TRUE;
}

//////////////////
// Idle loop: draw random rectangles
//
BOOL CMyApp::OnIdle(LONG lCount)
{
    // Get view window
    CFrameWnd* pFrame = (CFrameWnd*)m_pMainWnd;
    ASSERT_KINDOF(CFrameWnd, pFrame);
    ASSERT_VALID(pFrame);
    CWnd* pWnd = pFrame->GetActiveView();
    ASSERT_VALID(pWnd);

    // get client area rectangle
    CRect rcClient;
    pWnd->GetClientRect(&rcClient);

    // create random rectangle
    CRect rc(
        rand() % rcClient.Width(),
        rand() % rcClient.Height(),
        rand() % rcClient.Width(),
        rand() % rcClient.Height());

    // Now paint the rectangle
    CClientDC dc(pWnd);
    CBrush b( RGB(rand()&255, rand()&255, rand()&255));
    CBrush* pOldBrush = dc.SelectObject(&b);
    dc.Rectangle(&rc);
    dc.SelectObject(pOldBrush);

    return TRUE;            // tell MFC to keep calling idle function
}

void CMyApp::OnAppAbout()
{
    CDialog(IDD_ABOUTBOX).DoModal();
}
View.h
////////////////
// Standard MFC view
//
class CMyView : public CView {
public:
    virtual ~CMyView();
    CMyDoc* GetDocument() { return (CMyDoc*)m_pDocument; }

    //{{AFX_VIRTUAL(CMyView)
    public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    //}}AFX_VIRTUAL

protected:
    DECLARE_DYNCREATE(CMyView)
    CMyView();

    //{{AFX_MSG(CMyView)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};
View.cpp
////////////////////////////////////////////////////////////////
// RANDRECT 1998 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
#include "StdAfx.h"
#include "RandRect.h"
#include "Doc.h"
#include "View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
    //{{AFX_MSG_MAP(CMyView)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMyView::CMyView()
{
}

CMyView::~CMyView()
{
}

void CMyView::OnDraw(CDC* pDC)
{
    CMyDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
}

const LPCTSTR MYCLASSNAME = _T("RandRectView");

//////////////////
// Handle pre-creation: register new class like MFC class,
// but with private DC style turned on.
//
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
    if (CView::PreCreateWindow(cs)) {
        static BOOL bRegistered = FALSE;        // flag for first-time init

        if (!bRegistered) {
            // This is the first time a view is being created.
            // Need to register the view class.
            //
            TRACE("CMyView::PreCreateWindow, MFC classname = %s\n",
                  cs.lpszClass);
            WNDCLASSEX wc;
            wc.cbSize = sizeof(WNDCLASSEX);

            // Get class information for MFC default view.
            // The classname is in cs.lpszClass
            GetClassInfoEx(AfxGetInstanceHandle(), cs.lpszClass, &wc);

            // Modify name and style
            wc.lpszClassName = MYCLASSNAME;
            wc.style |= CS_OWNDC;

            // Register new class
            VERIFY(RegisterClassEx(&wc));
        }
        cs.lpszClass = MYCLASSNAME;
        return TRUE;
    }
    return FALSE;
}


Figure 5   CMyView::PreCreateWindow


 const LPCTSTR MYCLASSNAME = _T("RandRectView");
 
 //////////////////
 // Handle pre-creation: register new class like MFC class,
 // but with private DC style turned on.
 //
 BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
 {
     if (CView::PreCreateWindow(cs)) {
         static BOOL bRegistered = FALSE;        // flag for first-time init
 
         if (!bRegistered) {
             // This is the first time a view is being created.
             // Need to register the view class.
             //
             TRACE("CMyView::PreCreateWindow, MFC classname = %s\n", 
                   cs.lpszClass);
             WNDCLASSEX wc;
             wc.cbSize = sizeof(WNDCLASSEX);
 
             // Get class information for MFC default view.
             // The classname is in cs.lpszClass
             GetClassInfoEx(AfxGetInstanceHandle(), cs.lpszClass, &wc);
 
             // Modify name and style
             wc.lpszClassName = MYCLASSNAME;
             wc.style |= CS_OWNDC;
 
             // Register new class
             VERIFY(RegisterClassEx(&wc));
         }
         cs.lpszClass = MYCLASSNAME;
         return TRUE;
     }
     return FALSE;
 }

Figure 6   Window Class Styles Used by MFC

Window Type Class Style Flags
Normal Child Window CS_DBLCLKS | CS_HREDRAW |
CS_VREDRAW
OLE Control CS_PARENTDC | CS_DBLCLKS |
CS_HREDRAW | CS_VREDRAW
Control Bar none (this implies no double-clicks)
MDI Frame CS_DBLCLKS
Frame or View CS_DBLCLKS | CS_HREDRAW |
CS_VREDRAW


Figure 11   FindRiff.cpp


 ////////////////////////////////////////////////////////////////
 // 1998 Microsoft Systems Journal. 
 // If this code works, it was written by Paul DiLascia.
 // If not, I don't know who wrote it.
 // 
 // This program extracts RIFF files (avi and wav) from an EXE or DLL.
 // To build: 
 //    cl findriff.cpp
 
 #include <stdio.h>
 #include "FileName.h"
 
 char buf[1024];
 
 const char RIFF[] = "RIFF";             // RIFF tag
 const char AVI [] = "AVI ";             // AVI tag
 const char WAVE[] = "WAVE";             // WAV tag
 
 //////////////////
 // This function opens a file and searches it for RIFF files
 //
 int FindRiff(const char* lpFileName)
 {
    FILE *fp = fopen(lpFileName, "rb");
    if (fp==NULL) {
       fprintf(stderr, "Couldn't open file '%s'\n", lpFileName);
       return 0;
    }
 
    int c;            // char read
    int nFound = 0;   // number of RIFF files found in this file
    int iMatch = 0;   // offset into RIFF[] of char I'm looking for next
    
    while ((c = fgetc(fp)) != EOF) {
 
       if (c == RIFF[iMatch]) {          // if match:
 
          if (++iMatch >= 4) {           // if all match:
 
             // found a RIFF file!
             long pos = ftell(fp);       // remember pos
 
             // read size as next four bytes, in proper order LSB to MSB
             unsigned int b1 = fgetc(fp);
             unsigned int b2 = fgetc(fp);
             unsigned int b3 = fgetc(fp);
             unsigned int b4 = fgetc(fp);
             unsigned long size = b1 | b2<<8 | b3<<16 | b4<<24;
 
             // read 4-character type
             char type[4];
             fread(type, 1, 4, fp);
 
             if (strncmp(type, AVI, 4)==0 || strncmp(type, WAVE, 4)==0) {
 
                // found "AVI " or "WAVE "
 
                // generate new filename
                char newfilename[16];
                static int uniqueid = 0;
 
                sprintf(newfilename, "FILE%04d.%s", uniqueid++,
                   strncmp(type, AVI, 4)==0 ? "AVI" : "WAV");
 
                // create new file
                FILE* fpNew = fopen(newfilename, "wb");
                if (fpNew==NULL) {
                   fprintf(stderr, "Error creating file %s\n", newfilename);
 
                } else {
                   // Copy from source to new file. Size of file is size of
                   // chunk data plus 8 = size of header (ID + size field)
                   // 
                   printf("Creating %s\n", newfilename);
                   fseek(fp, pos-4, SEEK_SET);
                   size += 8;
                   while (size-- > 0)
                      fputc(fgetc(fp), fpNew);
                   fclose(fpNew);
                   nFound++;
                }
             }
             iMatch = 0;
          }
       } else {
          iMatch = 0;
       }
    }
    fclose(fp);
    return nFound;
 }
 
 //////////////////
 // Main entry point
 //
 int main(int argc, char* argv[])
 {
    if (argc < 2) {
       printf("FindRiff written 10-20-97 by Paul Dilascia\n");
       printf("Purpose: to find RIFF (AVI/WAV) resources in EXEs and DLLs\n");
       printf("Usage:   FindRiff <filespec>\n");
       printf("         findriff will create new files in the current directory,\n");
       printf("         using the names FILE0000.AVI, FILE0001.AVI, etc.\n");
       return 1;
    }
 
    // Do for each file in the file spec
    int totalFound = 0;
    for (int i=1; i < argc; i++) {
       CFileSpec spec;
       int nProcessed = 0;
       for (BOOL bMore = spec.First(argv[i]); bMore; bMore=spec.Next()) {
          if ((spec.attrib & ~_A_ARCH)==0) {
             int nFound = FindRiff(spec.GetPathName());
             printf("%d files found in %s\n", nFound, spec.GetPathName());
             nProcessed++;
             totalFound += nFound;
          }
       }
       if (nProcessed==0) {
          fprintf(stderr, "No files found to match '%s' \n", argv[i]);
       }
    }
    if (totalFound > 0) {
       printf("Warning: it may be illegal to copy audio/video clips--check with vendor before using!\n");
    }
    return 0;
 }