Search code examples
c#mouseeventlayered-windows

c# - If condition: mouse key clicked and bAlpha


In the topic below there were some suggestions on how to create a switch for two methods
with one button:
how-can-i-switch-between-two-methods-in-one-button-with-every-click

How could I achieve something like this with one specific Mouse button(switching between two LayeredWindowsAttributes for example)?

Or how could I code the following?
Right Clicked && bAlpha = 10: Set transparency value to 255
Right Clicked && bAlpha = 255: Set transparency value to 10

The main problem here is probably that I don't know how I check for bAlpha as if condition.

This is how I set the bAlpha value:

private void Form1_MouseDown(object sender, MouseEventArgs e)
  {
   SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);

        if (e.Button == MouseButtons.Right)
        {
            SetLayeredWindowAttributes(Handle, 0, 10, LWA_ALPHA);
            //SetLayeredWindowAttributes(Handle, 0, 255, LWA_ALPHA);
        }
  }

Solution

  • bool _transparent;
    
    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
        if (e.Button == MouseButtons.Right)
        {
            _transparent = !_transparent;
            byte alpha = (byte)(_transparent ? 10 : 255);
            SetLayeredWindowAttributes(Handle, 0, alpha, LWA_ALPHA);
        }
    }