How to draw partially transparent bitmap inside a fully transparent window? - c++

I have a fully transparent window:
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
wClass.hInstance = GetModuleHandleA(0);
wClass.lpfnWndProc = &Overlay::WinProc;
wClass.lpszClassName = overlay_window_name.c_str();
wClass.lpszMenuName = overlay_window_name.c_str();
wClass.style = CS_VREDRAW | CS_HREDRAW;
cid = RegisterClassEx(&wClass);
DWORD ex_style = 0;
DWORD style = 0;
ex_style = WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED;
style = WS_POPUP;
m_overlay_window = CreateWindowEx(ex_style, overlay_window_name.c_str(), overlay_window_name.c_str(), style, 0, 0, 0, 0, 0, 0, 0, this);
SetLayeredWindowAttributes(m_overlay_window, 0, 0, LWA_ALPHA);
SetLayeredWindowAttributes(m_overlay_window, 0, RGB(0, 0, 0), LWA_COLORKEY);
I am creating swap chain:
DXGI_SWAP_CHAIN_DESC desc{};
desc.Windowed = TRUE;
desc.BufferCount = 2;
desc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.SampleDesc.Count = 1; //multisampling setting
desc.SampleDesc.Quality = 0; //vendor-specific flag
desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
desc.OutputWindow = hwnd;
D3D_FEATURE_LEVEL levels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1
};
D3D_FEATURE_LEVEL feature_level;
UINT device_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE,
nullptr,
device_flags,
levels,
ARRAYSIZE(levels),
D3D11_SDK_VERSION,
&desc,
&m_swap_chain,
&m_device,
&feature_level,
&m_context
);
and then I want to draw half-transparent bitmap(through which I can see content of other windows):
m_render_target->DrawBitmap(m_bitmap, rect, 0.5, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
but this bitmap always not transparent. Only thing is going for him he becomes darker. May be something wrong with window or swap chain Im creating?

Related

Transparent bitmap 32bit as window background

How can I set a graphic in bmp 32bit format as window background with TRANSPARENCY? when i use bmp 28bit everything works but then there is no transparency. When i use 32bit bitmap with alpha the background is not rendered.
Application is in c ++ winapi.
bool ApplicationInit(HINSTANCE hinstance)
{
//HBITMAP hbmp = LoadBitmap(hinstance, MAKEINTRESOURCE(IDB_LAUNCHER_BG));
HBITMAP hbmp = (HBITMAP)LoadImage(hinstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
HBRUSH hbr = CreatePatternBrush(hbmp);
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = LauncherWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_ICON));
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = hbr;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = szWindowClass;
return RegisterClassEx(&wcx);
}
please help :)

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

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

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?

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

What is wrong with this context creation?

I would like to create a render context to use opengl.
The wglMakeCurrent(hdc, hrc) is returning true.
But for some reason OpenGL functions are ignored.
Here is the code for context creation:
int pixelFormat;
hdc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
pixelFormat = ChoosePixelFormat(hdc,&pfd);
SetPixelFormat(hdc,pixelFormat,&pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
And the window creation:
hinst = GetModuleHandle(NULL);
WNDCLASSEX wnd;
memset(&wnd, 0, sizeof(WNDCLASSEX));
wnd.hInstance = hinst;
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.lpszClassName = "__myGLApp__";
wnd.lpfnWndProc = WndProc;
RegisterClassEx(&wnd);
hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
"__myGLApp__",
"valami",
WS_VISIBLE,
xpos,
ypos,
width,
height,
NULL,
NULL,
hinst,
NULL);
Where am going I wrong?
you can use the glGetError() function to get an error code. You can also get a textual description with the functions gluErrorString() and gluErrorUnicodStringEXT() from the utility library. Something like this:
char *message;
message = gluErrorString(glGetError());
printf("OpenGL error: %s\n", message);