这是一个位图的淡出代码,有些地方看不懂,那为老大解释解释一下
HWND g_hWnd = NULL;
HBITMAP g_hBmp = NULL;g_hWnd = hWnd;
g_hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));void DrawFade() {
    HDC hdc = GetDC(g_hWnd);
    BITMAPINFO bi;
    memset(&bi, 0, sizeof(bi));
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    GetDIBits(hdc, g_hBmp, 0, 0, NULL, &bi, DIB_RGB_COLORS);
    int w = bi.bmiHeader.biWidth;
    int h = bi.bmiHeader.biHeight;
    int span = w*3;                        //为什么乘3又除4?
    while (span%4) {
        span++;
    }
    int bufSize = span*h;
    BYTE *buf = new BYTE[bufSize];
    memset(buf, 0, bufSize);
    bi.bmiHeader.biBitCount = 24;
    bi.bmiHeader.biCompression = 0;
    bi.bmiHeader.biSizeImage = span*h;
    GetDIBits(hdc, g_hBmp, 0, h, buf, &bi, DIB_RGB_COLORS);
    BYTE *p = NULL;
    float fadeRate = g_fadeValue/(float)255;
    for (int y = 0; y< h; y++) {             //这个循环帮忙解释一下
        for (int x = 0; x< w; x++) {
            p = &buf[span*y+x*3];
            *p = (BYTE)(fadeRate*(*p));
            p++;
            *p = (BYTE)(fadeRate*(*p));
            p++;
            *p = (BYTE)(fadeRate*(*p));
        }
    }
    HBITMAP hFadeBmp = CreateDIBitmap(hdc, &bi.bmiHeader , CBM_INIT, buf, &bi, DIB_RGB_COLORS);
    HDC hMemDC = CreateCompatibleDC(hdc);
    HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hFadeBmp);
    BitBlt(hdc, 0, 0, w, h, hMemDC, 0, 0, SRCCOPY);
    SelectObject(hMemDC, hOld);
    DeleteDC(hMemDC);
    delete buf;
    ReleaseDC(g_hWnd, hdc);
}

解决方案 »

  1.   

    如何用鼠标拉矩形选取画元?谢谢各位!在线等待! (zswking ) 没人能回答吗?
      

  2.   

    int span = w*3;                        //为什么乘3又除4?
        while (span%4) {
            span++;
        }'span' is a range to control the fade , the loop of while statement ensures 'span' can be divided by 4 
      

  3.   

    int span = w*3;                        //为什么乘3又除4?
        while (span%4) {
            span++;
        }'span' is a range to control the fade , the loop of while statement ensures 'span' can be divided exactly by 4 
      

  4.   

    for (int y = 0; y< h; y++) {             //这个循环帮忙解释一下
            for (int x = 0; x< w; x++) {
                p = &buf[span*y+x*3];
                *p = (BYTE)(fadeRate*(*p));
                p++;
                *p = (BYTE)(fadeRate*(*p));
                p++;
                *p = (BYTE)(fadeRate*(*p));
            }
        }
    This loop use fadeRate get the the new date, it contains three part to every pixel, R, G, B ,do u understand ?
    :)
      

  5.   

    是这样的
    位图在内存中的存储结构是 
               R(8bit)G(8bit)B(8bit) 于是用24bit就可以存储真彩色位图
    在一行有n个点时需要 n*24 bit的空间    
    由于 32位操作系统中int 的 字长为32bit , 所以需要 n*24/32 个int 字长,当不能整除时 就增加 n 的值,保证得出最小的 空间可以存储位图。这就是*3再除4的原因。