SORU
23 Ocak 2009, Cuma


Windows uygulamasında Konsol Haritayı?

Bir yol gösterecek bir Windows uygulamasında bir konsol var mı?

Böyle bir şey yapmak istiyorum:

static class Program
{
    [STAThread]
    static void Main(string[] args) {
        bool consoleMode = Boolean.Parse(args[0]);

        if (consoleMode) {
            Console.WriteLine("consolemode started");
            // ...
        } else {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

CEVAP
23 Ocak 2009, Cuma


Ne yapmak istediğinizi mantıklı bir şekilde mümkün değildir. Benzer bir soru so look at the answers vardı.

Sonra da insane approach (- backup available here. aşağı site) Jeffrey Knight tarafından yazılmış

Nasıl da GUI çalışabilen bir uygulama oluşturabilirim soru: (windows) modu veya komut satırı / konsol modu?

Bu yüzeyinde, bu kolay bir iş gibi görünür: bir Konsol oluşturun uygulama, bir windows formu ekleyin ve kaçıyorsun. Ancak, bir sorun var:

Sorun: Eğer GUI modunda çalıştırırsanız, bir pencere ve bir de sonunda arka planda gizlenen konsol için herhangi bir yolu yok mu sinir bozucu gizlemek.

İnsanlar bunu istiyor gibi çalışan gerçek amfibi bir uygulamadır sorunsuz her iki modda.

Eğer aşağı kırılırsa, aslında dört kullanım örnekleri var:

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

Bu, ama bir uyarı ile kodu gönderiyorum.

Aslında ben yaklaşım bu tür bir daha seni düşünmek değerinden yolda sorun. Örneğin, gerek iki farklı kullanıcı arabirimleri GUI' ve komut / shell. Garip bir mantık merkezi inşa etmek zorunda kalacaksın komut satırı vs GUI şu özetleri motor ve sadece gidiyor çok acayip şeyler. Eğer ben olsaydım, geri adım ve nasıl düşünmek istiyorum mod-anahtarlama bu tür olup olmadığını pratikte kullanılacak bu işe değer. Böylece, bu sana özel bir durum olmadıkça bunun için aradı, ben içeriye girdim, çünkü yakında bu kodu kendim kullanmak olmaz API çağrıları bir şey yapmam gereken durumlar, eğilimindedir ve kendi kendime "bir şeyler overcomplicating mıyım?". durma sor

=Çıkış tipi bir Windows Uygulaması

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
    {
        /*
    DEMO CODE ONLY: In general, this approach calls for re-thinking 
    your architecture!
    There are 4 possible ways this can run:
    1) User starts application from existing cmd window, and runs in GUI mode
    2) User double clicks to start application, and runs in GUI mode
    3) User starts applicaiton from existing cmd window, and runs in command mode
    4) User double clicks to start application, and runs in command mode.

    To run in console mode, start a cmd shell and enter:
        c:\path\to\Debug\dir\WindowsApplication.exe console
        To run in gui mode,  EITHER just double click the exe, OR start it from the cmd prompt with:
        c:\path\to\Debug\dir\WindowsApplication.exe (or pass the "gui" argument).
        To start in command mode from a double click, change the default below to "console".
    In practice, I'm not even sure how the console vs gui mode distinction would be made from a
    double click...
        string mode = args.Length > 0 ? args[0] : "console"; //default to console
    */

        [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();
            }
        }
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BuzzFeedVideo

    BuzzFeedVide

    10 AĞUSTOS 2011
  • Fullscreen

    Fullscreen

    23 Mart 2006
  • wowchick16

    wowchick16

    17 Mart 2007