PoshCode Archive  Artifact [85f8d9aaff]

Artifact 85f8d9aaff7385e75aa331593910007abc120373d35fd4c18c1c88b9e29a4b04:

  • File Lock-close-button.ps1 — part of check-in [813b01b55a] at 2018-06-10 13:35:38 on branch trunk — It’s useful if you offen use Alt+Space to manipulate host data (copy\paste). (user: greg zakharov size: 1558)

# encoding: ascii
# api: csharp
# title: Lock close button
# description: It’s useful if you offen use Alt+Space to manipulate host data (copy\paste).
# version: 0.1
# type: class
# author: greg zakharov
# license: CC0
# x-poshcode-id: 4059
# x-archived: 2017-05-30T19:03:01
# x-published: 2013-03-31T15:30:00
#
#
$code = @'
using System;
using System.Runtime.InteropServices;

namespace CloseButtonToggle {
  internal static class WinAPI {
    [DllImport("kernel32.dll")]
    internal static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeleteMenu(IntPtr hMenu,
                           uint uPosition, uint uFlags);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DrawMenuBar(IntPtr hWnd);

    [DllImport("user32.dll")]
    internal static extern IntPtr GetSystemMenu(IntPtr hWnd,
               [MarshalAs(UnmanagedType.Bool)]bool bRevert);

    const uint SC_CLOSE     = 0xf060;
    const uint MF_BYCOMMAND = 0;

    internal static void ChangeCurrentState(bool state) {
      IntPtr hMenu = GetSystemMenu(GetConsoleWindow(), state);
      DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
      DrawMenuBar(GetConsoleWindow());
    }
  }

  public static class Status {
    public static void Disable() {
      WinAPI.ChangeCurrentState(false); //its 'true' if need to enable
    }
  }
}
'@

Add-Type $code
[CloseButtonToggle.Status]::Disable()