winapi - 使用 GDI+ 的 Windows 初始屏幕

标签 winapi gdi+ gdi splash-screen

最终的目标是在窗口中使用透明度的启动屏幕,但这不是我现在所坚持的。

为了创建透明窗口,我首先尝试使用 GDI+ 在离屏缓冲区上合成启动屏幕和文本。

目前,我只是尝试合成缓冲区并显示它以响应“WM_PAINT”消息。目前这还行不通;我所看到的只是一个黑色的窗口。

我想我对在 GDI+ 中设置渲染目标然后渲染它们有误解(我正在尝试使用直接的 GDI blit 渲染屏幕)

无论如何,这是到目前为止的代码:

//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);      


      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                
}

这是处理 WM_PAINT 消息的片段:

---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<-----------------------

对于这么长的帖子,我深表歉意。有谁知道我不太明白如何使用 GDI+(或 GDI)写入屏幕外缓冲区,然后将其显示在屏幕上?

感谢您的阅读。

最佳答案

解决了,尽管我确信这不是最佳选择。 首先,正如 Chris Becke 建议的那样,您不应响应 WM_PAINT 消息,而应仅使用“UpdateLayeredWindow”进行更新。我遇到的问题之一是给 UpdateLayeredWindow 提供了一些错误的坐标,特别是我交换了源和目标(doh!)

以下是从 GDI+ 表面调用 UpdateLayeredWindow 的代码。这不是最佳的,但它有效。

void TOPLEVEL_FLOATING_WINDOW::update_layered_window()
{
    BLENDFUNCTION blend_func = { 0 };     //build the blend function object... I guess microsoft doesn't/didn't like constructors
    blend_func.AlphaFormat = AC_SRC_OVER;
    blend_func.BlendFlags = 0;
    blend_func.SourceConstantAlpha = 255;
    blend_func.AlphaFormat = AC_SRC_ALPHA;

    POINT_2i  pt = m_screen_pos;
    VECTOR_2i dim = m_bounds.dimensions();

    POINT_2i  pt_src (0,0) ;            

    Gdiplus::Color bg(0,0,0,0);
    m_gdip_offscreen_bm->GetHBITMAP(bg, &m_offscreen_bm);   //get the Hbitmap from my GDI+ bitmap - I think this is allocating a whole new bitmap off the heap. Yuck, very inefficient!
    HDC  splash_dc = CreateCompatibleDC(m_display_dc);      //create a temporary HDC        
    HGDIOBJ oldobj = SelectObject(splash_dc , m_offscreen_bm);  //'select' the bitmap.      
    UpdateLayeredWindow(m_hwnd,m_display_dc, (POINT*)&pt, (SIZE*)&dim, splash_dc , (POINT*)&pt_src, 0 ,&blend_func,ULW_ALPHA); //this call works and updates our splash screens hidden buffer       
    SelectObject(splash_dc, oldobj);    //some tidy up code
    DeleteObject(m_offscreen_bm);       //free the bitmap. Memory fragmentation HO!
    DeleteDC(splash_dc); //Delete the DC        
}

这就是我认为应该起作用但不起作用的。我会向任何能告诉我为什么不的人提供一些积分!我想这是因为 HDC 并不是生来平等的,UpdateLayeredWindow 函数只能接受由特定源以某种方式创建的 HDC。如果规则更明显就好了。

void TOPLEVEL_FLOATING_WINDOW::update_layered_window_in_an_ideal_world()
    {
        m_refresh_needed = false;
        BLENDFUNCTION blend_func = { 0 };
        blend_func.AlphaFormat = AC_SRC_OVER;
        blend_func.BlendFlags = 0;
        blend_func.SourceConstantAlpha = 255;
        blend_func.AlphaFormat = AC_SRC_ALPHA;

        POINT_2i  pt = m_screen_pos;
        VECTOR_2i dim = m_bounds.dimensions();

        POINT_2i  pt_src (0,0) ;            

        HDC gdi_HDC = m_screen_gdi_dc->GetHDC();    //I have a GDI+ 'Graphics' object whose back buffer is the image I want to composite on to the desktop      
        UpdateLayeredWindow(m_hwnd,m_display_dc, (POINT*)&pt, (SIZE*)&dim, gdi_HDC , (POINT*)&pt_src, 0 ,&blend_func,ULW_ALPHA);
        m_screen_gdi_dc->ReleaseHDC(gdi_HDC);   //be nice and release the gdi_HDC
    }

唯一的选择是忽略 GDI+ 并使用我自己的光栅库渲染所有内容,该库非常乐意渲染到任何离屏缓冲区。然而,这对于这个项目来说有点矫枉过正。

关于winapi - 使用 GDI+ 的 Windows 初始屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5221936/

相关文章:

winapi - TextOutW 在屏幕上与打印机上的显示不同

c - 如何从终端将字符传递给可执行文件?

c - C 和 Windows GDI 中的双缓冲*框架*

c# - 对标记为 ASCII 的 EXIF 属性使用 UTF8 解码是否安全?

C++ 在窗口上输出文本

c++ - 使用 WinAPI 进行夏令时和 UTC 到本地时间的转换

c# - WinApi SendMessage/PostMessage 在我的电脑上不工作?

C# GDI - 如何检查像素是否不透明?

c++ - 如何管理SVG文件中的超出范围的值?

.net - C# PrintDocument PaperSize/FontSize