⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewerlib.cpp

📁 图像显示软件源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//    and FontViewW is the UNICODE version
//
//    This function works best when the font's orientation and escapement are both set
//    to zero. When the orientation and escapement are the same but not zero then there
//    is the possiblity of some clipping occurring. When the orientation and escapement
//    are different then all bets are off.
//
/////////////////////////////////////////////////////////////////////////////

__declspec(dllexport) LRESULT FontViewW(HFONT TheFont,
                                        LPCWSTR Sample,
                                        LPCWSTR File,
                                        UINT Line,
                                        LPCWSTR Function,
                                        LPCWSTR Text)
{
    // Check that the windows are properly setup
    if (!AreWindowsReady())
    {
        return ERROR_NOT_READY;
    }

    // Validate the parameters
    if (!TheFont)
    {
        ATLTRACE(_T("ImageViewer.dll : FontViewW - A NULL HFONT handle was passed in\n"));
        return ERROR_INVALID_HANDLE;
    }

    LOGFONT LogFont = {0};
    if (0 == GetObject(TheFont, sizeof(LOGFONT), &LogFont))
    {
        ATLTRACE(_T("ImageViewer.dll : FontViewW - Failed to get the LOGFONT structure from the HFONT handle\n"));
        return ERROR_FUNCTION_FAILED;
    }

    // Add the values of the LOGFONT structure onto the end of the Text string parameter
    typedef std::map<BYTE, LPCTSTR> LogFontData;

    LogFontData CharSets;
    CharSets[ANSI_CHARSET]          = _T("ANSI_CHARSET");
    CharSets[BALTIC_CHARSET]        = _T("BALTIC_CHARSET");
    CharSets[CHINESEBIG5_CHARSET]   = _T("CHINESEBIG5_CHARSET");
    CharSets[DEFAULT_CHARSET]       = _T("DEFAULT_CHARSET");
    CharSets[EASTEUROPE_CHARSET]    = _T("EASTEUROPE_CHARSET");
    CharSets[GB2312_CHARSET]        = _T("GB2312_CHARSET");
    CharSets[GREEK_CHARSET]         = _T("GREEK_CHARSET");
    CharSets[HANGUL_CHARSET]        = _T("HANGUL_CHARSET");
    CharSets[MAC_CHARSET]           = _T("MAC_CHARSET");
    CharSets[OEM_CHARSET]           = _T("OEM_CHARSET");
    CharSets[RUSSIAN_CHARSET]       = _T("RUSSIAN_CHARSET");
    CharSets[SHIFTJIS_CHARSET]      = _T("SHIFTJIS_CHARSET");
    CharSets[SYMBOL_CHARSET]        = _T("SYMBOL_CHARSET");
    CharSets[TURKISH_CHARSET]       = _T("TURKISH_CHARSET");
    CharSets[VIETNAMESE_CHARSET]    = _T("VIETNAMESE_CHARSET");
    CharSets[JOHAB_CHARSET]         = _T("JOHAB_CHARSET");
    CharSets[ARABIC_CHARSET]        = _T("ARABIC_CHARSET");
    CharSets[HEBREW_CHARSET]        = _T("HEBREW_CHARSET");
    CharSets[THAI_CHARSET]          = _T("THAI_CHARSET");

    LogFontData OutPrecisions;
    OutPrecisions[OUT_CHARACTER_PRECIS] = _T("OUT_CHARACTER_PRECIS");
    OutPrecisions[OUT_DEFAULT_PRECIS]   = _T("OUT_DEFAULT_PRECIS");
    OutPrecisions[OUT_DEVICE_PRECIS]    = _T("OUT_DEVICE_PRECIS");
    OutPrecisions[OUT_OUTLINE_PRECIS]   = _T("OUT_OUTLINE_PRECIS");
    OutPrecisions[OUT_PS_ONLY_PRECIS]   = _T("OUT_PS_ONLY_PRECIS");
    OutPrecisions[OUT_RASTER_PRECIS]    = _T("OUT_RASTER_PRECIS");
    OutPrecisions[OUT_STRING_PRECIS]    = _T("OUT_STRING_PRECIS");
    OutPrecisions[OUT_STROKE_PRECIS]    = _T("OUT_STROKE_PRECIS");
    OutPrecisions[OUT_TT_ONLY_PRECIS]   = _T("OUT_TT_ONLY_PRECIS");
    OutPrecisions[OUT_TT_PRECIS]        = _T("OUT_TT_PRECIS");

    LogFontData ClipPrecisions;
    ClipPrecisions[CLIP_CHARACTER_PRECIS]   = _T("CLIP_CHARACTER_PRECIS");
    ClipPrecisions[CLIP_DEFAULT_PRECIS]     = _T("CLIP_DEFAULT_PRECIS");
    ClipPrecisions[CLIP_DFA_DISABLE]        = _T("CLIP_DFA_DISABLE");
    ClipPrecisions[CLIP_EMBEDDED]           = _T("CLIP_EMBEDDED");
    ClipPrecisions[CLIP_LH_ANGLES]          = _T("CLIP_LH_ANGLES");
    ClipPrecisions[CLIP_MASK]               = _T("CLIP_MASK");
    ClipPrecisions[CLIP_STROKE_PRECIS]      = _T("CLIP_STROKE_PRECIS");
    ClipPrecisions[CLIP_TT_ALWAYS]          = _T("CLIP_TT_ALWAYS");

    LogFontData Qualities;
    Qualities[ANTIALIASED_QUALITY]          = _T("ANTIALIASED_QUALITY");
    Qualities[CLEARTYPE_QUALITY]            = _T("CLEARTYPE_QUALITY");
    Qualities[CLEARTYPE_NATURAL_QUALITY]    = _T("CLEARTYPE_NATURAL_QUALITY");
    Qualities[DEFAULT_QUALITY]              = _T("DEFAULT_QUALITY");
    Qualities[DRAFT_QUALITY]                = _T("DRAFT_QUALITY");
    Qualities[NONANTIALIASED_QUALITY]       = _T("NONANTIALIASED_QUALITY");
    Qualities[PROOF_QUALITY]                = _T("PROOF_QUALITY");

    LogFontData Pitches;
    Pitches[DEFAULT_PITCH]  = _T("DEFAULT_PITCH");
    Pitches[FIXED_PITCH]    = _T("FIXED_PITCH");
    Pitches[VARIABLE_PITCH] = _T("VARIABLE_PITCH");

    LogFontData Families;
    Families[FF_DECORATIVE] = _T("FF_DECORATIVE");
    Families[FF_DONTCARE]   = _T("FF_DONTCARE");
    Families[FF_MODERN]     = _T("FF_MODERN");
    Families[FF_ROMAN]      = _T("FF_ROMAN");
    Families[FF_SCRIPT]     = _T("FF_SCRIPT");
    Families[FF_SWISS]      = _T("FF_SWISS");

    std::tstring TextString;
    if (NULL != Text && 0 != wcslen(Text))
    {
        TextString = CW2T(Text);
        TextString += _T("/n/n");
    }

    pja::CFormat cf;
    TextString += cf.MakeMessage(DllModule, IDS_LOGFONT1, 8,
                                 cf.TS(LogFont.lfHeight),
                                 cf.TS(LogFont.lfWidth),
                                 cf.TS(LogFont.lfEscapement),
                                 cf.TS(LogFont.lfOrientation),
                                 cf.TS(LogFont.lfWeight),
                                 cf.TS(LogFont.lfItalic ? _T("TRUE") : _T("FALSE")),
                                 cf.TS(LogFont.lfUnderline ? _T("TRUE") : _T("FALSE")),
                                 cf.TS(LogFont.lfStrikeOut ? _T("TRUE") : _T("FALSE")));

    TextString += cf.MakeMessage(DllModule, IDS_LOGFONT2, 7,
                                 cf.TS((*CharSets.find(LogFont.lfCharSet)).second),
                                 cf.TS((*OutPrecisions.find(LogFont.lfOutPrecision)).second),
                                 cf.TS((*ClipPrecisions.find(LogFont.lfClipPrecision)).second),
                                 cf.TS((*Qualities.find(LogFont.lfQuality)).second),
                                 cf.TS((*Pitches.find(LogFont.lfPitchAndFamily & 0x03)).second),
                                 cf.TS((*Families.find(LogFont.lfPitchAndFamily & 0xF0)).second),
                                 cf.TS(LogFont.lfFaceName));

    // Set the text used to display the font on the sample image
    std::tstring ImageText = _T("The quick brown fox jumps over the lazy dog. 1234567890");

    if (NULL != Sample && 0 != _tcslen(Sample))
    {
        std::tstring SampleText = Sample;
        std::tstring::size_type pos = SampleText.find_first_of(_T("\r\n"));
        if (pos != std::tstring::npos)
        {
            SampleText = SampleText.substr(0, pos);
        }

        if (SampleText.length() > 0)
        {
            ImageText = SampleText;
        }
    }

    // Create the bitmap that gets displayed in the Image Viewer application
    pja::CBitmap ImageBitmap;

    // Create the memory device context used to draw onto the bitmap
    pja::CCompatibleDC ImageDC(NULL);
    if (NULL == ImageDC)
    {
        ATLTRACE(_T("ImageViewer.dll : FontViewW - Failed to create memory dc\n"));
        return ERROR_FUNCTION_FAILED;
    }

    // Get the bitmap ready for display
    SelectObject(ImageDC, TheFont);

    RECT ImageRect = {0};

    // get the required height of the bitmap
    TEXTMETRIC tm = {0};
    GetTextMetrics(ImageDC, &tm);
    ImageRect.bottom = tm.tmHeight;

    // Calculate the required width of the bitmap. Some characters in some fonts
    // have overhangs and/or underhangs so large that the second or even third
    // character from either end of the text can affect the size of the bitmap
    // needed to display the text.
    ABCFLOAT ABCWidths = {0};
    long left = 0;
    long right = 0;
    long pos = 0;

    for (std::tstring::iterator it = ImageText.begin(); it != ImageText.end(); ++it)
    {
        if (GetCharABCWidthsFloat(ImageDC, *(it), *(it), &ABCWidths))
        {
            pos += long(ABCWidths.abcfA);
            left = min(left, pos);
            pos += long(ABCWidths.abcfB);
            right = max(right, pos);
            pos += long(ABCWidths.abcfC);
        }
    }

    ImageRect.right = right - left;

    POINT Offset = {0};
    if (LogFont.lfEscapement % 3600 != 0)
    {
        CalculateRect(ImageRect, LogFont.lfEscapement, Offset);
    }

    // create the bitmap
    ImageBitmap = pja::CBitmap(NULL, ImageRect.right, ImageRect.bottom);
    if (NULL == ImageBitmap)
    {
        ATLTRACE(_T("ImageViewer.dll : FontViewW - Failed to create bitmap\n"));
        return ERROR_FUNCTION_FAILED;
    }

    // fill the backgound colour and draw the text on the bitmap
    SelectObject(ImageDC, ImageBitmap);

    BGBrush Background;
    FillRect(ImageDC, &ImageRect, Background);
    SetTextColor(ImageDC, Background);
    SetBkMode(ImageDC, TRANSPARENT);
    if (LogFont.lfEscapement % 3600 == 0)
    {
        // do not clip any underhang
        ImageRect.left -= left;
    }
    else
    {
        // adjust the starting point of the text so the text is visible
        ImageRect.left += Offset.x;
        ImageRect.top += Offset.y;
    }

    if (LogFont.lfEscapement != LogFont.lfOrientation)
    {
        SetGraphicsMode(ImageDC, GM_ADVANCED);
    }

    DrawText(ImageDC, ImageText.c_str(), -1, &ImageRect, DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP);

    // Call BitmapViewW to send the bitmap to the Image Viewer
    return BitmapViewW(ImageBitmap, File, Line, Function, CT2W(TextString.c_str()));
}

__declspec(dllexport) LRESULT FontViewA (HFONT TheFont,
                                         LPCSTR Sample,
                                         LPCSTR File,
                                         UINT Line,
                                         LPCSTR Function,
                                         LPCSTR Text)
{
    return FontViewW(TheFont, CA2W(Sample), CA2W(File), Line, CA2W(Function), CA2W(Text));
}

#ifdef __cplusplus
}
#endif

/////////////////////////////////////////////////////////////////////////////
//
//  END OF VIEWERLIB.CPP
//
/////////////////////////////////////////////////////////////////////////////



/****************************************************************************

// This is a basic function template for adding a new "View" function to the
// library. Simply change the "<function name>", "<class name>", and "<object name>"
// texts in the template code below to something meaningful, and add the code
// needed where ever there is a line that starts with "// TODO:"

__declspec(dllexport) LRESULT <function name>ViewW(<class name> <object name>,
                                                   LPCWSTR File,
                                                   UINT Line,
                                                   LPCWSTR Function,
                                                   LPCWSTR Text)
{
    // Check that the windows are properly setup
    if (!AreWindowsReady())
    {
        return ERROR_NOT_READY;
    }

    // Validate the parameters

    // TODO: Add code here to validate the parameters passed into this function

    // Create the bitmap that gets displayed in the Image Viewer application
    pja::CBitmap Image;

    // TODO: Add code to initialize Image. May include changing the c'tor line above

    if (NULL == Image)
    {
        ATLTRACE(_T("ImageViewer.dll : <function name>ViewW - Failed to create bitmap\n"));
        return ERROR_FUNCTION_FAILED;
    }

    // Create the memory device context used to draw onto the bitmap
    pja::CCompatibleDC ImageDC(NULL);
    if (NULL == ImageDC)
    {
        ATLTRACE(_T("ImageViewer.dll : <function name>ViewW - Failed to create memory dc\n"));
        return ERROR_FUNCTION_FAILED;
    }

    SelectObject(ImageDC, Image); 

    // Get the bitmap ready for display

    // TODO: Add drawing code here that displays the object the way you want. Use ImageDC as the device context.

    // Call BitmapViewW to send the bitmap to the Image Viewer
    return BitmapViewW(Image, File, Line, Function, Text);
}

__declspec(dllexport) LRESULT <function name>ViewA (<class name> <object name>,
                                                    LPCSTR File,
                                                    UINT Line,
                                                    LPCSTR Function,
                                                    LPCSTR Text)
{
    return <function name>ViewW(<object name>, CA2W(File), Line, CA2W(Function), CA2W(Text));
}

****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -