2014-01-16 41 views
0

我已經在標題中看到了幾個解決方案,但它們都沒有做我想做的。將控制檯應用程序移動到用戶選擇的背景中

我有ch = Console.ReadKey().KeyChar;,如果用戶輸入'y'我希望應用程序進入後臺。如果用戶輸入'n',則程序繼續,如同沒有任何事情發生。這可能嗎?我做了很多研究,但仍找不到適合我的人。

這是我到現在爲止:

char ch = '0'; 
Console.WriteLine("Enter Log File Destenation:"); 
string url = Console.ReadLine(); 
Console.WriteLine("Run In BackGround ? (Defaul Set to False)"; 
ch = Console.ReadKey().KeyChar; 
if (ch == 'y') 
{ 
    //Move To Background 
} 

    // continue with program 

這是一個比特幣率記錄。它會從網站標題中獲取比特率並將其記錄到一個txt文件中,該文件的目的地由用戶在程序開始時設置。 目標設置後,它會詢問它是否應該在後臺運行。 無論哪種方式,程序進入一段時間(真)循環的日誌。

+0

我認爲你不能做它... –

+0

你最好的選擇可能是要把你發佈的代碼信息調用在後臺運行的應用程序的啓動應用程序。 – itsme86

回答

0

使用SetWindowPos法帕拉姆HWND_BOTTOM

using System.Runtime.InteropServices; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     [DllImport("user32.dll", SetLastError=true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); 

     [DllImport("user32.dll")] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     public static extern bool GetWindowRect(IntPtr hWnd, out W32RECT lpRect); 

     [StructLayout(LayoutKind.Sequential)] 
     public struct W32RECT 
     { 
      public int Left; 
      public int Top; 
      public int Right; 
      public int Bottom; 
     } 

     public const uint HWND_BOTTOM = 1; 

     static void Main(string[] args) 
     { 
      IntPtr handle = Process.GetCurrentProcess().MainWindowHandle; 
      W32RECT rect; 
      GetWindowRect(handle , out rect); //to get position and size of your console 
      SetWindowPos(handle, IntPtr.Zero, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, HWND_BOTTOM);//to set background position of your console with the same size and screen's position 
     } 
    } 
} 

未經測試,但它會工作

來源:http://www.developpez.net/forums/d199635/environnements-developpement/delphi/setforegroundwindow-setbackgroundwindow/(如果你需要我可以翻譯)

+0

你能更具體嗎?我不明白這種方法 – user3107990

+0

我編輯我的帖子與代碼 – OhMyGeo

相關問題