窗体顶置两种方法:
方法1:
/// <summary>
/// 设置当前活动的窗口
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
/// <summary>
/// 得到当前活动的窗口
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern System.IntPtr GetForegroundWindow();
private void MainFrm_Load(object sender, EventArgs e)
{
SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2);
}
方法2:
private void testBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
SetWindowPos(GetForegroundWindow(), -1, 0, 0, 0, 0, 1 | 2);
}
解决任务栏图片显示有时失效方法:
private void testBtn_Click(object sender, EventArgs e)
{
this.Close();
Form2 frm = new Form2();
frmShowInTaskbar = false;
frm.Show();
frm.ShowInTaskbar = true;
}