[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="ShowWindow")] public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow) ;public const int SW_SHOW = 5;public const int SW_HIDE = 0;显示窗体 ShowWindow(handler,SW_SHOW );隐藏窗体 ShowWindow(handler,SW_HIDE );handler是窗体句柄
隐藏窗口 this.Hide();this.showInTasb=false;就行了
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace textCC { public partial class Form7 : Form { public Form7() { InitializeComponent(); } [DllImport("user32.dll")] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); public const int SW_HIDE = 0; public const int SW_SHOWNORMAL = 1; public const int SW_NORMAL = 1; public const int SW_SHOWMINIMIZED = 2; public const int SW_SHOWMAXIMIZED = 3; public const int SW_MAXIMIZE = 3; public const int SW_SHOWNOACTIVATE = 4; public const int SW_SHOW = 5; public const int SW_MINIMIZE = 6; public const int SW_SHOWMINNOACTIVE = 7; public const int SW_SHOWNA = 8; public const int SW_RESTORE = 9; public const int SW_SHOWDEFAULT = 10; public const int SW_FORCEMINIMIZE = 11; public const int SW_MAX = 11; private void button1_Click(object sender, EventArgs e) { IntPtr HWnd = FindWindow(null, "计算器"); MessageBox.Show("隐藏窗体"); ShowWindow(HWnd, SW_HIDE); MessageBox.Show("显示窗体"); ShowWindow(HWnd, SW_SHOW); } } }
public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow) ;public const int SW_SHOW = 5;public const int SW_HIDE = 0;显示窗体
ShowWindow(handler,SW_SHOW );隐藏窗体
ShowWindow(handler,SW_HIDE );handler是窗体句柄
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace textCC
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
} [DllImport("user32.dll")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); public const int SW_HIDE = 0;
public const int SW_SHOWNORMAL = 1;
public const int SW_NORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_MAXIMIZE = 3;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_SHOW = 5;
public const int SW_MINIMIZE = 6;
public const int SW_SHOWMINNOACTIVE = 7;
public const int SW_SHOWNA = 8;
public const int SW_RESTORE = 9;
public const int SW_SHOWDEFAULT = 10;
public const int SW_FORCEMINIMIZE = 11;
public const int SW_MAX = 11; private void button1_Click(object sender, EventArgs e)
{
IntPtr HWnd = FindWindow(null, "计算器"); MessageBox.Show("隐藏窗体");
ShowWindow(HWnd, SW_HIDE);
MessageBox.Show("显示窗体");
ShowWindow(HWnd, SW_SHOW);
}
}
}
用ShowWindow(HWnd, SW_SHOW)的话,如果是最小化到托盘并且窗体隐藏就无法显示了
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr FindWindow(string strclassName, string strWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool OpenIcon(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
void FindAndOpenWindow(string Title)
{
IntPtr hWnd = (IntPtr ) FindWindow(null, Title);
if (hWnd != IntPtr.Zero)
{
bool isIcon = IsIconic(hWnd);
if ( !isIcon )
{
SetForegroundWindow(hWnd);
}
else
{
OpenIcon(hWnd);
}
}
}
IsIconic 函数功能:该函数确定给定窗口是否是最小化(图标化)的窗口。
函数原型:BOOL IsIconic(HWND hWnd);
参数:
hWnd:被测试窗口的句柄。
返回值:如果窗口已图标化,返回值为非零;如果窗口未图标化,返回值为零。
速查:Windows NT:3.1以上版本;Windows:95以上版本;Windows CE:不支持;头文件:winuser.h;
库文件:user32.Iib。
OpenIcon VB声明 Declare Function OpenIcon Lib "user32" Alias "OpenIcon" (ByVal hwnd As Long) As Long
说明:恢复一个最小化的程序,并将其激活 返回值 Long,非零表示成功,零表示失败。会设置GetLastError 参数表
参数
类型及说明:hwnd Long,欲恢复的窗口句柄
针对vb窗体,应使用vb的WindowState属性
试试:
ShowWindow(HWnd, SW_SHOWNORMAL)
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Activate();
} else if (this.WindowState == FormWindowState.Maximized || this.WindowState == FormWindowState.Normal) {
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}最小化的时候并没有吧窗口隐藏,而是把任务栏给隐藏了
这样要恢复的话应该也要调用下面的那段的api吧this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Activate();大家斟酌下