C++, GDI+, Load Bitmap and use UpdateLayeredWindow to show Bitmap - c++

Community, I have a small problem and I need help.
I want to load a bitmap and want to show this with UpdatelayerdWindow. I have written the following source code. But nothing appears.
When I use the BitBlt function, the picture appears.
Where do I have here the error in my source code?
ATOM SplashRegisterClass(HINSTANCE hInstance) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
void ShowBitMap(HWND hwnd, HINSTANCE hInstance) {
int nuXPos = 0;
int nuYPos = 0;
int width = 0;
int height = 0;
BITMAP bm;
hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hbmpSplash == NULL)
{
::MessageBox(NULL, __T("Image not Found"), __T("Error"), MB_OK);
}
GetObject(hbmpSplash, sizeof(bm), &bm);
SIZE size = { bm.bmHeight, bm.bmWidth };
width = bm.bmWidth;
height = bm.bmHeight;
POINT ptZero = { 0 };
HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorinfo = { 0 };
monitorinfo.cbSize = sizeof(monitorinfo);
GetMonitorInfo(hmonPrimary, &monitorinfo);
const RECT & rcWork = monitorinfo.rcWork;
nuXPos = rcWork.left + (rcWork.right - rcWork.left) / 2;
nuYPos = rcWork.top + (rcWork.bottom - rcWork.top) / 2;
nuXPos = nuXPos - (175 / 2);
nuYPos = nuYPos - (170 / 2);
HDC hdcScreen = GetDC(NULL);
HDC hdcneu = CreateCompatibleDC(hdcScreen);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpSplash);
POINT position = { nuXPos, nuYPos };
BLENDFUNCTION blend = { 0 };
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
//Next line only for test!
//::BitBlt(hdcScreen, nuXPos, nuYPos, width, height, hdcMem, 0, 0, SRCCOPY);
//Show the Bitmap
if (!UpdateLayeredWindow(splashOwner, hdcScreen, &position, &size, hdcMem, NULL, RGB(0, 0, 0), &blend, ULW_ALPHA))
{
//int LastError = GetLastError();
}
SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
}
HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) {
SplashRegisterClass(hInstance);
splashOwner = CreateWindow(szWindowClass, NULL, WS_POPUP, 0, 0, 100, 100, NULL, NULL, hInstance, NULL);
if (!splashOwner)
{
::MessageBox(NULL, __T("Error"), __T("Error"), MB_OK);
}
ShowBitMap(splashOwner, hInstance);
return CreateWindowEx(WS_EX_LAYERED, szWindowClass, 0, WS_POPUP, 0, 0, 100, 100, splashOwner, NULL, hInstance, NULL);
}
I want delete everything when the main programme is running. Can I make with this function?
void DeleteSplashWindow(){
ReleaseDC(splashOwner, 0);
}
Thanks for help!

hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
This function fails, it should give an error. Change it to
hbmpSplash = (HBITMAP)::LoadImage(0, L"software.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
When creating layered window you must call SetLayeredWindowAttributes. Example
HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) {
...
HWND hwnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass,
0, WS_POPUP, 0, 0, 100, 100,
splashOwner, NULL, hInstance, NULL);
SetLayeredWindowAttributes(hwnd, transparentColor, alpha, LWA_COLORKEY | LWA_ALPHA);
return hwnd;
}

Thanks for the help, I arrive myself to the solution. 1. I forgot ShowWindow(hwnd,SW_SHOW); and
2.void DeleteSplashWindow(){
ReleaseDC(splashOwner, NULL);
ShowWindow(splashOwner, NULL);
}

Related

Painted image is always black

I'm trying draw an image within a popup windows. I get the bitmap infos from a stream. That part should be ok, because if I create a file and save the bitmap it looks fine.
The byte array is stored as follows:
static BYTE* _bitampContent;
Here is how I create the window:
static WCHAR name[] = L"Bitmap Display";
HINSTANCE _instance = ::GetModuleHandle(NULL);
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
wndClass.style = 0;
wndClass.lpfnWndProc = class::_WindowProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hInstance = _instance;
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = name;
if (RegisterClass(&wndClass) == 0)
{
MessageBox(0, L"The Window is not registered", L"Message", MB_OK);
return 0;
}
RECT rc;
GetWindowRect(hwndOwner, &rc);
int xPos = (GetSystemMetrics(SM_CXSCREEN) - rc.right) / 2;
int yPos = (GetSystemMetrics(SM_CYSCREEN) - rc.bottom) / 2;
hWnd = CreateWindowEx(WS_EX_TOPMOST, name, name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, _instance, NULL);
if (hWnd == 0)
{
MessageBox(0, L"hWnd = 0", L"Message", MB_OK);
return 0;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
msg.wParam;
And in the WndProc function a paint the bitmap.
LRESULT CALLBACK class::_WindowProc(HWND hWnd, UINT uMsgId, WPARAM wParam, LPARAM lParam)
{
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hmemdc;
switch (uMsgId)
{
case WM_PAINT:
BITMAPINFOHEADER bmpInfoHeader;
bmpInfoHeader = { 0 };
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biClrImportant = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biHeight = 200;
bmpInfoHeader.biWidth = 200;
bmpInfoHeader.biPlanes = 1;
unsigned long pixel_data_size;
pixel_data_size = 200 * ((200 * (24 / 8)));
bmpInfoHeader.biSizeImage = pixel_data_size;
BITMAPINFO dbmi;
ZeroMemory(&dbmi, sizeof(dbmi));
dbmi.bmiHeader = bmpInfoHeader;
dbmi.bmiColors->rgbBlue = 0;
dbmi.bmiColors->rgbGreen = 0;
dbmi.bmiColors->rgbRed = 0;
dbmi.bmiColors->rgbReserved = 0;
void* bits;
bits = NULL;
hbitmap = CreateDIBSection(NULL, &dbmi, DIB_RGB_COLORS, &bits, NULL, 0);
if (hbitmap == NULL) {
MessageBox(NULL, L"Bitmap not loaded", L"Error", MB_OK);
return 0;
}
GdiFlush();
// copy pixels into DIB.
memcpy(bits, _bitampContent, sizeof(_bitampContent));
GetObject(hbitmap, sizeof(BITMAP), &bitmap);
PAINTSTRUCT paintStruct;
hdc = BeginPaint(hWnd, &paintStruct);
hmemdc = CreateCompatibleDC(hdc);
SelectObject(hmemdc, hbitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hmemdc, 0, 0, SRCCOPY);
DeleteObject(hbitmap);
EndPaint(hWnd, &paintStruct);
return 0;
My problem is now, that the painted image is always black. I assume that this behavior has something to do with the DC's but I don't know exactly what.
What do I need to change?

Transparency in GDI DCs

I have the "simple" goal of drawing a bitmap with some transparency around it on the screen. That bit wasn't so hard:
#include <windows.h>
#include "BBKG.h"
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static int wH = 156;
static int wW = 166;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC mem0, mem1;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
mem0 = CreateCompatibleDC(0);
mem1 = CreateCompatibleDC(0);
SelectObject(mem0, hbmColour);
SelectObject(mem1, hbmMask);
SetBkColor(mem0, crTransparent);
BitBlt(mem1, 0, 0, bm.bmWidth, bm.bmHeight, mem0, 0, 0, SRCCOPY);
BitBlt(mem0, 0, 0, bm.bmWidth, bm.bmHeight, mem1, 0, 0, SRCINVERT);
DeleteDC(mem0);
DeleteDC(mem1);
return hbmMask;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
hInst = hInstance;
MSG msg;
HWND hwnd;
WNDCLASSW wc;
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = L"nope";
wc.hInstance = hInst;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
hwnd = CreateWindowW(wc.lpszClassName, L"",
WS_VISIBLE | WS_POPUP| WS_EX_TRANSPARENT,
100, 100, wW, wH, NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
//Workaround for focusables stealing my Esc key
if (msg.message == WM_KEYDOWN){
if (msg.wParam == VK_ESCAPE) {
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static int px;
static int py;
static HBITMAP bhbm;
static RECT nRect = { 0, 0, wW, wH };
switch (msg)
{
case WM_CREATE:
{
HWND bb = CreateWindowW(L"STATIC", L"",
WS_VISIBLE | WS_CHILD ,
0, 0, wW, wH,
hwnd, (HMENU)11, hInst, NULL);
//SetTimer(hwnd, 1, 80, NULL);
return 0;
}
case WM_PAINT: {
//Vars
RECT wRect;
if (GetUpdateRect(hwnd, &wRect, FALSE) == 0) {
return 0; //Nothing to paint
}
PAINTSTRUCT gps;
PAINTSTRUCT ps;
BeginPaint(hwnd, &gps);
HWND bb = GetDlgItem(hwnd, 11);
HDC bbhdc = BeginPaint(bb, &ps);
HDC mdc = CreateCompatibleDC(bbhdc);
//Load Image
BITMAP pBM;
HBITMAP pHBM = (HBITMAP)LoadImage(NULL, L"twi00.bmp", 0, 0, 0, LR_LOADFROMFILE);
HBITMAP pMBM = CreateBitmapMask((HBITMAP)pHBM, 0x00000000);
GetObject(pHBM, sizeof(pBM), &pBM);
//Paint
HBITMAP oldBM = (HBITMAP)SelectObject(mdc, pMBM);
BitBlt(bbhdc, 0, 0, pBM.bmWidth, pBM.bmHeight, mdc, 0, 0, SRCAND);
SelectObject(mdc, pHBM);
BitBlt(bbhdc, 0, 0, pBM.bmWidth, pBM.bmHeight, mdc, 0, 0, SRCPAINT);
//Cleanup
SelectObject(mdc, oldBM);
DeleteObject(pHBM);
DeleteObject(pMBM);
DeleteDC(mdc);
EndPaint(bb, &ps);
EndPaint(hwnd, &gps);
return 1;
}
case WM_ERASEBKGND: {
return 0;
}
case WM_DESTROY:
{
DeleteObject(bhbm);
PostQuitMessage(0);
return 0;
}
case WM_LBUTTONDOWN:
SetCapture(hwnd);
px = LOWORD(lParam);
py = HIWORD(lParam);
return 1;
case WM_LBUTTONUP:
{
ReleaseCapture();
return 1;
}
case WM_MOUSEMOVE:
{
if (GetCapture() == hwnd)
{
RECT rcWindow;
GetWindowRect(hwnd, &rcWindow);
SetWindowPos(hwnd, NULL, rcWindow.left + LOWORD(lParam) - px, rcWindow.top + HIWORD(lParam) - py, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
break;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
Use any generic bmp with a black border will do, I used this:
Now the question is, how can I make it so that when I move the window (click/drag) the background updates? I was hoping for something like putting the bitmap into a transparent window so that it's overlayed on top of things but it seems to just grab the pixels of what ever is behind it.
I'm attempting to do this without GDI+ or other libraries, if possible.
CreateWindow() does not accept extended window styles, such as WS_EX_TRANSPARENT (which is why it has EX in its name). You have to use CreateWindowEx() instead:
hwnd = CreateWindowExW(WS_EX_TRANSPARENT,
wc.lpszClassName, L"",
WS_VISIBLE | WS_POPUP,
100, 100, wW, wH, NULL, NULL, hInst, NULL);
A better option is to create a layered window (see also this) by using the WS_EX_LAYERED extended style). Then you can use the UpdateLayeredWindow() function to provide the window with the bitmap and the transparent color (you can also specify alpha as well). Let the window manage all of the hard work of drawing the bitmap transparently for you.
Your WndProc() can also respond to the WM_NCHITTEST message to tell the OS that all clicks on the window should be treated as if the user were clicking on the window's titlebar. Let the window handle the mouse tracking and auto-positioning for you.
Try something more like this:
#include <windows.h>
HINSTANCE hInst;
static int wH = 156;
static int wW = 166;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
hInst = hInstance;
WNDCLASSW wc = {0};
wc.lpszClassName = L"nope";
wc.hInstance = hInst;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
HWND hwnd = CreateWindowEx(WS_EX_LAYERED,
wc.lpszClassName, L"",
WS_POPUP, 100, 100, wW, wH, NULL, NULL,
hInst, NULL);
HBITMAP hBmp = (HBITMAP) LoadImage(NULL, L"twi00.bmp", 0, 0, 0, LR_LOADFROMFILE);
HDC hdcScreen = GetDC(0);
HDC hdcBmp = CreateCompatibleDC(hdcScreen);
HBITMAP oldBM = (HBITMAP) SelectObject(hdcBmp, hBmp);
POINT pt = {0};
UpdateLayeredWindow(hwnd,
hdcScreen,
NULL, NULL,
hdcBmp, &pt,
RGB(0, 0, 0), // black
NULL, ULW_COLORKEY
);
SelectObject(hdcBmp, oldBM);
DeleteDC(hdcBmp);
ReleaseDC(0, hdcScreen);
DeleteObject(hBmp);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
//Workaround for focusables stealing my Esc key
if ((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE) {
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_NCHITTEST:
{
return HTCAPTION;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}

How can I capture a transparent or translucent window in win32

I want to have a transparent window which has only borders and than capture it (and see all the windows behind it - since its transparent) to bitmap.
I managed to create the transparent window but when I try to capture it than it ceases to be transparent and I get it original background.
This is what I have so far,
creating the window:
//registering the window class with white backgournd
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = ScreenCaptureROIWinProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = _WPModule.GetHInstance();
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = CreateSolidBrush(0x00FFFFFF);//white brush;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_lpszClassName;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
//creating a layered window
m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_LAYERED,g_lpszClassName, NULL,
WS_THICKFRAME, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,
NULL, hInstance(), NULL);
// Set white brushed sections of the window to be transparent
SetLayeredWindowAttributes(m_hWnd, 0x00FFFFFF, 0, LWA_COLORKEY);
capturing the window:
RECT ROIRect;
GetWindowRect(m_hWnd,ROIRect);
HDC hdcScreen = GetDC(m_hWnd);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
BitBlt(
hdcMem,
0,
0,
ROIRect.right - ROIRect.left, //width of window
ROIRect.bottom - ROIRect.top, //height of window
hdcScreen,
0,
0,
SRCCOPY);

GDIplus flickering

I'm trying to draw with GDIplus on an transparent window and experiencing a lot of flickering. I've read plenty of threads that suggest implementing double-buffering or rendering to offscreen surface would help, which I've done but to no avail.
Any idea what I've done wrong and how to fix it?
Thanks.
#include <Windows.h>
#include <stdio.h>
#include <time.h>
#include <GdiPlus.h>
#pragma comment(lib, "GdiPlus.lib")
HWND hWnd = NULL;
WNDCLASSEX wcex;
HDC hdc = NULL;
PAINTSTRUCT ps;
HGDIOBJ hfDefault;
MSG msg;
COLORREF transKey = RGB(37,14,103);
ULONG_PTR m_gdiplusToken;
char window_title[9] = "testings";
char window_class[9] = "testings";
int window_width = GetSystemMetrics(SM_CXSCREEN);
int window_height = GetSystemMetrics(SM_CYSCREEN);
DWORD MainThread(LPVOID lpArgs)
{
while(1)
{
RECT rc = {0,0,window_width,window_height};
InvalidateRect(hWnd, &rc, false);
UpdateWindow(hWnd);
Sleep(50);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_CREATE)
{
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
SetLayeredWindowAttributes(hWnd, transKey, 128, LWA_COLORKEY);
}
else if(msg == WM_PAINT)
{
hdc = BeginPaint(hWnd, &ps);
Gdiplus::Graphics g(hdc);
g.Clear(Gdiplus::Color(37,14,103));
Gdiplus::Bitmap* curBitmap = new Gdiplus::Bitmap(window_width, window_height);
Gdiplus::Graphics* g1 = g.FromImage(curBitmap);
static int x=1,y=1;
// x += 3;
y += 2;
Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 255));
g1->DrawLine(&pen, x, y, window_width/2, window_height/2);
g.DrawImage(curBitmap, 0, 0);
EndPaint(hWnd, &ps);
}
else if(msg == WM_CLOSE)
{
DestroyWindow(hWnd);
}
else if(msg == WM_DESTROY)
{
PostQuitMessage(0);
}
else
{
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HBRUSH hbrBackground = CreateSolidBrush(transKey);
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = hbrBackground;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = window_class;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if(!RegisterClassEx(&wcex))
return 1;
hWnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
window_class,
window_title,
WS_POPUP,
0,
0,
window_width,
window_height,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return 1;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)MainThread, NULL, NULL, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return msg.wParam;
}
You have the WS_EX_TRANSPARENT attribute on your window, which causes the window underneath it to repaint every time your window is invalidated. Try removing it.
The meaning of WS_EX_TRANSPARENT is to tell Windows that you won't be drawing on the full window surface (i.e. leaving parts of it transparent), so it needs to make sure everything underneath is rendered first so it can show through.

C++ WINAPI setting window background image doesn't work

I hate that I have done it before, I feel like I am missing something and I don't know what it is. So, I am trying to set the background image of a windows in C++ WinAPI.
My Resources.rc
#include "Resources.h"
BACKGROUND BITMAP "background.bmp"
My Resources.h
#define BACKGROUND 200
My Main.cpp
....
WNDCLASSEX wcls;
wcls.cbSize = sizeof(WNDCLASSEX);
wcls.style = CS_PARENTDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
wcls.lpfnWndProc = WndProc;
wcls.cbClsExtra = 0;
wcls.cbWndExtra = 0;
wcls.hInstance = hInstance;
wcls.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(HICON));
wcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wcls.hbrBackground = HBRUSH(COLOR_WINDOW + 1);
wcls.lpszClassName = L"WndClass";
wcls.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(HICON));
HWND Window = CreateWindowEx (
WS_EX_LAYERED|WS_EX_TOOLWINDOW,
wcls.lpszClassName,
L"Window Title",
WS_VISIBLE|WS_POPUP|WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
384,
128,
NULL,
NULL,
hInstance,
NULL
);
if (!Window ) return 0;
SetLayeredWindowAttributes(
Window,
RGB(35, 35, 35),
85,
LWA_ALPHA|LWA_COLORKEY
);
SetWindowPos(
Window,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE|SWP_NOREDRAW|SWP_NOSIZE
);
ShowWindow(Window, SW_SHOWNORMAL);
UpdateWindow(Window);
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM Wpar, LPARAM Lpar) {
HBITMAP Background = NULL;
BITMAP BgImg;
HDC DeviceContext, BgContext;
PAINTSTRUCT PaintStruct;
switch (Msg) {
case WM_PAINT:
ContextLansator = BeginPaint(hWnd, &PaintStruct);
Fundal = LoadBitmap(hInstance, MAKEINTRESOURCE(BACKGROUND));
FundalLansator = CreateCompatibleDC(DeviceContext);
SelectObject(DeviceContext, Background);
GetObject(Background, sizeof(BgImg), &BgImg);
BitBlt(DeviceContext, 0, 0, 384, 128, BgContext, 0, 0, SRCAND);
DeleteObject(Background);
DeleteDC(BgContext);
EndPaint(hWnd, &PaintStruct);
break;
.....
I am using Code::Blocks. The resource is embedded correctly, the window starts but just a white background, the image is not painted. The WM_PAINT message is called once, at the beginning. I fill like something is wrong with BgImg but I don't know what. Some help needed! Thanks!
Create a PatternBrush and set it to the background before registering class. For example:
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPIBACKGROUND));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = CreatePatternBrush(LoadBitmap( hInstance, MAKEINTRESOURCE( IDB_BITMAP1 ) ) );
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINAPIBACKGROUND);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
It looks like this:
Bitmap depth shouldn't matter. But take a look at the styles in your implementation.
i use this code and it work fine for me.
i use code block 4.7 too.
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
BITMAP bitmap;
HDC hdcMem;
HGDIOBJ oldBitmap;
hBitmap01 = (HBITMAP)LoadImage(NULL, "c:\\energy.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hdc = BeginPaint(wnd01, &ps);
hdcMem = CreateCompatibleDC(hdc);
oldBitmap = SelectObject(hdcMem, hBitmap01);
GetObject(hBitmap01, sizeof(bitmap), &bitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
EndPaint(wnd01, &ps);
}
After 20 hours working on what I think is a similar symptom, I found a solution.
My problem was bitmap not loading. I verified the file was truly bitmap and code correct. I corrected and recompiled but Code::Blocks would not load it (return = 0). Try deleting, the two folders BIN and OBJ and the two files xxx.depend and xxx.layout. Then recompile and run.