Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help one more time with the same problem basically.
I adjust the opacity of my form using Layered Windows style and I can reference a value with GWL_EXSTYLE to control the opacity level. I'm not sure why I have the trouble here, but with my solution so far I'm unsure how I should retrieve current opacity level of the form and adjust it automatically with a timer. I try to create the control behaviour with a mousewheel and a timer.
RickZeeland posted a fully functional example of the correct behaviour with regular panel opacity here.
This is exactly what I try to achieve. If someone could help me to implement the solution in this rather minimalistic setup with Layered Windows that I prepared, I could finally finish one of my apps that can handle even more complex images.

What I have tried:

I figured out how to control opacity of the form directly with mousewheel, but I need help with the right behaviour for automatic value control with a timer.
C#
this.MouseWheel += MyMouseWheel;

[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;


private void MyMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
 SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);

 int alpha2 = mousedeltaval + 255;
 var alpha = (byte)alpha2;
 SetLayeredWindowAttributes(Handle, 0, alpha, LWA_ALPHA);

   if (e.Delta < 1)
     if (alpha2 > 1)
     {
      mousedeltaval = mousedeltaval - 1;
     }
   if (e.Delta > 1)
      if (alpha2 < 254)
      {
       mousedeltaval = mousedeltaval + 1;
}}}
Posted
Updated 11-Nov-19 7:51am
v6
Comments
Richard MacCutchan 10-Nov-19 13:12pm    
You should post this as a Reply to RickZeeland's explanation, so he can help you.
Member 13920025 11-Nov-19 6:55am    
Ok, thanks for the tip.

1 solution

You were close !
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinMouse1
{
    /// <summary>
    /// Change the transparency of the form using the mousewheel
    /// </summary>
    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);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900