Alternative to TransparencyKey property?

  • Thread starter Thread starter nicolasr
  • Start date Start date
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
 
oops, forgot to say that I always get error:

"Wrong parameter" (translated from german!)

Nick
 
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.
 
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
 
Back
Top