无法显示位值

时间:2014-07-29 19:33:13

标签: visual-c++ mfc

我正在编写一个程序,将数据文件中的值显示为图像。但我只能得到一个黑暗的客户区。这是一个类似于我的代码的小程序。你能看到我错过的吗?我只改变了OnDraw功能。

void CColorDisplayView::OnDraw(CDC* pDC)
{
    CColorDisplayDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    CRect rect;
    GetClientRect(rect);
    int nWidth = rect.Width();
    int nHeight = rect.Height();

    int          i, incr, R, G, B ;
    LOGPALETTE * plp ;

    plp = (LOGPALETTE *)(malloc (sizeof (LOGPALETTE) + 246 * sizeof (PALETTEENTRY))) ;

    plp->palVersion    = 0x0300 ;
    plp->palNumEntries = 247 ;

    // The following loop calculates 31 gray shades, but 3 of them
    //        will match the standard 20 colors
    for (i = 0, G = 0, incr = 8 ; G <= 0xFF ; i++, G += incr)
    {
        plp->palPalEntry[i].peRed   = (BYTE) G ;
        plp->palPalEntry[i].peGreen = (BYTE) G ;
        plp->palPalEntry[i].peBlue  = (BYTE) G ;
        plp->palPalEntry[i].peFlags = 0 ;

        incr = (incr == 9 ? 8 : 9) ;
    }

    // The following loop is responsible for 216 entries, but 8 of 
    //        them will match the standard 20 colors, and another
    //        4 of them will match the gray shades above.

    for (R = 0 ; R <= 0xFF ; R += 0x33)
    for (G = 0 ; G <= 0xFF ; G += 0x33)
    for (B = 0 ; B <= 0xFF ; B += 0x33)
    {
        plp->palPalEntry[i].peRed   = (BYTE) R ;
        plp->palPalEntry[i].peGreen = (BYTE) G ;
        plp->palPalEntry[i].peBlue  = (BYTE) B ;
        plp->palPalEntry[i].peFlags = 0 ;

        i++ ;
    }

    CPalette cPalette;
    BOOL success = cPalette.CreatePalette (plp) ;
//  BOOL success = cPalette.CreateHalftonePalette(pDC);
    free (plp) ;

    CPalette* pOldPal = pDC->SelectPalette(&cPalette, FALSE);
    pDC->RealizePalette();

    // Allocate a bitmap header. Windows takes care of freeing this memory.
    BITMAPINFO *    pbmiDIB = NULL;
    pbmiDIB = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFOHEADER) + 256*sizeof(WORD)];
    if (pbmiDIB == NULL)
    {
        throw;
    }

    // Fill in the BITMAPINFOHEADER
    pbmiDIB->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pbmiDIB->bmiHeader.biWidth = nWidth;
    pbmiDIB->bmiHeader.biHeight = nHeight;
    pbmiDIB->bmiHeader.biPlanes = 1;
    pbmiDIB->bmiHeader.biBitCount = 8;          // max 256 colors supported
    pbmiDIB->bmiHeader.biCompression = BI_RGB;
    pbmiDIB->bmiHeader.biSizeImage = 0;
    pbmiDIB->bmiHeader.biXPelsPerMeter = 0;
    pbmiDIB->bmiHeader.biYPelsPerMeter = 0;
    pbmiDIB->bmiHeader.biClrUsed = 0;
    pbmiDIB->bmiHeader.biClrImportant = 0;

    // Create our DIB. This call allocates the memory for our bitmap.
    LPVOID pvBits = NULL;
    HBITMAP hBitmap = CreateDIBSection(
        pDC->GetSafeHdc(), 
        pbmiDIB,
        DIB_PAL_COLORS,
        (void **)&pvBits,        // Here's where we put the image
        NULL,
        0);

    LPBYTE pSensorValues = (LPBYTE) pvBits;
    delete pbmiDIB;
    pbmiDIB = NULL;

    int temp = 0;
    int k = 0;
    for(int j = 0; j < nHeight; j++) {
        for(int i = 0; i < nWidth; i++) {
            *pSensorValues = k % 247;
            temp = *pSensorValues;
            pSensorValues++;
            temp = *pSensorValues;
        }
        if (j % 10 == 0)
            k++;
    }

    CDC memdc;
    memdc.CreateCompatibleDC(pDC);
    CBitmap* pOldBm = (CBitmap *) memdc.SelectObject(hBitmap);

    BOOL bRet = pDC->BitBlt( 
        rect.left, 
        rect.top, 
        nWidth, nHeight,
        &memdc, 0, 0, SRCCOPY );
}

0 个答案:

没有答案