在用控制台面板写入一些后台程序,在用于项目中时,会需要隐藏控制面板的操作。毕竟放一个控制台窗口在前台不仅容易被用户操作误关闭,对用户来说,这个窗口也不友好。这时,就需要将窗口进行隐藏。
1.引用 user32.dll 扩展文件
(1)直接从网上下载
(2)从C盘windows\system32目录下copy出来,放到待运行的Debug/Release 目录下
介于user32.dll 为非托管DLL,需要用到 DllImportAttribute 属性 对从非托管 DLL 导出的函数进行调用。
DllImportAttribute 属性(需要引用命名空间 System.Runtime.InteropServices)
2.功能模块如下
class Program
{
[DllImport("User32.dll", EntryPoint = "ShowWindow")] //
private static extern bool ShowWindow(IntPtr hWnd, int type);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
static void Main(string[] args)
{
GetHide();
Console.ReadKey();
}
/// <summary>
/// 隐藏控制台窗口
/// </summary>
private static void GetHide()
{
Console.Title = "TestStartHide"; //当前窗口的标题
IntPtr ParenthWnd = new IntPtr(0);
IntPtr et = new IntPtr(0);
ParenthWnd = FindWindow(null, "TestStartHide"); //根据Title 找到对应窗口的句柄,进行操作
ShowWindow(ParenthWnd, 0);//隐藏本dos窗体, 0: 后台执行;1:正常启动;2:最小化到任务栏;3:最大化
}
}
2019-04-19 留下此篇随笔,仅供参考。