Hi,
I'm trying to convert an application from C++ to CSharp.
Under C++ I used the SetLayeredWindowAttributes() API
to make parts of my form transparent. Now in CSharp, I
was trying the forms TransparencyKey property. However,
it doesn't work on my system with 32bit display and I found
this confirmed by other posts on this topic.

I asked myself if I could avoid this problem by dllimport-ing
the API function. I'm very new to CSharp and Dotnet and
could not get it working, yet. I would really appreciate if
anyone could tell me what is wrong in the following code:

//the original function signature
/*
BOOL SetLayeredWindowAttributes(HWND hwnd,
COLORREF crKey,
BYTE bAlpha,
DWORD dwFlags
);
*/



//define the key constant
private const int LWA_COLORKEY = 0x00000001;


[DllImport("user32.dll", SetLastError=true)]
public static extern int SetLayeredWindowAttributes(int hwnd,
int cKey, byte bAlpha, int dwFlags);


//in the forms constructor

int ret = SetLayeredWindowAttributes(this.Handle.ToInt32(), 0x00FFFFFF, 0,
LWA_COLORKEY);

if(ret == 0)
{
try
{
throw new Win32Exception();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}

thanks for any ideas,
Nick

Re: Alternative to TransparencyKey property? by nicolasr

nicolasr
Mon Sep 08 18:39:58 CDT 2003

oops, forgot to say that I always get error:

"Wrong parameter" (translated from german!)

Nick



Re: Alternative to TransparencyKey property? by Herfried

Herfried
Mon Sep 08 19:22:58 CDT 2003

Hello,

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> schrieb:
> I'm trying to convert an application from C++ to CSharp.
> Under C++ I used the SetLayeredWindowAttributes() API
> to make parts of my form transparent. Now in CSharp, I
> was trying the forms TransparencyKey property. However,
> it doesn't work on my system with 32bit display and I found
> this confirmed by other posts on this topic.
>
> I asked myself if I could avoid this problem by dllimport-ing
> the API function. I'm very new to CSharp and Dotnet and
> could not get it working, yet. I would really appreciate if
> anyone could tell me what is wrong in the following code:

I think the handle will return 0 because the window isn't yet created in
the constructor. You can check this by checking the value of the form's
'IsHandleCreated' property.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet



Re: Alternative to TransparencyKey property? by nicolasr

nicolasr
Tue Sep 09 05:26:58 CDT 2003

thanks for the help!
Yes the handle is 0. I moved the code into the Load event and now the error
is gone.
However, as with the TransparencyKey property the transparency only seems
to work with display color depths lower than 24bit. This makes all the new
layered window stuff practically unusable to me. I can't require the user to
change
it's display settings just to run my small program.
I did not have the problem when calling SetLayeredWindowAttributes() in my
Win32 C++ program version, though!

Nick