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

📄 imagecls.cpp

📁 WIN32高级程序设计书附源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        return NumColors * sizeof(RGBTRIPLE);
    else
        return NumColors * sizeof(RGBQUAD);
}


//  ImageClass::DibNumColors
//  Return the number of colours in a DIB.

WORD ImageClass::DibNumColors( VOID FAR *pv )

{
    int                 bits;
    LPBITMAPINFOHEADER  lpbi;
    LPBITMAPCOREHEADER  lpbc;

    lpbi = ((LPBITMAPINFOHEADER)pv);
    lpbc = ((LPBITMAPCOREHEADER)pv);

    /*  With the BITMAPINFO format headers, the size of the palette
     *  is in biClrUsed, whereas in the BITMAPCORE - style headers, it
     *  is dependent on the bits per pixel ( = 2 raised to the power of
     *  bits/pixel).
     */
    if (lpbi->biSize != sizeof(BITMAPCOREHEADER)){
        if (lpbi->biClrUsed != 0)
            return (WORD)lpbi->biClrUsed;
        bits = lpbi->biBitCount;
    }
    else
        bits = lpbc->bcBitCount;

    switch (bits){
        case 1:
                return 2;
        case 4:
                return 16;
        case 8:
                return 256;
        default:
                /* A 24 bitcount DIB has no color table */
                return 0;
    }
}


//  ImageClass::lread
//  Private function to read a HUGE block of data in 32k chunks.

DWORD ImageClass::lread (int fh, VOID FAR *pv, DWORD ul )

{
    DWORD     ulT = ul;
    BYTE huge *hp = (BYTE huge *) pv;
    const WORD MAXREAD = 32768;         // Number of bytes to be read during

    while (ul > (DWORD)MAXREAD) {
        if (_lread(fh, (LPSTR)hp, (WORD)MAXREAD) != MAXREAD)
                return 0;
        ul -= MAXREAD;
        hp += MAXREAD;
    }
    if (_lread(fh, (LPSTR)hp, (WORD)ul) != (WORD)ul)
        return 0;
    return ulT;
}


//  ImageClass::BitmapFromDib
//  Create a device dependent bitmap (DDB) from a DIB.

HBITMAP ImageClass::BitmapFromDib ( HANDLE hdib, HPALETTE hpal )

{
    LPBITMAPINFOHEADER  lpbi;
    HPALETTE            hpalT;
    HDC                 hdc;
    HBITMAP             hbm;

//    StartWait();

    if (!hdib)
        return NULL;

    lpbi = (LPBITMAPINFOHEADER) (VOID FAR *)GlobalLock(hdib);

    if (!lpbi)
        return NULL;

    hdc = GetDC(NULL);

    if (hpal){
        hpalT = SelectPalette(hdc,hpal,FALSE);
        RealizePalette(hdc);     // GDI Bug...????
    }

    hbm = CreateDIBitmap(hdc,
                (LPBITMAPINFOHEADER)lpbi,
                (LONG)CBM_INIT,
                (LPSTR)lpbi + lpbi->biSize + PaletteSize(lpbi),
                (LPBITMAPINFO)lpbi,
                DIB_RGB_COLORS );

    if (hpal)
        SelectPalette(hdc,hpalT,FALSE);

    ReleaseDC(NULL,hdc);
    GlobalUnlock(hdib);

//    EndWait();

    return hbm;
}


//  ImageClass::Draw
//  Draw the DIB on the screen or other device.

void ImageClass::Draw( HWND hWnd, HDC hDC, int x, int y )

{
    HPALETTE hpalT;
    BITMAPINFOHEADER bi;

    SetWindowOrg (hDC, x, y);

    if (bImageValid) {
        hpalT = SelectPalette (hDC, hpalCurrent, FALSE);
        RealizePalette (hDC);

        if (hbmCurrent && !bDIBToDevice) {
            DrawBitmap (hDC, 0, 0, hbmCurrent, SRCCOPY);
        }
        else if (hdibCurrent) {
            DibInfo (hdibCurrent, &bi);
            DibBlt (hDC,
                    0,
                    0,
                    (int)bi.biWidth,
                    (int)bi.biHeight,
                    hdibCurrent,
                    0,
                    0,
                    SRCCOPY);
        }
        else if (achFileName[0]) {
//            BandDIB (hWnd, hDC, 0, 0);
        }

        SelectPalette(hDC,hpalT,FALSE);
    }

//    DrawSelect(hDC, TRUE);
}


//  ImageClass::DrawBitmap
//  Draw a bitmap at a specified position in a DC.

BOOL ImageClass::DrawBitmap ( HDC hdc, int x, int y, HBITMAP hbm, DWORD rop )

{
    HDC       hdcBits;
    BITMAP    bm;
    BOOL      f;

    if (!hdc || !hbm)
        return FALSE;

    hdcBits = CreateCompatibleDC(hdc);
    GetObject(hbm,sizeof(BITMAP),(LPSTR)&bm);
    SelectObject(hdcBits,hbm);
    f = BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcBits,0,0,rop);
    DeleteDC(hdcBits);

    return f;
}


//  ImageClass::DibBlt
//  Draw a bitmap in CF_DIB format, taking the same parameters as BitBlt().

BOOL ImageClass::DibBlt (HDC hdc, int x0, int y0, int dx, int dy, HANDLE hdib, int x1, int y1, LONG rop)

{
    LPBITMAPINFOHEADER   lpbi;
    LPSTR                pBuf;

    if (!hdib)
        return PatBlt(hdc,x0,y0,dx,dy,rop);

    lpbi = (LPBITMAPINFOHEADER) (VOID FAR *)GlobalLock(hdib);

    if (!lpbi)
        return FALSE;

    pBuf = (LPSTR)lpbi + (WORD)lpbi->biSize + PaletteSize(lpbi);
    SetDIBitsToDevice (hdc, x0, y0, dx, dy,
                       x1,y1,
                       x1,
                       dy,
                       pBuf, (LPBITMAPINFO)lpbi,
                       DIB_RGB_COLORS );

    GlobalUnlock(hdib);
    return TRUE;
}


//  ImageClass::Paint
//  Respond to a WM_PAINT message.

void ImageClass::Paint( HWND hWnd )

{
    PAINTSTRUCT ps;                         // paint information structure
    HDC hDC;                                // display context handle

    /* If we have updated more than once, the rest of our
     * window is in some level of degradation worse than
     * our redraw...  we need to redraw the whole area
    */

    if (nUpdateCount>0) {
        BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        nUpdateCount = 0;
        InvalidateRect( hWnd, NULL, TRUE );
        return;
    }

    hDC = BeginPaint(hWnd, &ps);
    Draw( hWnd, hDC,
          GetScrollPos(hWnd,SB_HORZ),
          GetScrollPos(hWnd,SB_VERT) );
    EndPaint(hWnd, &ps);
}


//  ImageClass::PaletteChanged
//  Respond to a WM_PALETTECHANGED message.

void ImageClass::PaletteChanged( HWND hWnd )

{
    HDC hDC;                                // display context handle
    HPALETTE hOldPal;                       // handle to old palette
    int i;                                  // number fo changed colours

    if (bImageValid) {

        hDC = GetDC (hWnd);
        hOldPal = SelectPalette (hDC, hpalCurrent, 0);

        i = RealizePalette (hDC);

        // If the screen has not changed too many times,
        // update the screen colours.  Otherwise, redraw
        // the image.

        if (i) {

            if (nUpdateCount<3) {
                UpdateColors( hDC );
                nUpdateCount++;
            }
            else {
                InvalidateRect( hWnd, NULL, TRUE );
                nUpdateCount = 0;
            }
        }

        SelectPalette (hDC, hOldPal, 0);
        ReleaseDC (hWnd, hDC);
    }
}


//  ImageClass::QueryNewPalette
//  Respond to a WM_QUERYNEWPALETTE message.

int ImageClass::QueryNewPalette( HWND hWnd )

{
    HDC hDC;                                // display context handle
    HPALETTE hOldPal;                       // handle to old palette
    int i;                                  // number fo changed colours

    /* If palette realization causes a palette change,
       * we need to do a full redraw.
    */

    if (bImageValid) {

        hDC = GetDC (hWnd);
        hOldPal = SelectPalette (hDC, hpalCurrent, 0);

        i = RealizePalette(hDC);

        SelectPalette (hDC, hOldPal, 0);
        ReleaseDC (hWnd, hDC);

        if (i) {
            InvalidateRect (hWnd, (LPRECT) (NULL), 1);
            nUpdateCount = 0;
            return 1;
        }
        else
            return 0;
    }
    else
        return FALSE;
}


//  ImageClass::GetImageDimensions
//  Retrieve the width and height of the image.

void ImageClass::GetImageDimensions( int *pWidth, int *pHeight )

{
    BITMAPINFOHEADER bi;                    // bitmap information

    // If we have a valid image...

    if (bImageValid) {

        // Retrieve the DIB information.

        DibInfo( hdibCurrent, &bi );

        // Return the width and height.

        *pWidth = (int) bi.biWidth;
        *pHeight = (int) bi.biHeight;
    }
    else {
        *pWidth = 0;
        *pHeight = 0;
    }
}


//  ImageClass::SetCaption
//  Set the image caption.

void ImageClass::SetCaption( HWND hWnd )

//  Input arguments:
//  hWnd                Image window handle.

{
    char szTitle[32];                       // file title
    char szName[128];                       // window title
    BITMAPINFOHEADER bi;                    // bitmap information

    // Read the information about the current DIB.

    DibInfo(hdibCurrent,&bi);

    // Extract the filename from the full pathname.

    GetFileTitle( achFileName, szTitle, 32 );

    // Create a window caption from the filename and the DIB attributes.

    wsprintf (szName,
              "%ls %dx%dx%d",
              (LPSTR)strupr( szTitle ),
              (WORD)bi.biWidth,
              (WORD)bi.biHeight,
              (WORD)bi.biBitCount );

    // Set the caption.

    SetWindowText (hWnd, szName);
}


//  ImageClass::SetWindowSize
//  Resize the image window to fit the image.

void ImageClass::SetWindowSize( HWND hWnd )

//  Input arguments:
//  hWnd                Image window handle.

{
    BITMAPINFOHEADER bi;                    // bitmap information
    DWORD dwStyle;                          // window style
    RECT rect;                              // window rectangle

    // Read the information about the current DIB.

    DibInfo(hdibCurrent,&bi);

    // Get the window size which will result in the appropriate
    // dimensions for the client area.

    SetRect( &rect, 0, 0, int(bi.biWidth), int(bi.biHeight) );
    dwStyle = GetWindowLong( hWnd, GWL_STYLE );
    AdjustWindowRect( &rect, dwStyle, FALSE );

    // Set the window size.

    SetWindowPos( hWnd, NULL, 0, 0, rect.right - rect.left,
                  rect.bottom - rect.top, SWP_NOMOVE | SWP_NOZORDER );
}

⌨️ 快捷键说明

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