using System;
using System.Runtime.InteropServices;
namespaceformAnimation
{
////// Win32 implementation to show / hide a window with animation.///publicclassWinAPI
{
////// Animates the window from left to right. This flag can be used with roll or slide animation.///publicconstint AW_HOR_POSITIVE = 0X1;
////// Animates the window from right to left. This flag can be used with roll or slide animation.///publicconstint AW_HOR_NEGATIVE = 0X2;
////// Animates the window from top to bottom. This flag can be used with roll or slide animation.///publicconstint AW_VER_POSITIVE = 0X4;
////// Animates the window from bottom to top. This flag can be used with roll or slide animation.///publicconstint AW_VER_NEGATIVE = 0X8;
////// Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used.///publicconstint AW_CENTER = 0X10;
////// Hides the window. By default, the window is shown.///publicconstint AW_HIDE = 0X10000;
////// Activates the window.///publicconstint AW_ACTIVATE = 0X20000;
////// Uses slide animation. By default, roll animation is used.///publicconstint AW_SLIDE = 0X40000;
////// Uses a fade effect. This flag can be used only if hwnd is a top-level window.///publicconstint AW_BLEND = 0X80000;
////// Animates a window.///
[DllImport("user32.dll", CharSet=CharSet.Auto)]
publicstaticexternintAnimateWindow (IntPtr hwand , int dwTime , int dwFlags) ;
}
}
thanks
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Bahar Jalali165 reputationPosted Nov 10, 2009, 2:23 AM EST
0
there is an error ! i get this error too before that in work with threads but i don't use delegate ... u use delegate i don't know why this error happened!
Code
Cross-thread operation not valid: Control 'Form2' accessed from a thread other than the thread it was created on.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 10, 2009, 2:48 AM EST
0
Hi friend,
My code works for me without any issue.
We use delegates to avoid the mentioned cross thread issue. Don;t remove the delegate. Without directly disposing the form you should do it via a delegate. Use my code. It works.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 10, 2009, 12:46 AM EST
Try this code. Please test it and let me know if you find any issue. I could not test this code.
private void MyThreadFunction()
{
WinAPI.AnimateWindow(this.Handle, animationTime, flags);
this.ThreadFinished();
}
private void btnClose_Click(object sender, System.EventArgs e)
{
flags -= WinAPI.AW_ACTIVATE;
flags += WinAPI.AW_HIDE;
Thread mythread = new Thread(new ThreadStart(this.MyThreadFunction));
mythread.Start();
}
public delegate void disposeform();
public void ThreadFinished()
{
if (this.InvokeRequired)
{
this.Invoke(new disposeform(ThreadFinished), new object[] { });
return;
}
this.Dispose();
}
Do not forget this also on the top of the class.
using System.Threading;
If this helps, please accept my answer.
Bahar Jalali165 reputationPosted Nov 10, 2009, 2:23 AM EST
Bahar Jalali165 reputationPosted Nov 10, 2009, 2:42 AM EST
thanks my good friend
success
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 10, 2009, 2:48 AM EST
My code works for me without any issue.
We use delegates to avoid the mentioned cross thread issue. Don;t remove the delegate. Without directly disposing the form you should do it via a delegate. Use my code. It works.