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

📄 viewerlib.cpp

📁 图像显示软件源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    pBitmapInfo->bmiHeader.biPlanes        = 1;
    pBitmapInfo->bmiHeader.biBitCount      = Image.BitsPixel();
    pBitmapInfo->bmiHeader.biCompression   = BI_RGB;
    pBitmapInfo->bmiHeader.biSizeImage     = BitsSize;

    DataInsertionPointer += InfoSize;

    if (ColourTableSize)
    {   // If the bitmap has a colour table, it goes into the Databuffer fifth
        GetDIBColorTable(ImageDC, 0, 1 << Image.BitsPixel(), reinterpret_cast<RGBQUAD *>(DataInsertionPointer));
        DataInsertionPointer += ColourTableSize;
    }

    // The bitmap data bits go into the Databuffer last
    if (!GetDIBits(ImageDC, Image, 0, Image.Height(), DataInsertionPointer, pBitmapInfo, DIB_RGB_COLORS))
    {
        ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - GetDIBits() failed to get the bits from the DIB\n"));
        ReturnCode = ERROR_FUNCTION_FAILED;
    }
    else
    {
        // setup the COPYDATASTRUCT
        COPYDATASTRUCT CopyDataStruct;
        CopyDataStruct.dwData = 0xFEB11965;
        CopyDataStruct.cbData = DataBufferSize;
        CopyDataStruct.lpData = DataBuffer;

        if (IsWindow(Window))
        {   // Viewer window active, so send it the bitmap data
            LRESULT retval = SendMessageTimeout(Window,
                                                WM_COPYDATA,
                                                NULL,
                                                reinterpret_cast<LPARAM>(&CopyDataStruct),
                                                SMTO_BLOCK | SMTO_ABORTIFHUNG,
                                                200,
                                                reinterpret_cast<PDWORD_PTR>(&ReturnCode));
            if (!retval)
            {
                DWORD LastError = GetLastError();
                ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - SendMessageTimeout() failed to ")
                                  _T("send the WM_COPYDATA message to the Image Viewer\n"));
                if (0 == LastError || ERROR_TIMEOUT == LastError)
                {
                    ATLTRACE(_T("                                      SendMessageTimeout timed out.\n"));
                    ReturnCode = ERROR_TIMEOUT;
                }
                else
                {
                    ATLTRACE(_T("                                      GetLastError() returned : 0x%08X\n"), LastError);
                    ReturnCode = ERROR_FUNCTION_FAILED;
                }
            }
            else if (ERROR_SUCCESS != ReturnCode)
            {
                ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - Bitmap not accepted by the Image Viewer\n"));
                if (ERROR_INVALID_DATA == ReturnCode)
                {
                    ATLTRACE(_T("                                      CopyDataStruct corrupted\n"));
                    ReturnCode = ERROR_FUNCTION_FAILED;
                }
            }
        }
        else
        {
            ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - Image Viewer not running\n"));
            ReturnCode = ERROR_NOT_READY;
        }
    }

    delete[] DataBuffer;
    return ReturnCode;
}

/////////////////////////////////////////////////////////////////////////////
//
//  CalculateRect
//    This function is used to calculate the rectangle needed to contain
//    a rotated image. Currently it is only called by the FontViewW function
//    when the font is supposed to be drawn at an angle other than 0 degrees.
//
//    This code was adapted from the "Image Rotation in .NET" article written
//    by James T. Johnson at http://www.codeproject.com/csharp/rotateimage.asp
//
/////////////////////////////////////////////////////////////////////////////

void CalculateRect(RECT &rc, long Angle, POINT &Offset)
{
    const double pi = 3.1415926535897932384626433832795; // from windows calculator
    const double pi2 = pi / 2.0;

    double OldWidth = double(rc.right - rc.left);
    double OldHeight = double(rc.bottom - rc.top);

    Angle = Angle % 3600;

    double Theta = (Angle / 10.0) * pi / 180.0;
    double LockedTheta = Theta;

    while (LockedTheta < 0.0)
    {
        LockedTheta += 2 * pi;
    }

    double aTop, oTop;
    double aBottom, oBottom;

    if ((LockedTheta >= 0.0 && LockedTheta <= pi2) ||
        (LockedTheta >= pi && LockedTheta < (pi + pi2)))
    {
        aTop = fabs(cos(LockedTheta)) * OldWidth;
        oTop = fabs(sin(LockedTheta)) * OldWidth;

        aBottom = fabs(cos(LockedTheta)) * OldHeight;
        oBottom = fabs(sin(LockedTheta)) * OldHeight;
    }
    else
    {
        aTop = fabs(sin(LockedTheta)) * OldWidth;
        oTop = fabs(cos(LockedTheta)) * OldWidth;

        aBottom = fabs(sin(LockedTheta)) * OldHeight;
        oBottom = fabs(cos(LockedTheta)) * OldHeight;
    }

    double NewWidth = aTop + oBottom;
    double NewHeight = oTop + aBottom;

    long nWidth = long(ceil(NewWidth));
    long nHeight = long(ceil(NewHeight));

    if (LockedTheta >= 0.0 && LockedTheta <= pi2)
    {
        rc.right = rc.left + nWidth;
        rc.bottom = rc.top + nHeight;
        Offset.y += long(oTop);
    }
    else if (LockedTheta > pi2 && LockedTheta < pi)
    {
        rc.right = rc.left + nHeight;
        rc.bottom = rc.top + nWidth;
        Offset.y += nWidth;
        Offset.x += long(oTop);
    }
    else if (LockedTheta >= pi && LockedTheta < pi + pi2)
    {
        rc.right = rc.left + nWidth;
        rc.bottom = rc.top + nHeight;
        Offset.x += nWidth;
        Offset.y += long(aBottom);
    }
    else
    {
        rc.right = rc.left + nHeight;
        rc.bottom = rc.top + nWidth;
        Offset.x += long(aBottom);
    }
}

#ifdef __cplusplus
extern "C" {
#endif

/////////////////////////////////////////////////////////////////////////////
//
// GetBGBitmap
//   Returns a HBITMAP of the required size, filled with the background colour
//
//  Parameters :
//    Width  [in] - The required width of the bitmap.
//    Height [in] - The required height of the bitmap.
//
//  Returns :
//    An HBITMAP handle to the new bitmap
//
//  Note :
//    Call DeleteObject on the bitmap when you are done with it
//
/////////////////////////////////////////////////////////////////////////////

__declspec(dllexport) HBITMAP GetBGBitmap(UINT Width, UINT Height)
{
    HBITMAP hBitmap = CreateCompatibleBitmap(pja::CScreenDC(), Width, Height);
    pja::CCompatibleDC DC;
    SelectObject(DC, hBitmap);
    RECT rc = {0, 0, Width, Height};
    FillRect(DC, &rc, BGBrush());

    return hBitmap;
}

/////////////////////////////////////////////////////////////////////////////
//
//  BitmapView
//    Calls SendImageToViewer to send the supplied bitmap to the ImageViewer
//
//  Parameters :
//    TheBitmap [in] - The bitmap to send
//    File      [in] - The complete path to the source file that called this function
//    Line      [in] - The line number in the source file from where the call was made
//    Function  [in] - The name of the function from where the call was made
//    Text      [in] - User defined text
//
//  Returns :
//    ERROR_SUCCESS if the image was sent to the viewer, a Win32 error code otherwise
//
//  Note :
//    There are two versions of this function, BitmapViewA is the ANSI version
//    and BitmapViewW is the UNICODE version
//
/////////////////////////////////////////////////////////////////////////////

__declspec(dllexport) LRESULT BitmapViewW (HBITMAP TheBitmap,
                                           LPCWSTR File,
                                           UINT Line,
                                           LPCWSTR Function,
                                           LPCWSTR Text)
{
    return SendImageToViewer(TheBitmap, File, Line, Function, Text, NULL);
}

__declspec(dllexport) LRESULT BitmapViewA (HBITMAP TheBitmap,
                                           LPCSTR File,
                                           UINT Line,
                                           LPCSTR Function,
                                           LPCSTR Text)
{
    return SendImageToViewer(TheBitmap, CA2W(File), Line, CA2W(Function), CA2W(Text), NULL);
}

/////////////////////////////////////////////////////////////////////////////
//
//  IconView
//    Convert the supplied HICON to a HBITMAP with the AND mask on the right,
//    the XOR mask in the middle, and the icon on the left. Then pass the newly
//    created HBITMAP onto the BitmapView function which sends it to the ViewerApp.
//
//  Parameters :
//    TheIcon  [in] - The icon to send
//    File     [in] - The complete path to the source file that called this function
//    Line     [in] - The line number in the source file from where the call was made
//    Function [in] - The name of the function from where the call was made
//    Text     [in] - User defined text
//
//  Returns :
//    ERROR_SUCCESS if the icon was sent to the viewer, a Win32 error code otherwise
//
//  Note :
//    There are two versions of this function, IconViewA is the ANSI version
//    and IconViewW is the UNICODE version
//
/////////////////////////////////////////////////////////////////////////////

__declspec(dllexport) LRESULT IconViewW (HICON TheIcon,
                                         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 (!TheIcon)
    {
        ATLTRACE(_T("ImageViewer.dll : IconViewW - A NULL HICON handle was passed in\n"));
        return ERROR_INVALID_HANDLE;
    }

    ICONINFO info = {0};
    if (!GetIconInfo(TheIcon, &info))
    {
        ATLTRACE(_T("ImageViewer.dll : IconViewW - GetIconInfo() failed to get information from supplied HICON\n"));
        return GetLastError();
    }

    pja::CBitmap Colour(info.hbmColor);
    pja::CBitmap Mask(info.hbmMask);
    if (NULL == Mask)
    {
        ATLTRACE(_T("ImageViewer.dll : IconViewW - Failed to initialize mask\n"));
        return ERROR_FUNCTION_FAILED;
    }

    pja::CCompatibleDC MaskAndColourDC(NULL);
    if (NULL == MaskAndColourDC)
    {
        ATLTRACE(_T("ImageViewer.dll : IconViewW - Failed to initialize first memory dc\n"));
        return ERROR_FUNCTION_FAILED;
    }

    int width = 0;
    int height = 0;

    if (NULL != Colour)
    {
        SelectObject(MaskAndColourDC, Colour);
        height = Colour.Height();
        width = Colour.Width() * 3;
    }
    else
    {
        SelectObject(MaskAndColourDC, Mask);
        height = Mask.Height() / 2;
        width = Mask.Width() * 3;
    }

    // Create the bitmap that gets displayed in the Image Viewer application
    pja::CBitmap Image(MaskAndColourDC, width, height);
    if (NULL == Image)
    {
        ATLTRACE(_T("ImageViewer.dll : IconViewW - 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 : IconViewW - Failed to create memory dc\n"));
        return ERROR_FUNCTION_FAILED;
    }

⌨️ 快捷键说明

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