Windows Enhanced Metafile Pen vs Windows Metafile

446 views Asked by At

I'm working on a system that have huge database of metafiles (the old win3.0 format), and I need to convert that to Enhanced Metafile. I did convert the whole database, and manage to play the files to the display as it should be, except one thing:

  • The old metafiles use the current pen/brush to play the file to the screen.

  • On the other hand the enhanced metafiles need the pen to be "save"
    inside the file, and I didn't manage to change it in runtime.

I need a way to change the enhanced metafile pen, in runtime, so one metafile can be played in different pens depending on the application running.

1

There are 1 answers

0
rafaelxy On

I found a way.

Thanks to Grey Wolf http://www.cplusplus.com/user/z05DSL3A/

Follow the solution (written by GreyWolf): I have a class (somewhere) than handles EMFs as grey scales, as I remember you have to supply a callback function can on the fly replace the colour values with appropriate grey levels. I'll see if I can find the code (but it will not be until the weekend...as long as I remember).

Another thing you could try is using a generic postscript drive (from Adobe).

-== Edit ==-

To use the code below create a CGrayEMF object and call its EnumEMF() method. I don't think that it converts embedded bitmaps, but that should be able to be added.

class CEnumEMF
{
    // virtual function to process every EMF record, return 0 to terminate
    virtual int ProcessRecord(HDC hDC, HANDLETABLE * pHTable, const ENHMETARECORD * pEMFR, int nObj)
    {
        return 0;
    }

    // static callback function, dispatch to virtual function ProcessRecord
    static int CALLBACK EMFProc(HDC hDC, HANDLETABLE * pHTable, 
        const ENHMETARECORD * pEMFR, int nObj, LPARAM lpData)
    {
        CEnumEMF * pObj = (CEnumEMF *) lpData;

        if ( IsBadWritePtr(pObj, sizeof(CEnumEMF)) )
        {
            assert(false);
            return 0;
        }

        return pObj->ProcessRecord(hDC, pHTable, pEMFR, nObj);
    }

public:

    BOOL EnumEMF(HDC hDC, HENHMETAFILE hemf, const RECT * lpRect)
    {
        return ::EnumEnhMetaFile(hDC, hemf, EMFProc, this, lpRect);
    }
};

inline void MaptoGray(COLORREF & cr)
{
    if ( (cr & 0xFF000000) != PALETTEINDEX(0) ) // not paletteindex
    {
        BYTE gray = ( GetRValue(cr) * 77 + GetGValue(cr) * 150 + GetBValue(cr) * 29 + 128 ) / 256;

        cr = (cr & 0xFF000000) | RGB(gray, gray, gray);
    }
}


class CGrayEMF : public CEnumEMF
{
    // virtual function to process every EMF record, return 0 to terminate
    virtual int ProcessRecord(HDC hDC, HANDLETABLE * pHTable, const ENHMETARECORD * pEMFR, int nObj)
    {
        int rslt;

        switch ( pEMFR->iType )
        {
            case EMR_CREATEBRUSHINDIRECT:
                {
                    EMRCREATEBRUSHINDIRECT cbi;

                    cbi = * (const EMRCREATEBRUSHINDIRECT *) pEMFR;
                    MaptoGray(cbi.lb.lbColor);

                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & cbi, nObj);
                }
                break;

            case EMR_CREATEPEN:
                {
                    EMRCREATEPEN cp;

                    cp = * (const EMRCREATEPEN *) pEMFR;
                    MaptoGray(cp.lopn.lopnColor);

                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & cp, nObj);
                }
                break;

            case EMR_SETTEXTCOLOR:
            case EMR_SETBKCOLOR:
                {
                    EMRSETTEXTCOLOR stc;

                    stc = * (const EMRSETTEXTCOLOR *) pEMFR;
                    MaptoGray(stc.crColor);

                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & stc, nObj);
                }
                break;

            case EMR_RESERVED_105:
            case EMR_RESERVED_106:
            case EMR_RESERVED_107:
            case EMR_RESERVED_108:
            case EMR_RESERVED_109:
            case EMR_RESERVED_110:
            case EMR_RESERVED_119:
            case EMR_RESERVED_120:
                rslt = PlayEnhMetaFileRecord(hDC, pHTable, pEMFR, nObj);
                break;

            default:
                rslt = PlayEnhMetaFileRecord(hDC, pHTable, pEMFR, nObj);
        }

        return rslt;
    }
};