Hi all!
I'll keep this post a little shorter then I normally do, lack of time to post and all of that.
I am making a widget-type program. You know, no borderstyle, no taskbar, opacitycontrol, stuff like that.
It's going to be an RSS-aggregator by the way.
Now for my problem:
When I hover over the widget I would like it to raise the opacity to 1 (by doing an enormous load of this.Opacity += 0.01 through a timer) and when leaving the form, it should lower it the same way to what has been set up.
Now, I had the incrementing down, but the decrementing wouldn't work at all.
Here is the code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace RSSWidget
{
/// <summary>
/// RSS Widget, mainly used for Slashdot.org
/// </summary>
public partial class MainForm : Form
{
#region Variables
TrackBar TransparencyTrackBar;
Timer OpacityRaiseTimer;
Timer OpacityLowerTimer;
double TransparencyFormOpacityValue = 1;
#endregion
public MainForm()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(200, 300);
this.BackColor = Color.FromArgb(0, 0, 100);
this.ShowInTaskbar = false;
this.MouseHover += new EventHandler(MainForm_MouseHover);
this.MouseLeave += new EventHandler(MainForm_MouseLeave);
this.LostFocus += new EventHandler(MainForm_LostFocus);
RightClick();
OpacityRaiseTimer = new Timer();
OpacityRaiseTimer.Interval = 7;
OpacityRaiseTimer.Tick += new EventHandler(OpacityRaiseTimer_Tick);
OpacityLowerTimer = new Timer();
OpacityLowerTimer.Interval = 7;
OpacityLowerTimer.Tick += new EventHandler(OpacityLowerTimer_Tick);
}
void MainForm_LostFocus(object sender, EventArgs e)
{
if(this.Opacity >= TransparencyFormOpacityValue)
{
OpacityLowerTimer.Start();
}
else
{
OpacityLowerTimer.Stop();
}
this.FormBorderStyle = FormBorderStyle.None;
}
void MainForm_MouseLeave(object sender, EventArgs e)
{
if(this.Opacity >= TransparencyFormOpacityValue)
{
OpacityLowerTimer.Start();
}
else
{
OpacityLowerTimer.Stop();
}
this.FormBorderStyle = FormBorderStyle.None;
}
void MainForm_MouseHover(object sender, EventArgs e)
{
//
// This has a known bug, if you open TransparencyForm, then hover over the MainForm,
// the opacity will keep rising, not matter how you set the TrackBar.
// The workaround is to close the TransparencyForm and reload the TransparencyForm.
//
if(this.Opacity != 1.0)
{
OpacityRaiseTimer.Start();
}
else
{
OpacityRaiseTimer.Stop();
}
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
void OpacityLowerTimer_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
this.Opacity -= 0.01;
this.ResumeLayout();
}
void OpacityRaiseTimer_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
this.Opacity += 0.01;
this.ResumeLayout();
}
private void RightClick()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem m1 = new MenuItem();
MenuItem m2 = new MenuItem();
MenuItem m3 = new MenuItem();
m1.Text = "&Transparency";
m2.Text = "&Options";
m3.Text = "&Exit";
contextMenu.MenuItems.Add(m1);
contextMenu.MenuItems.Add(m2);
contextMenu.MenuItems.Add(m3);
m1.Click += new EventHandler(m1_Click);
m2.Click += new EventHandler(m2_Click);
m3.Click += new EventHandler(m3_Click);
this.ContextMenu = contextMenu;
}
void m2_Click(object sender, EventArgs e)
{
Form OptionsForm = new Form();
OptionsForm.Size = new Size(300, 300);
OptionsForm.Text = "Options";
OptionsForm.Show();
}
void m1_Click(object sender, EventArgs e)
{
Form TransparencyForm = new Form();
TransparencyForm.Size = new Size(120, 50);
TransparencyTrackBar = new TrackBar();
TransparencyTrackBar.Size = new Size(100, 30);
TransparencyTrackBar.Location = new Point(10, 10);
TransparencyTrackBar.Minimum = 0;
TransparencyTrackBar.Maximum = 90;
TransparencyTrackBar.Text = "Transparency";
TransparencyTrackBar.Value = (int)(RSSWidget.MainForm.ActiveForm.Opacity * TransparencyTrackBar.Maximum);
TransparencyTrackBar.Scroll += new EventHandler(TransparencyTrackBar_Scroll);
TransparencyForm.Controls.Add(TransparencyTrackBar);
TransparencyForm.Show();
}
void TransparencyTrackBar_Scroll(object sender, EventArgs e)
{
this.Opacity = ((Convert.ToDouble(TransparencyTrackBar.Value) / 100) + 0.1);
TransparencyFormOpacityValue = ((Convert.ToDouble(TransparencyTrackBar.Value) / 100) + 0.1);
}
void m3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Does anybody have an idea why this won't work? I'm testing it under Windows 7, if that makes any difference at all.
Thank you for any help in advance!