0
 while (panel6.Left != 0) //0 is the end postion and it'll start at -600
 {
     panel6.Left += 10;
     Thread.Sleep(50);
 }

This is the code I use to fade in a panel and to fade in works perfectly https://i.vgy.me/QE5M15.gif

this is the glitch I mean when fading out it like idk how to explain it

I've tried using a costum panel code from S.O. but that didn't work either, I also tried changing the alpha in a loop but that also glitches

I tried adding

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

that didn't work either

Also tried

async void Move()
{
    while (panel7.Left != -600) //0 is the end postion and it'll start at -600
    {
        panel7.Left -= 10;
        Task.Delay(5);
    }
}

but now the panel just disappears and doesn't do the slide effect

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
  • You have a background image, part of some control. The control that holds the Image should have these styles set: `ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer`. `Thread.Sleep()` is not a good idea. An `async` proc with `Task.Delay()` will work better here. – Jimi Jun 09 '19 at 16:02
  • 1
    It is remarkable that it doesn't glitch worse than that. You can't hang the UI thread in a loop like that and expect parts of the window to still paint correctly. Are you doing this in a worker thread? Uh-oh. Use a Timer instead. – Hans Passant Jun 09 '19 at 16:07
  • https://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls – Hans Passant Jun 09 '19 at 16:19
  • @Hans Passant IIRC, that method loses its *effect* if the Form is minimized/maximized. Do you know how to make it stick (without recreating the handle, that is)? – Jimi Jun 09 '19 at 17:20
  • @Jimi check edit – Kyllian Lissens Jun 09 '19 at 19:22
  • @HansPassant Check edit – Kyllian Lissens Jun 09 '19 at 19:22
  • Where did you put those lines of code? `while (panel7.Left != -600) panel7.Left -= 10;`? Are you sending the Panel off-screen? Starting from where? How are you calling the async method? From where? Post the complete code. – Jimi Jun 09 '19 at 19:26
  • @Jimi https://pastebin.com/raw/mVLVJw57 The panel starts off screen (-600) and I'm 1 trying to make panel slide in which works fine, and 2 make the panel slide off which doesn't work – Kyllian Lissens Jun 09 '19 at 19:40
  • Using the same settings that I proposed: [Sliding Panel](https://imgur.com/a/CHMtxLx). – Jimi Jun 09 '19 at 20:40

1 Answers1

0

Try these modifications:

  • All elements are animated using the async/await patter; Task.Run() for the gradient color
  • The sliding Panel works like in the example I posted.
  • All procedures are non-blocking, so the animations work when the Form is faded in and out or moved around

Set the Form's AutoScaleMode to AutoScaleMode.Dpi and possibly make your application DPIAware, if you haven't already.

public partial class Main_Menu3 : Form
{
    bool Active;
    //Color setcolor = new Color();
    Color StartColor = Color.FromArgb(91, 65, 193);
    Color EndColor = Color.FromArgb(242, 90, 95);
    int Steps = 30;

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();

    public Main_Menu3()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | 
                      ControlStyles.UserPaint | 
                      ControlStyles.OptimizedDoubleBuffer, true);
        this.UpdateStyles();

        InitializeComponent();
    }

    public static IEnumerable<Color> GetGradients(Color start, Color end, int steps)
    {
        int stepA = ((end.A - start.A) / (steps - 1));
        int stepR = ((end.R - start.R) / (steps - 1));
        int stepG = ((end.G - start.G) / (steps - 1));
        int stepB = ((end.B - start.B) / (steps - 1));

        for (int i = 0; i < steps; i++) {
            yield return Color.FromArgb(start.A + (stepA * i),
                                        start.R + (stepR * i),
                                        start.G + (stepG * i),
                                        start.B + (stepB * i));
        }
    }

    private async Task SetGradients()
    {
        IEnumerable<Color> colors = GetGradients(StartColor, EndColor, Steps);

        while (true) {
            foreach (var color in colors) {
                if (color == colors.Last()) {
                    colors = colors.Reverse();
                    break;
                }
                await Task.Delay(1);
                this.BeginInvoke(new MethodInvoker(() => { label6.ForeColor = color; }));
            }
        }
    }

    private void CustomButton1_MouseEnter(object sender, EventArgs e)
    {
        customButton1.ForeColor = Color.FromArgb(255, 255, 255);
    }

    private void CustomButton1_MouseLeave(object sender, EventArgs e)
    {
        customButton1.ForeColor = Color.FromArgb(100, 100, 100);
    }

    private void Main_Menu3_Load(object sender, EventArgs e)
    {
        if (!Directory.Exists("C://RC-M_Theme")) {
            Directory.CreateDirectory("C://RC-M_Theme");
        }
        if (File.Exists("C://RC-M_Theme/backgroundimage.png")) {
            this.BackgroundImage = new Bitmap("C:/RC-M_Theme/backgroundimage.png");
        }
    }

    private void Main_Menu3_Shown(object sender, EventArgs e) 
        => Task.Run(async ()=> await SetGradients());

    private async void Main_Menu3_Activated(object sender, EventArgs e)
    {
        this.Active = true;
        base.Opacity = 0.75;
        for (int i = 1; i <= 25; i++) {
            bool active = this.Active;
            if (active) {
                base.Opacity += 0.01;
                await Task.Delay(1);
            }
        }
    }

    private async void Main_Menu3_Deactivate(object sender, EventArgs e)
    {
        try {
            this.Active = false;
            base.Opacity = 0.75;
            for (int i = 1; i <= 25; i++) {
                bool flag = !this.Active;
                if (flag) {
                    base.Opacity -= 0.01;
                    await Task.Delay(1);
                }
            }
        }
        catch { }
    }

    private void Panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

    private void Main_Menu3_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

    private async void pictureBox1_Click(object sender, EventArgs e)
    {
        await ShowPanel(panel7, 50);
    }

    private async void pictureBox2_Click(object sender, EventArgs e)
    {
        await HidePanel(panel7, 50);
    }

    int scrollStep = 20;
    bool isPanelShowing = false;

    private async Task ShowPanel(Panel panel, int delay)
    {
        if (isPanelShowing) return;
        isPanelShowing = true;
        while (panel.Left < 0) {
            panel.Left += scrollStep;
            await Task.Delay(delay);
        }
        isPanelShowing = false;
    }

    private async Task HidePanel(Panel panel, int delay)
    {
        if (isPanelShowing) return;
        isPanelShowing = true;
        while (panel.Left > -panel.Width) {
            panel.Left -= scrollStep;
            await Task.Delay(delay);
        }
        isPanelShowing = false;
    }

    private async void FadeIn(Control ctrl, int interval = 80)
    {
        ctrl.BackColor = Color.FromArgb(0, 39, 42, 52);
        while (ctrl.BackColor.A < 245) {
            await Task.Delay(interval);
            ctrl.BackColor = Color.FromArgb(ctrl.BackColor.A + 10, 39, 42, 52);
        }
        ctrl.BackColor = Color.FromArgb(255, 39, 42, 52);
    }

    private async void FadeOut(Form form, int interval = 80)
    {
        Color c = form.BackColor;
        //Object is fully visible. Fade it out
        while (form.Opacity > 0.1f) {
            await Task.Delay(interval);
            form.Opacity -= 0.05f;
        }
        form.Opacity = 0.1f; //make fully invisible       
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61