0

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);
        }
  }
taraz
  • 117
  • 2
  • 8
  • You declare a boolean as in the other post, and then use that one inside the `Form1_MouseDown` method to find the correct value of the third `SetLayeredWindowAttributes` parameter (10 or 255 apparently). – Ray Oct 19 '19 at 23:07
  • You use if, just like you already did for e.Button. – Ray Oct 20 '19 at 11:54
  • Ok. I'm not sure how to reference the bAlpha parameter as the Layer alpha value properly. – taraz Oct 20 '19 at 12:26
  • You really only have to copy how the boolean is used in the linked question's answer. He's using a tertiary `?` operator there instead of an if, but that's basically doing the same in a shorter way. – Ray Oct 20 '19 at 12:30
  • Sorry. I cannot fully imagine how to apply this solution onto my code in the right format yet. Could someone provide a code sample, so I can see this with my own eyes? – taraz Oct 20 '19 at 12:49

2 Answers2

1
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);
    }
}
Ray
  • 7,940
  • 7
  • 58
  • 90
  • So I can name the byte integer value directly. Thank you, this answer was helpful. – taraz Oct 20 '19 at 14:44
  • I'm not sure what you mean with that; you do not have to store it in a local variables `alpha`, you can do `SetLayeredWindowAttributes(Handle, 0, (byte)(_transparent ? 10 : 255), LWA_ALPHA);` directly if you want that? – Ray Oct 20 '19 at 23:46
0

The final version(includes the fix for int to byte conversion).

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);
    }
}
taraz
  • 117
  • 2
  • 8