{"id":1549,"date":"2018-03-29T18:29:53","date_gmt":"2018-03-29T09:29:53","guid":{"rendered":"http:\/\/rageworx.info\/?p=1549"},"modified":"2021-05-17T01:44:52","modified_gmt":"2021-05-16T16:44:52","slug":"create-hbitmap-from-fl_rgb_image","status":"publish","type":"post","link":"https:\/\/rageworx.info\/?p=1549","title":{"rendered":"Create HBITMAP from Fl_RGB_Image"},"content":{"rendered":"<p>Here is a simple code for convert Fl_RGB_Image to HBITMAP.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">HBITMAP GetHBMPfromFlRGBImg( const Fl_RGB_Image* src )\r\n{\r\n    if ( src == NULL )\r\n        return NULL;\r\n\r\n    if ( src-&gt;d() != 4 ) \/\/\/ Accepts only for RGBA 4 depth.\r\n        return NULL;\r\n\r\n    BITMAPINFOHEADER bmih = {0};\r\n\r\n    bmih.biSize             = sizeof(BITMAPINFOHEADER);\r\n    bmih.biWidth            = src-&gt;w();\r\n    bmih.biHeight           = -(src-&gt;h());\r\n    bmih.biPlanes           = 1;\r\n    bmih.biBitCount         = 32;\r\n    bmih.biCompression      = BI_RGB ;\r\n    bmih.biXPelsPerMeter    = 10;\r\n    bmih.biYPelsPerMeter    = 10;\r\n\r\n    BITMAPINFO dbmi = {0};\r\n\r\n    dbmi.bmiHeader = bmih;\r\n    dbmi.bmiColors-&gt;rgbBlue = 0;\r\n    dbmi.bmiColors-&gt;rgbGreen = 0;\r\n    dbmi.bmiColors-&gt;rgbRed = 0;\r\n    dbmi.bmiColors-&gt;rgbReserved = 0;\r\n\r\n    unsigned psz = src-&gt;w() * src-&gt;h();\r\n    const uchar* refbuff = (const uchar*)src-&gt;data()[0];\r\n    uchar* pixels = new uchar[ psz * 4 ];\r\n\r\n    if ( pixels == NULL )\r\n        return NULL;\r\n\r\n    \/\/ need to convert RGBA to BGRA\r\n    for( unsigned cnt=0; cnt&lt;psz; cnt++ )\r\n    {\r\n        pixels[ cnt * 4 + 0 ] = refbuff[ cnt * 4 + 2 ];\r\n        pixels[ cnt * 4 + 1 ] = refbuff[ cnt * 4 + 1 ];\r\n        pixels[ cnt * 4 + 2 ] = refbuff[ cnt * 4 + 0 ];\r\n        pixels[ cnt * 4 + 3 ] = refbuff[ cnt * 4 + 3 ];\r\n    }\r\n\r\n    HDC hdc = GetDC(NULL);\r\n\r\n    HBITMAP hbmp = CreateDIBitmap(hdc, &amp;bmih, CBM_INIT, pixels, &amp;dbmi, DIB_RGB_COLORS);\r\n\r\n    delete[] pixels;\r\n\r\n    return hbmp;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>It should be help to convert Fl_RGB_Image ( must have depth 4 for RGBA ) to HBITMAP. And it works for changing Fl_Window to Layered window easily like this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">Fl_Window*  winTest = NULL;\r\nconst char*  strSplash[] = { \"Code by Raphael Kim, (C)2018, Copyright\\n\",\r\n                             \"Programming FLTK is funny !\\n\",\r\n                             \"Thank you.\" };\r\n\r\nint main (int argc, char ** argv)\r\n{\r\n    Fl_RGB_Image* imgBg = new Fl_PNG_Image( \"testbg.png\" );\r\n\r\n    if ( imgBg == NULL )\r\n        return 0;\r\n\r\n    int img_w = imgBg-&gt;w();\r\n    int img_h = imgBg-&gt;h();\r\n\r\n    winTest = new Fl_Window( img_w, img_h, \"Splash Test\" );\r\n    if ( winTest != NULL )\r\n    {\r\n        winTest-&gt;color( 0 );\r\n        winTest-&gt;end();\r\n        winTest-&gt;show();\r\n\r\n        \/\/ Make window layered splash for Windows32.\r\n        HWND hWnd = fl_xid( winTest );\r\n        HDC  hDC  = GetDC( hWnd );\r\n\r\n        POINT ptPos = {0,0};\r\n        SIZE sizeWnd = { winTest-&gt;w(), winTest-&gt;h() };\r\n\r\n        SetWindowLong( hWnd, GWL_EXSTYLE,\r\n                       GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);\r\n\r\n        HDC hdcMem = CreateCompatibleDC( hDC );\r\n        HBITMAP hbmp = GetHBMPfromFlRGBImg( imgBg );\r\n\r\n        fl_begin_offscreen( hbmp );\r\n        imgBg-&gt;draw( 0, 0 );\r\n\r\n        fl_font( FL_HELVETICA_ITALIC, 18 );\r\n        fl_color( 0xFFFFFFFF );\r\n\r\n        int tput_x = imgBg-&gt;w() \/ 10;\r\n        int tput_y = imgBg-&gt;h() \/ 2;\r\n\r\n        for( int cnt=0; cnt&lt;3; cnt++ )\r\n        {\r\n            fl_draw( strSplash[cnt], tput_x, tput_y );\r\n            tput_y += 20;\r\n        }\r\n\r\n        fl_end_offscreen();\r\n\r\n        HGDIOBJ oldBitmap = SelectObject(hdcMem, hbmp);\r\n\r\n        BLENDFUNCTION blend = {0};\r\n        blend.BlendOp = AC_SRC_OVER;\r\n        blend.SourceConstantAlpha = 255;\r\n        blend.AlphaFormat = AC_SRC_ALPHA;\r\n\r\n        UpdateLayeredWindow( hWnd, NULL,\r\n                             &amp;ptPos, &amp;sizeWnd,\r\n                             hdcMem, &amp;ptPos,\r\n                             0, \/\/\/ RGB(255,255,255),\r\n                             &amp;blend,\r\n                             ULW_ALPHA );\r\n\r\n        RedrawWindow( hWnd,\r\n                      NULL, NULL,\r\n                      RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );\r\n\r\n        SelectObject( hdcMem, oldBitmap );\r\n        DeleteObject( hbmp );\r\n    }\r\n\r\n    return Fl::run();\r\n}<\/pre>\n<p>And it may runs as this.<\/p>\n<p><a href=\"http:\/\/rageworx.info\/wp-content\/uploads\/2018\/03\/fltk_layered_win_win32.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1550\" src=\"http:\/\/rageworx.info\/wp-content\/uploads\/2018\/03\/fltk_layered_win_win32-500x375.png\" alt=\"\" width=\"500\" height=\"375\" srcset=\"https:\/\/rageworx.info\/wp-content\/uploads\/2018\/03\/fltk_layered_win_win32-500x375.png 500w, https:\/\/rageworx.info\/wp-content\/uploads\/2018\/03\/fltk_layered_win_win32-768x576.png 768w, https:\/\/rageworx.info\/wp-content\/uploads\/2018\/03\/fltk_layered_win_win32.png 800w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple code for convert Fl_RGB_Image to HBITMAP. HBITMAP GetHBMPfromFlRGBImg( const Fl_RGB_Image* src ) { if ( src == NULL ) return NULL; if ( src-&gt;d() != 4 ) \/\/\/ Accepts only for RGBA 4 depth. return NULL; BITMAPINFOHEADER bmih = {0}; bmih.biSize = sizeof(BITMAPINFOHEADER); bmih.biWidth = src-&gt;w();&#8230; <a href=\"https:\/\/rageworx.info\/?p=1549\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[570,71,569,109,32],"class_list":["post-1549","post","type-post","status-publish","format-standard","hentry","category-development","category-raphs","tag-application","tag-fltk","tag-layered","tag-win32","tag-windows"],"_links":{"self":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1549"}],"version-history":[{"count":0,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1549\/revisions"}],"wp:attachment":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}