1 Mayıs 2016 Pazar

Console Metodları

Giriş
Console Sınıfı yazısına bakabilirsiniz.

Kernel32 İçindeki Metodlar
Bu metodlar AllocConsole, FreeConsole, GetConsoleWindow, ShowWindow

AllocConsole() ve FreeConsole()
C# içinden konsol açabilmek için aşağıdaki kod lazım.
internal static class NativeMethods
{
        
  [DllImport("kernel32.dll", SetLastError = true)]
  internal static extern int AllocConsole();
 
        
  [DllImport("kernel32.dll", SetLastError = true)]
  internal static extern int FreeConsole();
}

AllocConsole int yerine bool dönsün istersek şöyle yaparız.
using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e) 
{ 
    AllocConsole(); 
} 

[DllImport("kernel32.dll", SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool AllocConsole(); 
ShowConsole() ve HideConsole()
Konsolu yoksa yaratan ve gösteren veya gizleyen başka metodlar şöyle
internal static class ConsoleAllocator
{
    [DllImport(@"kernel32.dll", SetLastError = true)]
    static extern bool AllocConsole();

    [DllImport(@"kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

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

    const int SwHide = 0;
    const int SwShow = 5;


    public static void ShowConsoleWindow()
    {
        var handle = GetConsoleWindow();

        if (handle == IntPtr.Zero)
        {
            AllocConsole();
        }
        else
        {
            ShowWindow(handle, SwShow);
        }
    }

    public static void HideConsoleWindow()
    {
        var handle = GetConsoleWindow();

        ShowWindow(handle, SwHide);
    }
}



Hiç yorum yok:

Yorum Gönder