-
[win32] - transparent and opacy
i build these function from VB6(programming language):
Code:
void Transparent()
{
long test=GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong( hwnd, GWL_EXSTYLE,test | WS_EX_LAYERED);
SetLayeredWindowAttributes (hwnd, clrBackColor, 0, LWA_COLORKEY);
const char *text;
text=to_string( GetLastError()).c_str();
MessageBox(NULL,text,"erro",MB_OK);
}
but i get an error from messagebox:
87:ERROR_INVALID_PARAMETER - The parameter is incorrect.
(these code is for hide the backcolor)
can anyone advice me?
-
Re: [win32] - transparent and opacy
Are you compiling as ASCII or UNICODE?
-
Re: [win32] - transparent and opacy
You should not call GetLastError unless the API function (SetLayeredWindowAttributes in your case) has failed.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
Are you compiling as ASCII or UNICODE?
sorry i don't know answer to that question... i'm using the Code Blocks IDE.
VictorN: yes... theres no error, on compiling, but no results on running. that's why i used the GetLastError()
-
Re: [win32] - transparent and opacy
MessageBox takes 4 parameters, The second and third parameters are typed as LPCTSTR. If you are compiling as UNICODE, then these become LPCWSTR which is const WCHAR* which is const wchar_t*. If you are compiling as ASCII, LPCTSTR becomes LPCSTR which is const char*. As text is defined as const char *, this is ASCII. If you are compiling as UNICODE, then MessageBox would be expecting a type of const wchar_t* and not const char* which is being provided - which could be the reason for the error 87. Try changing MessageBox to MessageBoxA().
Victor is quite right. The value returned by GetLastError() is only valid when an error has actually occured and the function in error states that the error number can be obtained by calling this function. Calling GetLastError() in a context in which it is not expected to be called can provide erronous information. SetLayeredWindowAttributes() returns 0 on failure and only in that case should GetLastError() be used.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
MessageBox takes 4 parameters, The second and third parameters are typed as LPCTSTR. If you are compiling as UNICODE, then these become LPCWSTR which is const WCHAR* which is const wchar_t*. If you are compiling as ASCII, LPCTSTR becomes LPCSTR which is const char*. As text is defined as const char *, this is ASCII. If you are compiling as UNICODE, then MessageBox would be expecting a type of const wchar_t* and not const char* which is being provided - which could be the reason for the error 87. Try changing MessageBox to MessageBoxA().
Victor is quite right. The value returned by GetLastError() is only valid when an error has actually occured and the function in error states that the error number can be obtained by calling this function. Calling GetLastError() in a context in which it is not expected to be called can provide erronous information. SetLayeredWindowAttributes() returns 0 on failure and only in that case should GetLastError() be used.
i change for MessageBoxA() and i get the same error message.
1 question: SetLayeredWindowAttributes() is only for main window and not child window?
now i tested the same code with main window and works fine.. so what is needed for work with child windows?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
Cambalinho
1 question: SetLayeredWindowAttributes() is only for main window and not child window?
now i tested the same code with main window and works fine.. so what is needed for work with child windows?
But do you ask this question instead of just read MSDN? :(:confused:
FYI: SetLayeredWindowAttributes function
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
When/where are you calling Transparent()?
inside of my class... it's a class member... so i can't hide the backcolor and make opacy?
-
Re: [win32] - transparent and opacy
But if read the MSDN documentation for SetLayeredWindowAttributes(), as pointed out by Victor, this function only works on child windows with Windows 8/8.1. What OS are you using? If you are using a Windows OS prior to version 8 then this function only works for top level windows. Read the MSDN on-line documentation!
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
But if read the MSDN documentation for SetLayeredWindowAttributes(), as pointed out by Victor, this function only works on child windows with Windows 8/8.1. What OS are you using? If you are using a Windows OS prior to version 8 then this function only works for top level windows. Read the MSDN on-line documentation!
my windows is 7, so is there another way for transparent and opacity?
-
Re: [win32] - transparent and opacy
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
i'm trying learn how use Regions, but i have 2 questions:
1 - i belive that i can do it pixel by pixel. but i'm confused... but i think that i can do it... at least i did with VB2010;
2 - with Regions, can i do opacy?
-
Re: [win32] - transparent and opacy
from here: http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
i read:
"With WS_EX_COMPOSITED set, all descendants of a window get bottom-to-top painting order using double-buffering. Bottom-to-top painting order allows a descendent window to have translucency (alpha) and transparency (color-key) effects, but only if the descendent window also has the WS_EX_TRANSPARENT bit set. Double-buffering allows the window and its descendents to be painted without flicker."
but i'm confused :(
i think that, when i create the main window, i must add the WS_EX_TRANSPARENT extended style and add the WS_EX_COMPOSITED with child window extended style.
but or i don't understand what these means or i did something wrong :(
because the STATIC control isn't created
-
Re: [win32] - transparent and opacy
-
Re: [win32] - transparent and opacy
With my test code (as posted previously), setting the main window as WS_EX_COMPOSITED and setting the static control WS_EX_TRANSPARENT has no effect as expected (non Windows 8 computer). I still get the static control created as if these styles weren't specified.
-
Re: [win32] - transparent and opacy
finally i found 1 solution(doublebuffering for avoid flickers and others):
(these code is in my label window procedure)
Code:
case WM_ERASEBKGND:
{
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
}
but i get these strange error on TransparenBlt():
"obj\Debug\main.o||In function `ZN5label7WndProcEP6HWND__jjl@16':"
"undefined reference to `_imp__TransparentBlt@44'"
the clrBackColor it's a COLORREF type.
isn't the 1st time that i get these strange error, can you please give me more information?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
Cambalinho
finally i found 1 solution(doublebuffering for avoid flickers and others):
(these code is in my label window procedure)
...
but i get these strange error on TransparenBlt():
...
It means you did NOT find a solution yet! :rolleyes:
Quote:
Originally Posted by
Cambalinho
get these strange error on TransparenBlt():
"obj\Debug\main.o||In function `ZN5label7WndProcEP6HWND__jjl@16':"
"undefined reference to `_imp__TransparentBlt@44'"
the clrBackColor it's a COLORREF type.
isn't the 1st time that i get these strange error, can you please give me more information?
It is a Linker error. Some .lib or .obj file containing TransparenBlt was not included (nor added) in your project.
Didn't you read the MSDN documentation about TransparenBlt ?
Didn't you add the Msimg32.lib library?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
VictorN
It means you did NOT find a solution yet! :rolleyes:
It is a Linker error. Some .lib or .obj file containing TransparenBlt was not included (nor added) in your project.
Didn't you read the MSDN documentation about TransparenBlt ?
Didn't you add the Msimg32.lib library?
you have right... i never thot that... thanks
and yes the code seems not working :(
maybe i will do it in other time
-
Re: [win32] - transparent and opacy
Quote:
finally i found 1 solution(doublebuffering for avoid flickers and others):
(these code is in my label window procedure)
I hope the version of this case code you use has a break statement at the end.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
I hope the version of this case code you use has a break statement at the end.
yes.. it have
-
Re: [win32] - transparent and opacy
see the 2 cases:
Code:
case WM_ERASEBKGND:
{
if (inst->blnTransparent==true)
{
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
}
}
break;
case WM_CTLCOLORSTATIC:
{
if (inst->blnTransparent==false)
{
HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, inst->clrTextColor);
SetBkMode(hdcStatic, TRANSPARENT);
SetBkColor(hdcStatic,inst->clrBackColor);
g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
return (LONG)g_hbrBackground;
}
}
break;
or the WM_ERASEBKGND message is ignored or the text override it:(
-
Re: [win32] - transparent and opacy
Quote:
or the WM_ERASEBKGND message is ignored
Have you put a break point on it in the debugger to see if you are getting this message? Or do what I had in one of my earlier code posts, output all message values received to a text file so then you can easily establish what messages are received in what order.
If you are receiving this message, then obviously the code is not doing what you expected - which is what exactly?
Also note from the documentation for WM_ERASEBKGND that if this message is processed, then you need to return a non-zero value. Returning a zero value indicates that the window will be erased and redrawn. Read the documentation! http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
Have you put a break point on it in the debugger to see if you are getting this message? Or do what I had in one of my earlier code posts, output all message values received to a text file so then you can easily establish what messages are received in what order.
If you are receiving this message, then obviously the code is not doing what you expected - which is what exactly?
Also note from the documentation for WM_ERASEBKGND that if this message is processed, then you need to return a
non-zero value. Returning a zero value indicates that the window will be erased and redrawn. Read the documentation!
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
i recive a black rectangle with the text:
Code:
case WM_ERASEBKGND:
{
if (inst->blnTransparent==true)
{
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
Sleep(2000);
return DefWindowProc(hwnd, msg, wParam, lParam);;
}
}
break;
so the problem is how i use bitblt()?
-
Re: [win32] - transparent and opacy
Code:
Sleep(2000);
return DefWindowProc(hwnd, msg, wParam, lParam);
You're supposed to return a non-zero value - not call defproc.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
Code:
Sleep(2000);
return DefWindowProc(hwnd, msg, wParam, lParam);
You're supposed to return a non-zero value - not call defproc.
now i changed to:
Code:
return (LONG)CreateSolidBrush(NULL_BRUSH)
-
Re: [win32] - transparent and opacy
Now you have a resource leak as you are creating a brush every time you change the background but you never delete the object.
See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Why don't you just return 1?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
you have right. but i continue with same result: a black rectangle :(
-
Re: [win32] - transparent and opacy
Code:
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
And you think this code does what? From where did you obtain it?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
Code:
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
And you think this code does what? From where did you obtain it?
copy the parent HDC to doublebuffer... copy the child HDC to doublebuffer(transparent... without backcolor) and then copy the doublebuffer to child HDC
-
Re: [win32] - transparent and opacy
You have checked, haven't you, that GetSize() and GetPosition() are returning the expected values?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
You have checked, haven't you, that GetSize() and GetPosition() are returning the expected values?
yes.. i have tested now:
Code:
struct Position
{
int X;
int Y;
};
struct Size
{
int Width;
int Height;
};
Position GetPosition()
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
Position crdSize = {LabelSize.left,LabelSize.top};
return crdSize;
}
Size GetSize()
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
return crdSize;
}
-
Re: [win32] - transparent and opacy
...and clrBackColor is set where?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
...and clrBackColor is set where?
Code:
void SetBackColor(COLORREF color)
{
clrBackColor=color;
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwnd,SW_SHOW);
}
color GetBackColor()
{
return clrBackColor;
}
-
Re: [win32] - transparent and opacy
...and SetBackColor() is called from where? And is it called before the WM_ERASEBKGND message is received?
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
...and SetBackColor() is called from where? And is it called before the WM_ERASEBKGND message is received?
it's a class member... the ShowWindow() helps me do the control update
-
Re: [win32] - transparent and opacy
But have you checked that SetBackColor() is called before the WM_ERASEBKGND message is recieved? If it is called after, then clrBackColor won't have been set before it's used in the message process code.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
But have you checked that SetBackColor() is called before the WM_ERASEBKGND message is recieved? If it is called after, then clrBackColor won't have been set before it's used in the message process code.
you came 1st?
WM_CTLCOLORSTATIC or WM_ERASEBKGND?
-
Re: [win32] - transparent and opacy
Output the received msg codes to a file or a debug window and see. It's for this sort of thing that I had the msg codes output to a file in an earlier example I posted.
-
Re: [win32] - transparent and opacy
Quote:
Originally Posted by
2kaud
Output the received msg codes to a file or a debug window and see. It's for this sort of thing that I had the msg codes output to a file in an earlier example I posted.
it's the WM_ERASEBKGND message.. is the 1st. and then cames the WM_CTLCOLORSTATIC message