English 中文(简体)
Windows Forms Opacity After Shown- C#
原标题:

I am tryig to fade-in a windows form using c# but it doesnt seem to work after I have shown the form. Is it possible to change the forms opacity after Ive shown it?

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Timers;

namespace ToolStrip
{
    public partial class Form1 : Form
    {
        Form ToolForm = new ToolForm();
        Form PropForm = new PropertyGrid();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ToolForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            ToolForm.Owner = this;
            ToolForm.Show();
            ToolForm.Location = new Point(50, 50);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PropForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            PropForm.Owner = this;
            PropForm.Show();
            PropForm.Location = new Point(50, 50);

            System.Timers.Timer aTimer = new System.Timers.Timer(10000);

            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 2000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            PropForm.Opacity = PropForm.Opacity - 0.25;
            Console.WriteLine(PropForm.Opacity);
        }
    }
}
最佳回答

because you r using System.Timers.Timer which is a multithread timer, in it s OnTimedEvent() it calls control created by another thread, which cause exception.

If you use System.Windows.Forms.Timer, it will work. i tested.

问题回答

Using your code (and creating the other necessary Form classes), I get a cross-threading exception the first time the timer fires and the event handler is called, as Benny suggests.

Making changes to your code to check InvokeRequired in the timer event handler, and use Invoke if necessary to change PropForm.Opacity, results in the opacity changing after the form is shown, as required.

Note that you probably want to start with an Opacity of 0, and increase it gradually - otherwise your form will start off completely solid and gradually fade out

I will mention in passing that Opacity will have no effect on some versions of Windows, though you say you have Opacity effects working elsewhere, so it shouldn t be that in this case.

Ive gotten it to work without timers:

        int Loop = 0;

        for (Loop = 100; Loop >= 5; Loop -= 10)
        {
            this.PropForm.Opacity = Loop / 95.0;
            this.PropForm .Refresh();
            System.Threading.Thread.Sleep(100);
        }

but i cant seem to change this example to fade-in instead of out.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签