2009-10-10 96 views
17

打開,我發現在這個線程一些源代碼由雷克斯洛根張貼在這裏的SO:設置位置WinForms應用程序

link text

......還有還有一些非常有趣的代碼發佈在這個由Foredecker相同的線程,但它是不完整和複雜的:我沒有足夠的'追蹤設施知道如何完全實現它...

我能夠使用此控制檯代碼雷克斯(善意地)成功地發佈在WinForms應用程序中以記錄各種事件,並推送messa調試時使用的ges;我也可以從應用程序代碼中清除它。

我似乎無法做的事情是當我打開控制檯窗口(在主窗體加載事件中)時可靠地設置控制檯窗口的屏幕位置。我得到的編譯阻擋System.ArgumentOutOfRangeException錯誤,如果我嘗試設置WindowLeft或WindowTop性質是這樣的:

窗口的位置必須設置成 當前窗口大小的控制檯的緩衝區內適合 和 號一定不能是負面的。 參數名:左實際值是 #

我可以,但是,設置WINDOWWIDTH和WINDOWHEIGHT性能。

我試圖移動激活所述控制檯的不同位置包括代碼:

    Program.cs文件之前的MainForm被的InitializeComponent「
  1. 之前和呼叫後運行」在
  2. ()中在MainForm的構造函數
  3. 在窗體的窗體Load事件
  4. 的Event

的孔索le在代碼中的所有這些地方被激活了,但是在看起來隨機切換左上象限中的那個地方看起來沒有任何變化。

控制檯窗口打開的位置隨機變化(主窗體始終在屏幕上的相同位置初始化)。

回答

36

你可以嘗試這樣的事情。

此代碼將控制檯窗口的位置設置爲控制檯應用程序

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.InteropServices; 


namespace ConsoleApplication10 
{ 
    class Program 
    { 
    const int SWP_NOSIZE = 0x0001; 


    [DllImport("kernel32.dll", ExactSpelling = true)] 
    private static extern IntPtr GetConsoleWindow(); 

    private static IntPtr MyConsole = GetConsoleWindow(); 

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 

    static void Main(string[] args) 
    { 
     int xpos = 300; 
     int ypos = 300; 
     SetWindowPos(MyConsole, 0, xpos, ypos, 0, 0, SWP_NOSIZE); 
     Console.WriteLine("any text"); 
     Console.Read(); 
    } 
    } 
} 

此代碼設置在WinForm應用程序控制檯窗口的位置。

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 


namespace WindowsFormsApplication10 
{ 
    static class Program 
    { 

    const int SWP_NOSIZE = 0x0001; 

    [System.Runtime.InteropServices.DllImport("kernel32.dll")] 
    private static extern bool AllocConsole(); 

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern IntPtr GetConsoleWindow(); 

    [STAThread] 
    static void Main() 
    { 
     AllocConsole(); 
     IntPtr MyConsole = GetConsoleWindow(); 
     int xpos = 1024; 
     int ypos = 0; 
     SetWindowPos(MyConsole, 0, xpos, ypos, 0, 0, SWP_NOSIZE); 
     Console.WindowLeft=0; 
     Console.WriteLine("text in my console"); 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 
    } 
} 
+0

謝謝RRUZ;我能夠將您的代碼合併到Rex Logan發佈的基本代碼中,並將控制檯窗口設置爲我想要的地方。 我只是好奇:爲什麼要調用'Console.Read()?這是一個我正在運行的WinForms應用程序,並且我僅將控制檯用於「日誌記錄」:在WinForm應用程序中初始化控制檯時,這是一個標準的事情嗎? 非常感謝!最好,賬單 – BillW 2009-10-10 20:15:02

+0

@BillW - 它在那裏,讓窗口保持可見狀態,直到你點擊'return'。該示例在設置窗口位置後不做任何操作,因此只會關閉,您不會看到它具有正確的位置。 – ChrisF 2009-10-10 20:22:42

+0

嗨ChrisF, 感謝您的迴應! fyi:使用我在原始請求中鏈接的Rex Logan發佈的代碼,控制檯窗口確實存在,不需要任何對Console.Read()的調用。 best,Bill – BillW 2009-10-11 03:49:46

相關問題