Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move
event with no luck.
I'm stuck, any help?
views:
788answers:
3Don't think JQuery works on the desktop just yet...
Gurdas Nijor
2009-10-18 07:19:53
the question didn't mention windows form, just form, I thought its web form
Moksha
2009-10-18 07:39:06
Sorry, should've been more clear about that.
Nate Shoffner
2009-10-18 07:50:00
Not really, I think it's very clear. What asp.net form has opacity, can be moved, and raises a `Form_Move` event?
Kobi
2009-10-18 08:19:35
+1
A:
To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:
[STAThread]
static void Main()
{
using (Form form = new Form())
using (Timer tmr = new Timer())
{
tmr.Interval = 500;
bool first = true;
tmr.Tick += delegate
{
tmr.Stop();
form.Opacity = 1;
};
form.Move += delegate
{
if (first) { first = false; return; }
tmr.Stop();
tmr.Start();
form.Opacity = 0.3;
};
Application.Run(form);
}
}
Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.
Marc Gravell
2009-10-18 07:37:59
I get an error saying "SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application."
Nate Shoffner
2009-10-18 07:43:21
Ok, I put Application.SetCompatibleTextRenderingDefault(false); before everything else and it runs. But no transparency when the form is moved.
Nate Shoffner
2009-10-18 07:54:46
+3
A:
The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.
The ResizeEnd event fires after a form has finished moving, so something like this should work:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.5;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
Bauer
2009-10-18 08:01:53
Nice one! I didn't know that ResizeEnd fired when the user finished moving a form. I notice that ResizeBegin fires when the user starts moving a form, so maybe he could use this rather than the Form_Load / canMove / Form_Move trick?
itowlson
2009-10-18 08:18:11
Thank you so much! This is perfect! I'll experiment with it even more. By the way, love your avatar Bauer ;).
Nate Shoffner
2009-10-18 08:19:49