I have a form which I make click-through using the following function calls:
SetWindowLong(Handle, GWL_EXSTYLE, (IntPtr)(GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
SetLayeredWindowAttributes(Handle, 0, 0xFF, LWA_ALPHA);
This works fine, however when I try to fade that window using the System.Windows.Forms.Form.Opacity
property I get the following exception:
System.ComponentModel.Win32Exception (0x80004005): The parameter is not valid
at System.Windows.Forms.Form.UpdateLayered()
at System.Windows.Forms.Form.set_Opacity(Double value)
How can I achieve both things at the same time?
The following works on a windows form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern System.UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
static extern int SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte bAlpha, uint dwFlags);
public const int GWL_EXSTYLE = -20;
public const uint WS_EX_LAYERED = 0x80000;
public const uint LWA_ALPHA = 0x2;
public const uint LWA_COLORKEY = 0x1;
public Form1()
{
InitializeComponent();
IntPtr Handle = this.Handle;
UInt32 windowLong = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong32(Handle, GWL_EXSTYLE, (uint)(windowLong ^ WS_EX_LAYERED));
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
}
}
}
User contributions licensed under CC BY-SA 3.0