• 0

[.NET] Alpha-blended hover windows


Question

Hey guys, hope you're well.

Here's a quick question for you. I have a VB.NET project (though help in C# would be just as useful) and wish to emulate the alpha-blended hover windows that can be seen in Windows Live Messenger. Here's a screenshot:

post-32224-1236284228.png

Similarly the 'toast' popups when a contact signs in use a similar, alpha-blended effect.

Individual elements of this aren't so much of an issue - hovering and fading in/out can be achieved with relative ease, but the alpha-blended background-image of the window has got me stuck.

I've found tutorials that involve inheriting the standard Windows Form class and can produce entire forms that will take a 32-bit PNG as an image but I'm not convinced this is the most efficient manner of achieving this. Any thoughts?

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Yeah, but he is using a windows form. Why would you recommend the AJAX toolkit? That's for the web...

Link to comment
Share on other sites

  • 0

I think you can pull if off using a WPF control or form. You can use WPF controls in a Windows Forms and the other way around.

Edited by Doli
Link to comment
Share on other sites

  • 0
Hey guys, hope you're well.

Here's a quick question for you. I have a VB.NET project (though help in C# would be just as useful) and wish to emulate the alpha-blended hover windows that can be seen in Windows Live Messenger. Here's a screenshot:

post-32224-1236284228.png

Similarly the 'toast' popups when a contact signs in use a similar, alpha-blended effect.

Individual elements of this aren't so much of an issue - hovering and fading in/out can be achieved with relative ease, but the alpha-blended background-image of the window has got me stuck.

I've found tutorials that involve inheriting the standard Windows Form class and can produce entire forms that will take a 32-bit PNG as an image but I'm not convinced this is the most efficient manner of achieving this. Any thoughts?

I don't think its a bad way of doing things (but IMO applications should stick to the UI conformities of the parent OS ;)), but if your not happy doing it with PNGs, you can probably do something using DirectDraw. I'm not sure where to start looking though, I never do anything like this.

Link to comment
Share on other sites

  • 0

In normal apps I wouldn't dream of doing something like this, but for the purposes of an immersive experience I am. I'm constrained by .NET 2.0 for this so no WPF but will look into it some more and post my progress.

Thanks for the suggestions.

Link to comment
Share on other sites

  • 0

Well, sorry if I'm misunderstanding here but can't you just draw a gradient background on a window with an opacity set <100?

Like this:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace testapp
{
	static class Program
	{
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new Form1());
		}
	}

	public partial class Form1 : Form
	{
		public Form1()
		{
			this.SuspendLayout();

			this.Name = "Form1";
			this.Text = "Form1";
			this.ClientSize = new System.Drawing.Size(240, 200);
			this.FormBorderStyle = FormBorderStyle.None;
			this.Opacity = 0.92;

			GraphicsPath path = new GraphicsPath();

			int rad = 10;
			path.AddArc(0, 0, 2 * rad, 2 * rad, 180, 90);
			path.AddArc(this.Width - 2 * rad, 0, 2 * rad, 2 * rad, 270, 90);
			path.AddLine(this.Width, rad, this.Width, this.Height - rad);
			path.AddArc(this.Width - 2 * rad, this.Height - 2 * rad, 2 * rad, 2 * rad, 0, 90);
			path.AddLine(this.Width - rad, this.Height, rad, this.Height);
			path.AddArc(0, this.Height - 2 * rad, 2 * rad, 2 * rad, 90, 90);
			path.AddLine(0, this.Height - rad, 0, rad);

			Region = new Region(path);

			Label lb = new Label();
			lb.Name = "label1";
			lb.Location = new Point(10, 10);
			lb.AutoSize = true;

			lb.BackColor = Color.Transparent;
			lb.Font = new Font("Segoe UI", 12F, FontStyle.Bold, GraphicsUnit.Point, 0);
			lb.Text = "This is my window";

			this.Controls.Add(lb);

			this.ResumeLayout(false);
			this.PerformLayout();
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);

			LinearGradientBrush brush = new LinearGradientBrush(rect,
					Color.FromArgb(255, 180, 205, 230), Color.FromArgb(200, 215, 230, 235),
					LinearGradientMode.Vertical);

			e.Graphics.FillRectangle(brush, rect);
		}
	}
}

Link to comment
Share on other sites

  • 0

It's not specifically a gradient I'm after, it's full bitmap alpha-blend support, plus with the whole form less than 100% opacity its contents will be also etc. Thanks tho!

Link to comment
Share on other sites

  • 0

Indeed, I wondered if there were any .NET Framework 2.0 / 3.0 / 3.5 new features of Windows Forms that would enable it without a third-party DLL though.

Link to comment
Share on other sites

  • 0

Simple text. I've found a DLL I can use but it's be great if there were just a few user32.dll imports and calls I could use to do it without any third-party stuff.

Link to comment
Share on other sites

  • 0

Not entirely sure what you need then :p

If you don't want it in a DLL you can just copy the code to your program and you won't need any external libraries. If you're not liking it due to code required, there isn't much (that I know, and I'm not exactly good at .NET) you can do in terms of less code. The major downside to using the layered window way is you can't use the common controls and have to make your own up, which if you're just showing text isn't too hard.

Link to comment
Share on other sites

  • 0

:) What I have now does work - the purpose of the thread was to see if using a separate form would be the most efficient approach. Which, apparently, it seems to be.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.