Layered Windows

Started by
4 comments, last by mutex 16 years, 11 months ago
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
Advertisement
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.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
i wish that was it but no, still get 87, it compiles and runs fine is just the UpdateLayeredWindow is doing nothing to the window.
Are you specifying WS_EX_LAYERED in your CreateWindowEx call? Is UpdateLayeredWindow return failure before you check the last error?
-Mike
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);
}
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.

This topic is closed to new replies.

Advertisement