Sign in to follow this  

Layered Windows

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im trying to get my window to update using update UpdateLayeredWindow, but no luck so far.
void update(HWND hwnd){
	HDC pDC = GetDC(hwnd);
	HDC dcMem;
	dcMem = CreateCompatibleDC(pDC);
 
	HBITMAP bmp;
	//bmp = LoadBitmap(aInstance, "clockbg.bmp");
	bmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
	if(!bmp)MessageBox(NULL,"FAIL TO LOAD IMAGE", "ERROR",NULL);
 
	HBITMAP pOldBitmap = (HBITMAP) SelectObject(dcMem, bmp);
 
 
	BLENDFUNCTION blend;
	blend.BlendOp = AC_SRC_OVER;
	blend.BlendFlags = 0;
	blend.SourceConstantAlpha = 255;
	blend.AlphaFormat = AC_SRC_ALPHA;
 
	POINT ptSrc = {10, 10 };
	SIZE sz = { 100, 100 };
	UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, RGB(255,255,255), &blend, ULW_COLORKEY );
	DWORD dwError = GetLastError(); // <- this gives me 87
 
	DeleteDC(dcMem);
	DeleteObject(pDC);
}
thank you

Share this post


Link to post
Share on other sites
Well System Error Code 87 is ERROR_INVALID_PARAMETER.

Just glancing over your call to UpdateLayeredWindow(), it looks like the 7th parameter should be a pointer to a COLORREF value, but you're instead passing the value directly. That said, I don't know if that's what is causing the ERROR_INVALID_PARAMETER.

Share this post


Link to post
Share on other sites
yes i am using WS_EX_LAYERED
and UpdateLayeredWindow is returning 0, is failing.

#define WIN32_LEAN_AND_MEAN

#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED 0x00080000
#endif

#ifndef LWA_COLORKEY
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#endif

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

HINSTANCE aInstance;

void update(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, char *args, int show) {
HWND hwnd; //window handle
MSG msg; //message
WNDCLASSEX windowClass; //window class
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW ;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = instance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDI_APPLICATION);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//(HBRUSH)GetStockObject(NewBrush); set window background colour
windowClass.lpszMenuName = NULL;//MAKEINTRESOURCE(IDR_MENU1); //add menu bar
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));

//register the window class
if(!RegisterClassEx(&windowClass))
return 0;

hwnd = CreateWindowEx(WS_EX_LAYERED,
"MyClass",
"Test",
WS_OVERLAPPEDWINDOW |
//WS_MINIMIZEBOX|
WS_VISIBLE |
WS_SYSMENU,
200, 100,
400, 350,
NULL,
NULL,
instance,
NULL);


MSG message;

while (GetMessage(&message, 0, 0, 0)) {
TranslateMessage(&message);
DispatchMessage(&message);
}

return message.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){

switch(message){
case WM_CREATE:
SetLayeredWindowAttributes(hwnd, 0, 240, LWA_ALPHA);
update(hwnd);
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
//ShowWindow(window, SW_HIDE );
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;

break;
default:
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}

void update(HWND hwnd){
HDC pDC = GetDC(hwnd);
HDC dcMem;
dcMem = CreateCompatibleDC(pDC);
COLORREF colour;
colour = RGB(255,255,255);
HBITMAP bmp;
//bmp = LoadBitmap(aInstance, "clockbg.bmp");
bmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
if(!bmp)MessageBox(NULL,"FAIL TO LOAD IMAGE", "ERROR",NULL);

HBITMAP pOldBitmap = (HBITMAP) SelectObject(dcMem, dcMem);
BLENDFUNCTION blend;
blend.BlendOp = AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;

POINT ptSrc = {10, 10 };
SIZE sz = { 100, 100 };
//UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA );
int p = UpdateLayeredWindow(hwnd, NULL, NULL, NULL, dcMem, NULL, colour, &blend, LWA_ALPHA );
DWORD dwError = GetLastError(); // <- this gives me 87

DeleteDC(dcMem);
DeleteObject(pDC);
}

Share this post


Link to post
Share on other sites
I notice that in WM_CREATE you call SetLayeredWindowAttributes followed by update(), which calls UpdateLayeredWindow.

MSDN says:
Quote:
Note that once SetLayeredWindowAttributes has been called for a layered window, subsequent UpdateLayeredWindow calls will fail until the layering style bit is cleared and set again.
You can't mix the two calls. It's one or the other.

Share this post


Link to post
Share on other sites

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this