Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a value int -can be retrieved as intopacitylevel- that controls opacity of an element on a Form. When I run my application, the value is set to a number of the random range 1 - 255.
How could I put the following together as a mousewheel solution in c#?: What I want is, moving the mwheel up or down will constantly change the opacity value to the minimum or maximum value depending on mwheel movement direction by adding/subtracting 1 to the current value every 1ms.
This behaviour precisely:
For up direction, stop value increase only when the mwheel was moved to the next spot upwards or when the value is at the maximum 255. For down direction, stop value decrease only when the mwheel was moved to the next spot downwards or when the value is at the minimum 1.
This .gif shows the result I want: at this place was a picture in the past

What I have tried:

I'm new to c#, and I'm not very certain or very flexible with the options I have yet.
At the first place I'm unsure how I should define the specific mousewheel rules and combined with a timer?/flag? to decrease/increase a value correctly.
I think I figured out a way to check if mousewheel moved up or down at least.
I'm not sure if it makes a lot sense to start like that, but this was my first approach in the right direction eventually.
C#
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
  int mousedeltaval = e.Delta / 120
   if (mousedeltaval == 1) //mousewheel up move
   {
   }
   if (mousedeltaval == -1) //mousewheel down move
   {
   }
}
Posted
Updated 10-Nov-19 6:25am
v14

You can not change the Forms background like this, but you can do it for a Panel:
public Form1()
{
    InitializeComponent();
    this.panel1.MouseWheel += MyMouseWheel;
}

private void MyMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
    int mousedeltaval = e.Delta / 12;
    int alpha = this.panel1.BackColor.A + mousedeltaval;

    if (alpha > -1 && alpha < 255)
    {
        this.panel1.BackColor = Color.FromArgb(alpha, this.panel1.BackColor);
        this.panel1.Invalidate();
    }
}
 
Share this answer
 
Comments
Member 13920025 6-Nov-19 8:27am    
Thanks so far, but this doesn't answer the question I think.
I didn't ask specifically how to control opacity on a Form.
The opacity control works fine with Form's background image, it's defined already as intopacityvalue.
I used .dll invoking as native solution to make that possible.
I'd like to see a solution that works with my already as int defined value intopacityvalue and the mousewheel events behaviour I explained above to increase/decrease the value by 1 per ms and stop under the right conditions.
I might be wrong, but it doesn't look like the first answer I got, takes care of everything yet.
RickZeeland 6-Nov-19 8:39am    
Ah, now I see what you want with a Timer, I'll see what I can do :)
Member 13920025 6-Nov-19 8:43am    
Thanks a lot.
The third try:
public partial class Form1 : Form
{
    Timer myTimer = new Timer();
    int mousedeltaval = 0;
    bool running;

    public Form1()
    {
        InitializeComponent();
        myTimer.Interval = 100;
        myTimer.Tick += MyTimerTick;
        this.panel1.MouseWheel += MyMouseWheel;
    }

    /// <summary>
    /// Change the transparency of panel1.
    /// </summary>
    private void MyMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (running)
        {
            running = false;
            myTimer.Stop();
            System.Threading.Thread.Sleep(100);
        }
        else
        {
            if (e.Delta < 0)
            {
                mousedeltaval = -10;
            }
            else
            {
                mousedeltaval = 10;
            }

            running = true;
            myTimer.Start();
        }
    }

    private void MyTimerTick(Object myObject, EventArgs myEventArgs)
    {
        int alpha = this.panel1.BackColor.A + mousedeltaval;

        if (alpha > -1 && alpha < 255)
        {
            this.panel1.BackColor = Color.FromArgb(alpha, this.panel1.BackColor);
            this.panel1.Invalidate();
        }

        if (alpha < 1 || alpha > 254)
        {
            running = false;
            myTimer.Stop();
        }
    }
}

To reduce flicker, see: drawing - how to stop flickering C# winforms - Stack Overflow[^]
 
Share this answer
 
v4
Comments
Member 13920025 6-Nov-19 11:35am    
Thanks. I tested the code in a new Form, but I see no effect.
I must do something wrong(temporary added the code of my Form1 to the question).
I added a "panel1" with red Back Color in Design view to the window, dragged a "timer1" onto panel1.
What else could I miss here?
In the current solution, the expected result would be that mousewheel up increases opacity level of "panel1" constantly, next mousewheel up stop it and when the value is 255 stop automatically and the opposite behaviour with mousewheel down and the minimum value of 1?
RickZeeland 6-Nov-19 12:42pm    
You don't need to drag a timer on the form as it is defined completely in code. using mousewheel up and down the transparency should change.
Member 13920025 6-Nov-19 13:01pm    
OK. What else could be wrong, if the error is not in the code?
I added the form1 code in a new project and a panel1 to the window with red back color.
RickZeeland 6-Nov-19 13:24pm    
Bad mouse :)
Member 13920025 6-Nov-19 13:31pm    
Perhaps a problem with focus, when the mouse is fully functional?

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