Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here, this will do the job. You'll need to ensure you have gdiplus and msimg32 libraries linked for the AlphaBlend (TransparentBlt assumes that a <em>single</em> rgb color in the image will be treated as transparent. I.e it's just 1-bit transparency when using TransparentBlt - fully opaque or fully transparent.)</p> <p>You'll (presumably) wish to use AlphaBlend for this job.</p> <p>Note - I haven't bothered to add an WM_ERASEBKGND to the WndProc function. As a consequence, each time the image is re-drawn, it's drawn straight over the top of what's already there (try resizing the window). Just do a FillRect in erasebkg, and you'll be fine.</p> <p>EDIT: Code updated to use displayImage with a NULL hbitmap to do the background too.</p> <p>Here 'go:</p> <pre><code>#define WIN32_LEAN_AND_MEAN #define WINVER 0x0600 // needed for alphablend function.. #include &lt;windows.h&gt; #include &lt;gdiplus.h&gt; const char g_szClassName[] = "myWindowClass"; // BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF // requires GDIPlus HBITMAP mLoadImg(WCHAR *szFilename) { HBITMAP result=NULL; Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false); bitmap-&gt;GetHBITMAP(NULL, &amp;result); delete bitmap; return result; } //void CStaticImg::displayImage(HBITMAP mBmp, HWND mHwnd) void displayImage(HBITMAP mBmp, HWND mHwnd) { RECT myRect; BITMAP bm; HDC screenDC, memDC; HBITMAP oldBmp; BLENDFUNCTION bf; GetObject(mBmp, sizeof(bm), &amp;bm); bf.BlendOp = AC_SRC_OVER; bf.BlendFlags = 0; bf.SourceConstantAlpha = 0xff; bf.AlphaFormat = AC_SRC_ALPHA; screenDC = GetDC(mHwnd); GetClientRect(mHwnd, &amp;myRect); if (mBmp == NULL) FillRect(screenDC, &amp;myRect, WHITE_BRUSH); else { memDC = CreateCompatibleDC(screenDC); oldBmp = (HBITMAP)SelectObject(memDC, mBmp); AlphaBlend (screenDC, 0, 0, myRect.right,myRect.bottom, memDC, 0, 0, bm.bmWidth,bm.bmHeight, bf); SelectObject(memDC, oldBmp); DeleteDC(memDC); ReleaseDC(mHwnd, screenDC); } } // Step 4: the Window Procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HBITMAP myImage; switch(msg) { case WM_CLOSE: DestroyWindow(hwnd); break; case WM_ERASEBKGND: displayImage(NULL, hwnd); break; case WM_CREATE: myImage = mLoadImg(L"bg_shadow_png.png"); break; case WM_PAINT: //displayImage(HBITMAP mBmp, HWND mHwnd); displayImage(myImage, hwnd); ValidateRect(hwnd, NULL); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; static Gdiplus::GdiplusStartupInput gdiplusStartupInput; static ULONG_PTR gdiplusToken; // so we can load all the image formats that windows supports natively - (I'm using a transparent PNG on the main dialog) GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, NULL); //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&amp;wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0) { TranslateMessage(&amp;Msg); DispatchMessage(&amp;Msg); } Gdiplus::GdiplusShutdown(gdiplusToken); return Msg.wParam; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload