2016-07-07 54 views
2

首先我寫一些UINT作爲UINT** framebuffer中的顏色,然後通過CreateDIBSection創建一個BITMAPINFO,但是在運行該程序之後,窗口是黑色的,而不是我設置的某種顏色,怎麼了?如何使用CreateDIBSection在BITMAPINFO中編寫顏色數據?

PAINTSTRUCT ps; 
     HDC hdc; 

    static int s_widthClient, s_heightClient; 
    static BITMAPINFO s_bitmapInfo; 
    static HDC s_hdcBackbuffer;  
    static HBITMAP s_hBitmap; 
    static HBITMAP s_hOldBitmap; 
    static void* s_pData;   

    switch (message) 
    { 
    case WM_CREATE: 
    { 

     RECT rc; 
     GetClientRect(hWnd, &rc); 
     s_widthClient = rc.right - rc.left; 
     s_heightClient = rc.bottom - rc.top; 

     Tiny3DDevice pDevice(s_widthClient, s_heightClient, s_pData); 
     pDevice.Test(); 

     BITMAPINFOHEADER bmphdr = { 0 }; 
     bmphdr.biSize = sizeof(BITMAPINFOHEADER); 
     bmphdr.biWidth = s_widthClient; 
     bmphdr.biHeight = -s_heightClient; 
     bmphdr.biPlanes = 1; 
     bmphdr.biBitCount = 32; 
     bmphdr.biSizeImage = s_heightClient * s_widthClient * 4; 

     s_hdcBackbuffer = CreateCompatibleDC(nullptr); 

     HDC hdc = GetDC(hWnd); 
     //s_hBitmap = CreateCompatibleBitmap(hdc, s_widthClient, s_heightClient); 
     s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS, 
      reinterpret_cast<void**>(&pDevice.m_pFramebuffer), nullptr, 0); 

     s_hOldBitmap = (HBITMAP)SelectObject(s_hdcBackbuffer, s_hBitmap); 
     ReleaseDC(hWnd, hdc); 
    } 
    break; 
    case WM_PAINT: 
    { 
     hdc = BeginPaint(hWnd, &ps); 
     //BitBlt(s_hdcBackbuffer, 0, 0, s_widthClient, s_heightClient, nullptr, 0, 0, WHITENESS); 
     ////draw text 
     //SetTextColor(s_hdcBackbuffer, RGB(0, 0, 0)); 
     //SetBkMode(s_hdcBackbuffer, TRANSPARENT); 
     //TextOut(s_hdcBackbuffer, 0, 5, text.c_str(), text.size()); 

     BitBlt(ps.hdc, 0, 0, s_widthClient, s_heightClient, s_hdcBackbuffer, 0, 0, SRCCOPY); 
     EndPaint(hWnd, &ps); 
    } 
     break; 

和Tiny3DDevice:

class Tiny3DDevice 
    { 
    public: 
     Tiny3DDevice(int width, int height, void *fb); 
     ~Tiny3DDevice(); 
    public: 
     void Test();  
    public: 
     int m_width; 
     int m_height; 
     UINT** m_pFramebuffer; 
    }; 
Tiny3DDevice::Tiny3DDevice(int width, int height, void *fb) 
{ 
    m_width = width; 
    m_height = height; 
    m_pFramebuffer = new UINT*[width]; 
    for (int i = 0; i < width; ++i) 
    { 
     m_pFramebuffer[i] = new UINT[height]; 
    } 
} 

void Tiny3DDevice::Test() 
{ 
    ZCFLOAT3 color(0.5f, 0.5f, 0.5f); 
    for (int i = 0; i < m_width; ++i) 
     for (int j = 0; j < m_height; ++j) 
     { 
      //m_pFramebuffer[i][j] = MathUtil::ColorToUINT(color); 
      m_pFramebuffer[i][j] = 0x3fbcefff; 
     } 
} 

有什麼不好?我應該如何寫數據m_framebuffer?有什麼想法?

+0

不要把'CreateCompatibleDC'和'SelectObject(memdc,s_hBitmap);'放在'WM_CREATE'中。將它放在'WM_PAINT'中,並在最後釋放資源。你擁有它的方式幾微秒更快,但它是錯誤的。 –

回答

2

,你需要這樣的

PVOID pv; 
if (s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS, &pv, 0, 0)) 
{ 
    RtlFillMemoryUlong((PULONG)pv, bmphdr.biSizeImage, 0x3fbcefff); 
} 

代碼,你並不需要分配PV(pDevice.m_pFramebuffer在你的代碼),因爲它在CreateDIBSection分配。你只需要填寫它。您的Tiny3DDevice代碼完全錯誤且毫無意義。對於s_hdcBackbuffer 使用靜態瓦爾等 - 噩夢

BITMAPINFOHEADER bmphdr = { 0 }; 

沒有人嘗試使用這種:)?

BITMAPINFOHEADER bmphdr = { }; 
+0

但我想通過像'SetPixel'那樣更改m_pFramebuffer來改變顏色,我該怎麼做? – zhangbaochong

+0

但在什麼問題?或者只需填寫pv(m_pFramebuffer)或保存這個指針,以便稍後進行動態更改。你是否理解我的答案? – RbMm

+0

是的,沒關係,謝謝!我刪除分配m_pFramebuffer的代碼,並改變它的代碼如下:'如果(s_hBitmap = CreateDIBSection(nullptr,(PBITMAPINFO)bmphdr,DIB_RGB_COLORS, \t \t \t的reinterpret_cast (&pDevice.m_pFramebuffer),nullptr,0)) \t \t { \t \t \t pDevice.Test(); \t \t}' – zhangbaochong

相關問題