Alternative to TransparencyKey property?

N

nicolasr

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
 
N

nicolasr

oops, forgot to say that I always get error:

"Wrong parameter" (translated from german!)

Nick
 
H

Herfried K. Wagner [MVP]

Hello,

nicolasr said:
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.
 
N

nicolasr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top