You were close !
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WinMouse1
{
public partial class Form2 : Form
{
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int LWA_ALPHA = 0x2;
Timer myTimer = new Timer();
int mousedeltaval = 0;
int alpha = 255;
bool running;
public Form2()
{
InitializeComponent();
myTimer.Interval = 100;
myTimer.Tick += MyTimerTick;
this.MouseWheel += MyMouseWheel;
}
private void MyMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (running)
{
running = false;
myTimer.Stop();
System.Threading.Thread.Sleep(50);
return;
}
if (e.Delta < 0)
{
mousedeltaval = -10;
}
else
{
mousedeltaval = 10;
}
running = true;
System.Threading.Thread.Sleep(50);
myTimer.Start();
}
private void MyTimerTick(Object myObject, EventArgs myEventArgs)
{
alpha += mousedeltaval;
if (alpha > -1 && alpha < 255)
{
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, (byte)alpha, LWA_ALPHA);
return;
}
if (alpha < 0)
{
alpha = 0;
}
else
{
alpha = 255;
}
running = false;
myTimer.Stop();
Debug.Print("alpha = " + alpha);
}
}
}