Problem with SetLayeredWindowAttributes(...)

S

snovi28

My code should make a window 50% transparrent:

const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x00080000;
const int LWA_ALPHA = 0x00000002;

SetWindowLong( hMyWnd,
GWL_EXSTYLE,
GetWindowLong(hMyWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

if (!SetLayeredWindowAttributes(hMyWnd,
Color.FromArgb(0,0,0),
Convert.ToByte(255 * 0.50),
LWA_ALPHA))
{
// Error
}

This is in Microsoft Visual C# 2005 project. The methods are imported
from user32.dll.

The code compiles/links fine but during run-time I'm getting exception
for the call to SetLayeredWindowAttributes:

System.Runtime.InteropServices.MarshalDirectiveException was unhandled
Message="Cannot marshal 'parameter #2': Invalid managed/unmanaged
type combination."

Tried to play with Color.FromArgb(0,0,0) but could make it work. Any
ideas/suggestions?
 
S

Stoitcho Goutsev \(100\)

Hi,

Why do you want to use PInvoke? Form class has always supported Opacity
property the internally sets the window as layered. Just set the form's
Opacity=0.5 to get 50% transparency.
 
S

snovi28

Thanks for the suggestion but I'm not sure I can use Form.Opacity as
this is for a Outlook.Inspector window (in my VSTO 2005 Outlook add-in
project) and Outlook.Inspector objects don't expose Form methods.
Unless you know a thick of how this could be done, of course... If yes,
please let me know!

BTW - The main problem with SetLayeredWindowAttributes seems to be in a
mismatch for its second parameter - on managed side it expects a Color
structure and on the unmanaged side it's a UINT
(http://pinvoke.net/default.aspx/user32.SetLayeredWindowAttributes).
Strange...
 
S

Stoitcho Goutsev \(100\)

Well I don't know anything about VSTO, but SetLayeredWindowAttributes API as
any others Win32 API that accept colors expect COLORREF structure. You can
get COLORREF structure out of your .NET color by using
ColorTranslator.ToWin32 method.
 

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