栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Windows应用程序中显示控制台?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Windows应用程序中显示控制台?

您想要做的事不可能是理智的。还有一个类似的问题,请看一下答案。

然后,还有一种由杰弗里·奈特(Jeffrey Knight)编写的疯狂方法(可在此处下载站点-
进行备份):

问题:如何创建可以在GUI(Windows)模式或命令行/控制台模式下运行的应用程序?

从表面上看,这似乎很容易:创建一个Console应用程序,向其中添加Windows窗体,然后启动并运行。但是,有一个问题:

问题:如果以GUI模式运行,最终将在后台隐藏一个窗口和一个讨厌的控制台,而您将无法隐藏它。

人们似乎想要的是一个真正的两栖应用程序,它可以在任何一种模式下都能平稳运行。

如果将其分解,实际上这里有四个用例:

User starts application from existing cmd window, and runs in GUI modeUser double clicks to start application, and runs in GUI modeUser starts application from existing cmd window, and runs in command

mode
User double clicks to start application, and runs in command mode.

我正在发布代码来执行此操作,但需要注意。

实际上,我认为这种方法会使您遇到很多麻烦,而不是值得的。例如,您将必须具有两个不同的UI-一个用于GUI,另一个用于命令/
shell。您将必须构建一些奇怪的中央逻辑引擎,该引擎从GUI与命令行进行抽象,这只会变得很奇怪。如果是我,我会退后一步,思考一下如何在实践中使用它,以及是否值得进行这种模式切换。因此,除非有特殊情况需要,否则我不会自己使用该代码,因为一旦遇到需要API调用才能完成某件事的情况,我就会停下来问自己“我是否使事情过于复杂了?
”。

输出类型= Windows应用程序

using System;using System.Collections.Generic;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Diagnostics;using Microsoft.Win32;namespace WindowsApplication{    static class Program    {        

        [Dllimport("kernel32.dll", SetLastError = true)]        static extern bool AllocConsole();        [Dllimport("kernel32.dll", SetLastError = true)]        static extern bool FreeConsole();        [Dllimport("kernel32", SetLastError = true)]        static extern bool AttachConsole(int dwProcessId);        [Dllimport("user32.dll")]        static extern IntPtr GetForegroundWindow();        [Dllimport("user32.dll", SetLastError = true)]        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int

lpdwProcessId);

        [STAThread]        static void Main(string[] args)        { //TODO: better handling of command args, (handle help

(–help /?) etc.)
string mode = args.Length > 0 ? args[0] : “gui”; //default
to gui

 if (mode == "gui") {     MessageBox.Show("Welcome to GUI mode");     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);     Application.Run(new Form1()); } else if (mode == "console") {     //Get a pointer to the forground window.  The idea here

is that
//IF the user is starting our application from an
existing console
//shell, that shell will be the uppermost window. We’ll
get it
//and attach to it
IntPtr ptr = GetForegroundWindow();

     int  u;     GetWindowThreadProcessId(ptr, out u);     Process process = Process.GetProcessById(u);     if (process.ProcessName == "cmd" )    //Is the uppermost

window a cmd process?
{
AttachConsole(process.Id);

         //we have a console to attach to ..         Console.WriteLine("hello. It looks like you started

me from an existing console.”);
}
else
{
//no console AND we’re in console mode … create a
new console.

         AllocConsole();         Console.WriteLine(@"hello. It looks like you double

clicked me to start
AND you want console mode. Here’s a new console.”);
Console.WriteLine(“press any key to continue …”);
Console.ReadLine();
}

     FreeConsole(); }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402907.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号