c# - 如果条件:单击鼠标键和 bAlpha

c# - If condition: mouse key clicked and bAlpha

在下面的主题中,有一些关于如何为两种方法创建开关的建议
一键完成:

如何使用一个特定的鼠标按钮(例如在两个 LayeredWindowsAttributes 之间切换)实现类似的功能?

或者我如何编写以下代码?
右键单击 && bAlpha = 10:将透明度值设置为 255
右键单击 && bAlpha = 255:将透明度值设置为 10

这里的主要问题可能是我不知道如何检查 bAlpha 条件。

这是我设置 bAlpha 值的方式:

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);
        }
  }
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);
    }
}

最终版本(包括对 int 到字节转换的修复)。

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);
    }
}