2014-07-02 1 views
-1

csharp n'est pas ma langue maternelle mais j'essaie de comprendre comment modifier mon code csharp afin que la fenêtre de la console n'apparaisse pas lorsque j'exécute l'exécutable.Comment masquer la fenêtre de la console?

Le code j'utilise est:

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.IO; 
using System.Threading; 

namespace Foreground { 
    class GetForegroundWindowTest { 

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)] 
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

    public static void Main(string[] args){ 
     while (true){ 
      IntPtr fg = GetForegroundWindow(); //use fg for some purpose 

      var bufferSize = 1000; 
      var sb = new StringBuilder(bufferSize); 

      GetWindowText(fg, sb, bufferSize); 

      using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
      { 
       sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString()); 
      } 

      Thread.Sleep(5000); 
     } 
    } 
    } 
} 

Ma tentative avortée est:

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.IO; 
using System.Threading; 

namespace Foreground { 
    class GetForegroundWindowTest { 

    /// Foreground dll's 
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)] 
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

    /// Console hide dll's 
    [DllImport("kernel32.dll")] 
    static extern IntPtr GetConsoleWindow(); 

    [DllImport("user32.dll")] 
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

    const int SW_HIDE = 0; 

    public static void Main(string[] args){ 
     while (true){ 
      IntPtr fg = GetForegroundWindow(); //use fg for some purpose 

      var bufferSize = 1000; 
      var sb = new StringBuilder(bufferSize); 

      GetWindowText(fg, sb, bufferSize); 

      using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
      { 
       sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString()); 
      } 

      var handle = GetConsoleWindow(); 
      ShowWindow(handle, SW_HIDE); 

      Thread.Sleep(5000); 
     } 
    } 
    } 
} 

Toute aide serait grandement apprécié.

+0

Peut-être que vous essayez de construire autre chose qu'une application console? Quelque chose comme un service Windows ou une application de la barre d'état système? – David

+1

Pourquoi cette question est downvoted? Op a fait une idée d'être nouveau dans la langue et fournir un code qui échoue. – Tigran

Répondre

1

Choisissez Windows Application au lieu de Console Application sur les propriétés de votre projet.

+0

@Robert S'il vous plaît juste télécharger Visual Studio Express. – tnw

+0

Auriez-vous la gentillesse d'expliquer comment vous faites ce Visual Studio Express 2013? – Phoenix

Questions connexes