我是楼主,其实我是想通过这个问题解决我遇见的一个类似的问,我的程序运行,加载主窗体时,我做了个等待画面,等待画面结束后打开主窗体,但是主窗体取在桌面的最后面,windows资源管理器盖住了,试了好多方法都不行,最后用API函数搞定了 [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); public const int HWND_TOPMOST = -1; public const int SWP_SHOWWINDOW = 40; 这个API是把窗体放到windows桌面的最上面,虽然解决了,但是我不知道出现这种情况是为什么,必须调用API解决该问题吗?? 请大家帮忙分析一下,谢谢!!!!
最后在托盘点击事件里判断就好了
用Form.ContainsFocus来判断主窗体是否为活动窗体,
用Form.Activate()来激活主窗体,比如如下的伪代码:
void On_NotifyIcon_Click(object sender, EventArgv e)
{
if( !mainForm.ContainsFocus() )
{
mainForm.Activate();
}
else
{
mainForm.WindowState = FormWindowState.Minimized;
}
}
事件处理程序调用下面的代码。/// <summary>
/// 显示隐藏窗口
/// </summary>
/// <param name="showWindow"></param>
private void ShowHideWindow(bool showWindow)
{
if(showWindow == true)//显示
{
if(this.ShowInTaskbar==false)
{
this.ShowInTaskbar = true;
this.Visible = true;
//this.Show();
}
if(this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
}
else//隐藏
{
if(this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
if(this.ShowInTaskbar == true)
{
this.ShowInTaskbar = false;
this.Visible = false;
//this.Hide();
}
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
if (this.TopMost) this.WindowState = FormWindowState.Minimized;
else this.TopMost = true;
}
else
{
this.WindowState = FormWindowState.Normal;
this.TopMost = true;
}
}
private void 关于我们ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("关于我们");
} private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
} private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
显示隐藏主界面ToolStripMenuItem.Text = "显示主界面";
}
else
{
显示隐藏主界面ToolStripMenuItem.Text = "隐藏主界面";
}
} private void 显示隐藏主界面ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
else
{
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
} private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
//if (this.Visible)
//{
// this.Visible = false;
//}
//else
//{
// this.Visible = true;
//}
MessageBox.Show("设置");
} private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("帮助");
}
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
public const int HWND_TOPMOST = -1;
public const int SWP_SHOWWINDOW = 40;
这个API是把窗体放到windows桌面的最上面,虽然解决了,但是我不知道出现这种情况是为什么,必须调用API解决该问题吗??
请大家帮忙分析一下,谢谢!!!!
1,设置窗体显示在最前
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
2,获得显示在最前的窗体
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
将取得的窗口句柄和当前的句柄比较一下就知道是不是当前窗体在最前了
this.Visible = true;你把窗体或者托盘隐藏就行了。在加些判断条件