hi: I find out a way to resolve that.
We need to use P/Invoke to invoke WinApi functions, the function is in
coredll.dll files. The function name is SetForegroundWindow(IntPtr Hwnd);
Where Hwnd is the window handle of the form. I catch that HWnd at the
form's load method by calling GetForegroundWindow().
The code looks like this, I simplify the code, but should work. Try it.
Wayne
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Itron.MRFSFD.Gui
{
#region SummaryFormHeader
//************************************************************
// Type Name: SummaryForm
/// <summary>
/// Summary description for SummaryForm.
/// </summary>
///
/// <remarks></remarks>
///
/// <authorName>Peter Dunn</authorName>
/// <creationDate>May 4, 2004</creationDate>
//************************************************************
#endregion SummaryFormHeader
public class SummaryForm : Form
{
#region Variables
private const int SW_SHOW = 5;
private IntPtr m_Hwnd;
public IntPtr Hwnd
{
get
{
return m_Hwnd;
}
}
#endregion Variables
#region CONSTRUCTORS
#region SummaryForm Method
public SummaryForm()
{
InitComponent();
}
#endregion SummaryForm Method
#endregion CONSTRUCTORS
#region Dispose Method
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#endregion Dispose Method
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitComponent()
{
this.Load += new System.EventHandler(this.SummaryForm_Load);
}
private void SummaryForm_Load(object sender, System.EventArgs e)
{
m_Hwnd = GetForegroundWindow();
}
#endregion Windows Form Designer generated code
#region Public Methods
public void ShowAsTopWindow()
{
Show();
SetForegroundWindow(Hwnd);
Refresh();
}
#endregion Public Methods
#region DllImport
[DllImport("coredll.dll")]
private static extern
IntPtr GetForegroundWindow();
static extern bool SetForegroundWindow(IntPtr hWnd);
#endregion DllImport
}
}
Lloyd Dupont said:
Poor you.
I've given up on that and basically avoid such behavior by avoiding anything
which could lead to such behavior.
However this is a very common, generally non-deterministic bug, so if you
have a clear deterministic sample where such bug arise, I do advice you to
file a bug report here:
http://lab.msdn.microsoft.com/productfeedback/
(MS is very responsive, you'll see)
Choose VisualC#, type your bug description and follows the wizard. In your
bug description give an hyperlink to where download your sample.
and let us all knows!