Visual Studio 2013 C++中的位图透明度

Con*_*sed 0 c++ windows winapi image visual-studio

我对这个感到困惑.我使用论坛推荐的程序将32位PNG转换为带alpha通道的32位位图.我将这些添加到资源编辑器并通过工具箱将它们放到Dialog上.我已经阅读了(我认为是广泛的)关于位图透明度和Visual Studio中的限制.
让我感到困惑的是,我通过Visual Studio资源编辑器将图片控件添加到我的对话框中.例如,我有两个红球,一个是24位位图,另一个是32位位图.在visual studio的测试模式下,当使用资源编辑器打开.rc时,透明度看起来很好. 视觉工作室的测试模式

但是,当我以编程方式调用DialogBox时,我没有透明度.

DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1),
    NULL, DialogProc);
Run Code Online (Sandbox Code Playgroud)

以编程方式调用DialogBox时的对话框 当我单击Visual Studio中的测试按钮时,它必须调用例程DialogBox或类似的以显示位图.当我将位图放在资源编辑器中的对话框时,它显示透明度.微软做了什么,我没做什么?
我是故意在没有MFC的情况下开发的.这是问题,只有在MFC中才能使用Dialogbox加载透明度(我意识到它缩减为CreateWindowEX).我意识到也可以使用各种bitblt方法.那是Visual Studio在幕后做的吗?还审查了WM_CTLCOLORSTATIC等材料.您有什么想法?是否有一些简单的方法来调用DialogBox并在对话框上获得透明的BMP?或者我们都被迫使用MFC?或者用户必须编写例程来擦除背景/油漆等.

对我而言,如果没有透明图像,对话框很容易,这似乎有点奇怪.需要非方形图像?这在某种程度上是一个问题.现在,软件工程师必须向Dialog Callback或程序添加大量代码.对我来说似乎更多的错误.

谢谢您,对于您的评论.

 #include <windows.h>
 #include <winuser.h>
 #include "resource.h"


 INT_PTR CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM   lParam)
  {
  int wmId, wmEvent;

  switch (message)
   {
   case WM_COMMAND:
    wmId = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case IDOK:
        EndDialog(hwnd, LOWORD(wParam));
        break;
    case IDCANCEL:
        EndDialog(hwnd, LOWORD(wParam));
        return TRUE;

    }

case WM_PAINT:
    break;

case WM_DESTROY:
    EndDialog(hwnd, LOWORD(wParam));
    break;
}
return FALSE; 
}


 int APIENTRY WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR     lpCmdLine,
 int       nCmdShow)
 {


DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1),
    NULL, DialogProc);

return 0;
 }
Run Code Online (Sandbox Code Playgroud)

zet*_*t42 6

这个问题有两种可能的解决方案,它们都有优点和缺点.

解决方案1通过简单地将应用程序清单文件添加到资源来修复OP所声明的原始问题.此解决方案无需编码.使用该解决方案实现的透明度并不完美,但自Windows XP以来所有Windows版本都支持.

解决方案2更先进,因为它创建了一个分层的子窗口,在对话框背景上提供图像的真实透明度以及任何重叠的子控件.缺点是至少需要Windows 8并且必须写出相当数量的非平凡代码(但是你很幸运,因为我已经为你做了这个;-)).

解决方案1 ​​ - 添加应用程序清单

仅当您添加指定公共控件版本6.0.0.0应用程序清单时,本机静态控件才支持具有Alpha透明度的位图.从屏幕截图中控件的"老派"外观我们可以看到您还没有这样的清单.

将以下代码段保存到名为"manifest.xml"的文件中,并将其放入应用程序资源文件夹中.在Visual Studio中,右键单击项目,转到"清单工具">"输入和输出">"其他清单文件">输入"manifest.xml"的相对路径,不带引号.

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
    <application>
        <!-- Windows 10 --> 
        <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
        <!-- Windows 8.1 -->
        <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
        <!-- Windows 8 -->
        <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
        <!-- Windows 7 -->
        <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
        <!-- Windows Vista -->
        <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
    </application>
  </compatibility>
  <dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
  </dependency>
</assembly>
Run Code Online (Sandbox Code Playgroud)

无需进一步编码,只需在资源编辑器中选择图片控件的位图资源(实际上是静态控件).

要使用的位图应该是32 bpp位图,自下而上的行顺序,非预乘alpha.如果使用PixelFormer从PNG转换,请使用格式A8:R8:G8:B8(32 bpp)而不选择导出对话框中的其他复选框.如果您使用XnView进行转换,只需保存为BMP,默认情况下它将使用此格式.

结果:

在此输入图像描述

我们可以看到,我们只获得"虚假"的透明度.图像下方的任何其他控件将被剪切到静态控件的边界.

解决方案2 - 使用分层子窗口

使用分层子窗口(WS_EX_LAYERED扩展样式)可以实现真正的透明度.从Windows 8开始支持此功能.但它需要一些编码.

我将所需的代码包装到一个函数中SetLayeredWindowFromBitmapResource(),该函数可以从对话框的WM_INITDIALOG处理程序中调用.该函数将任何错误抛出为std::system_error异常,因此您必须添加try/catch块来处理错误(这将在下面的"usage"示例中进一步说明).

#include <system_error>

/// Turn given window into a layered window and load a bitmap from given resource ID 
/// into it.
/// The window will be resized to fit the bitmap.
/// Bitmap must be 32bpp, top-down row order, premultiplied alpha.
///
/// \note For child windows, this requires Win 8 or newer OS
///       (and "supportedOS" element for Win 8 in application manifest)  
///
/// \exception Throws std::system_error in case of any error.

void SetLayeredWindowFromBitmapResource( 
    HWND hwnd, UINT bitmapResourceId, HINSTANCE hInstance = nullptr ) 
{
    // Enable "layered" mode for the child window. This enables full alpha channel 
    // transparency.

    // GetWindowLong() won't reset the last error in case of success.
    // As we can't judge from the return value of GetWindowLong() alone if 
    // the function was successful (0 may be returned even in case of
    // success), we must reset the last error to reliably detect errors.
    ::SetLastError( 0 ); 
    DWORD exStyle = ::GetWindowLong( hwnd, GWL_EXSTYLE );
    if( !exStyle )
    {
        // NOTE: Call GetLastError() IMMEDIATELY when a function's return value 
        // indicates failure and it is documented that said function supports 
        // GetLastError().
        // ANY other code (be it your own or library code) before the next line 
        // must be avoided as it may invalidate the last error value.
        if( DWORD err = ::GetLastError() )
            throw std::system_error( static_cast<int>(err), 
                std::system_category(),
                "SetLayeredWindowFromBitmapResource: Could not get extended window style" );
    }

    // SetWindowLong() won't reset the last error in case of success.
    // As we can't judge from the return value of GetWindowLong() alone if 
    // the function was successful (0 may be returned even in case of
    // success), we must reset the last error to reliably detect errors.
    ::SetLastError( 0 ); 
    if( !::SetWindowLong( hwnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED ) ) 
    {
        if( DWORD err = ::GetLastError() )
            throw std::system_error( static_cast<int>(err), 
                std::system_category(),
                "SetLayeredWindowFromBitmapResource: Could not set extended window style" );
    }

    // Use RAII ( https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization )
    // to cleanup resources even in case of exceptions.
    // This greatly simplifies the code because now we don't have to manually cleanup the 
    // resources at every location in the code where we throw an exception.
    struct Resources {
        HBITMAP hImage = nullptr;
        HGDIOBJ hOldImage = nullptr;
        HDC hMemDC = nullptr;

        // This destructor will be automatically called before the function 
        // SetLayeredWindowFromBitmapResource() returns aswell as any locations 
        // in the code where the "throw" keyword is used to throw an exception.
        ~Resources() 
        {
            if( hMemDC ) 
            {
                if( hOldImage )
                    ::SelectObject( hMemDC, hOldImage );
                ::DeleteDC( hMemDC );
            }
            if( hImage )
                ::DeleteObject( hImage );
        }
    } res;

    // Make it possible to use nullptr as an argument for the hInstance parameter of 
    // this function. This means we will load the resources from the current executable 
    // (instead of another DLL).
    if( ! hInstance )
        hInstance = ::GetModuleHandle( nullptr );

    // Load bitmap with alpha channel from resource. 
    // Flag LR_CREATEDIBSECTION is required to create a device-independent bitmap that 
    // preserves the alpha channel.
    res.hImage = reinterpret_cast<HBITMAP>(::LoadImage(
        hInstance, MAKEINTRESOURCE( bitmapResourceId ), IMAGE_BITMAP, 
        0, 0, LR_CREATEDIBSECTION ));
    if( !res.hImage )
    {
        DWORD err = ::GetLastError();
        throw std::system_error( static_cast<int>(err),
            std::system_category(),
            "SetLayeredWindowFromBitmapResource: Could not load bitmap resource" );
    }

    // Get bitmap information (width, height, etc.)
    BITMAP imgInfo{ 0 };
    if( !::GetObject( res.hImage, sizeof( imgInfo ), &imgInfo ) )
    {
        DWORD err = ::GetLastError();
        throw std::system_error( static_cast<int>(err),
            std::system_category(),
            "SetLayeredWindowFromBitmapResource: Could not get bitmap information" );
    }

    if( imgInfo.bmBitsPixel != 32 || imgInfo.bmPlanes != 1 )
    {
        // Use a constant error value here because this is our own error condition.
        // Of course GetLastError() wouldn't return anything useful in this case.
        DWORD err = ERROR_INVALID_DATA;
        throw std::system_error( err, std::system_category(),
            "SetLayeredWindowFromBitmapResource: bitmap must be 32 bpp, single plane" );
    }

    // Create a memory DC that will be associated with the image.
    // UpdateLayeredWindow() can't use image directly, it must be in a memory DC.
    res.hMemDC = ::CreateCompatibleDC( nullptr );
    if( !res.hMemDC )
    {
        DWORD err = ::GetLastError();
        throw std::system_error( static_cast<int>(err), 
            std::system_category(),
            "SetLayeredWindowFromBitmapResource: Could not create memory DC" );
    }

    res.hOldImage = ::SelectObject( res.hMemDC, res.hImage );
    if( !res.hOldImage )
    {
        DWORD err = ::GetLastError();
        throw std::system_error( static_cast<int>(err), 
            std::system_category(),
            "SetLayeredWindowFromBitmapResource: Could not select bitmap into memory DC" );
    }

    // Assign the image to the child window, making it transparent.
    SIZE size{ imgInfo.bmWidth, imgInfo.bmHeight };
    POINT ptSrc{ 0, 0 };
    BLENDFUNCTION blend{ AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
    if( !::UpdateLayeredWindow( hwnd, nullptr, nullptr, &size, res.hMemDC, &ptSrc, 
                                0, &blend, ULW_ALPHA ) )
    {
        DWORD err = ::GetLastError();
        throw std::system_error( static_cast<int>(err), 
            std::system_category(),
            "SetLayeredWindowFromBitmapResource: Could not update layered window" );
    }

    // Destructor of res object will cleanup resources here!
}
Run Code Online (Sandbox Code Playgroud)

用法:

可以在对话框过程的WM_INITDIALOG处理程序中调用该函数,请参见下面的示例.该示例还说明了如何处理错误.

注意:我在这里调用MessageBoxA()因为std::exception::what()返回一个const char*显然是多字节(ANSI)编码的字符串,其中包含来自操作系统的本地化错误消息(使用VS2015或更新版本).

#include <sstream>

/// Dialog box procedure.
INT_PTR CALLBACK TestDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) {
    UNREFERENCED_PARAMETER( lParam );
    switch( message ) {
        case WM_INITDIALOG: {
            // This is the child window where we want to show the image (e. g. a static).
            if( HWND hwndImage = ::GetDlgItem( hDlg, IDC_IMAGE ) ){
                try{
                    SetLayeredWindowFromBitmapResource( hwndImage, IDB_BITMAP1 );
                }
                catch( std::system_error& e ){
                    std::ostringstream msg;
                    msg << e.what() << std::endl << "Error code: " << e.code();
                    ::MessageBoxA( hDlg, msg.str().c_str(), "Error", MB_ICONERROR );
                }
            }
            return TRUE;
        }
        case WM_COMMAND: {
            if( LOWORD( wParam ) == IDOK || LOWORD( wParam ) == IDCANCEL ){
                EndDialog( hDlg, LOWORD( wParam ) );
                return TRUE;
            }
            break;
        }
    }
    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

结果:

截图

陷阱:

应用程序必须具有至少指定Win 8兼容性的清单资源:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
    <application>
        <!-- Windows 10 --> 
        <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
        <!-- Windows 8.1 -->
        <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
        <!-- Windows 8 -->
        <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>   
    </application>
  </compatibility>
  <dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
  </dependency>
</assembly>
Run Code Online (Sandbox Code Playgroud)

要加载的图像必须是32 bpp,自上而下的位图,具有预乘alpha通道.

例如,可以使用PixelFormer将常规PNG转换为此格式.打开图像,然后单击文件>导出.选择位图,格式A8:R8:G8:B8(32 bpp),预乘alpha,自上而下行顺序.