Windows splash screen using GDI+

Posted by Luther on Stack Overflow See other posts from Stack Overflow or by Luther
Published on 2011-03-07T16:09:16Z Indexed on 2011/03/07 16:10 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

The eventual aim of this is to have a splash screen in windows that uses transparency but that's not what I'm stuck on at the moment.

In order to create a transparent window, I'm first trying to composite the splash screen and text on an off screen buffer using GDI+.

At the moment I'm just trying to composite the buffer and display it in response to a 'WM_PAINT' message. This isn't working out at the moment; all I see is a black window.

I imagine I've misunderstood something with regards to setting up render targets in GDI+ and then rendering them (I'm trying to render the screen using straight forward GDI blit)

Anyway, here's the code so far:

//my window initialisation code
void MyWindow::create_hwnd(HINSTANCE instance, const SIZE &dim)
{                       
    DWORD ex_style = WS_EX_LAYERED ; //eventually I'll be making use of this layerd flag 
    m_hwnd = CreateWindowEx(
            ex_style,
            szFloatingWindowClass , 
            L"", 
            WS_POPUP  ,
            0, 
            0,       
            dim.cx, 
            dim.cy,
            null, 
            null, 
            instance, 
            null);      

     SetWindowLongPtr(m_hwnd ,0, (__int3264)(LONG_PTR)this);


      m_display_dc = GetDC(NULL);
      //This was sanity check test code - just loading a standard HBITMAP and displaying it in WM_PAINT. It worked fine
      //HANDLE handle= LoadImage(NULL , L"c:\\test_image2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);       
      m_gdip_offscreen_bm = new Gdiplus::Bitmap(dim.cx, dim.cy);

      m_gdi_dc = Gdiplus::Graphics::FromImage(m_gdip_offscreen_bm);//new Gdiplus::Graphics(m_splash_dc );//window_dc ;m_splash_dc       

        //this draws the conents of my splash screen - this works if I create a GDI+ context for the window, rather than for an offscreen bitmap. 
        //For all I know, it might actually be working but when I try to display the contents on screen, it shows a black image
      draw_all();
        //this is just to show that drawing something simple on the offscreen bit map seems to have no effect
      Gdiplus::Pen pen(Gdiplus::Color(255, 0, 0, 255));
      m_gdi_dc->DrawLine(&pen, 0,0,100,100);

      DWORD last_error = GetLastError(); //returns '0' at this stage                
}

And here's the snipit that handles the WM_PAINT message:

---8<-----------------------
//Paint message snippit

    case WM_PAINT:
    {
        BITMAP bm;
        PAINTSTRUCT ps;

        HDC hdc = BeginPaint(vg->m_hwnd, &ps); //get the HWNDs DC

        HDC hdcMem = vg->m_gdi_dc->GetHDC();   //get the HDC from our offscreen GDI+ object

        unsigned int width = vg->m_gdip_offscreen_bm->GetWidth();  //width and height seem fine at this point
        unsigned int height = vg->m_gdip_offscreen_bm->GetHeight();
        BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);   //this blits a black rectangle

        DWORD last_error = GetLastError();              //this was '0'

        vg->m_gdi_dc->ReleaseHDC(hdcMem);  

        EndPaint(vg->m_hwnd, &ps);  //end paint

        return 1;
    }

    ---8<-----------------------

My apologies for the long post. Does anybody know what I'm not quite understanding regarding how you write to an offscreen buffer using GDI+ (or GDI for that matter)and then display this on screen?

Thank you for reading.

© Stack Overflow or respective owner

Related posts about winapi

Related posts about gdi+