有没有办法用C#删除其他进程的关闭按钮?

时间:2013-05-30 01:23:22

标签: c# .net user-interface

我用C ++尝试过,似乎需要将代码注入另一个进程来禁用其主窗口的关闭按钮。但我很好奇,因为我实际上需要它用于.NET程序,我可以用C#将其主窗口的关闭按钮变灰吗?

PS。我正在做的过程再次不是我的过程。我可以这样找到:

Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;

    //Now how to disable it's close button?
}

1 个答案:

答案 0 :(得分:5)

我想我明白了:

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

[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);


const uint SC_CLOSE = 0xF060;
const uint MF_BYCOMMAND = 0x00000000;

Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;

    IntPtr nSysMenu = GetSystemMenu(pFoundWindow, false);
    if (nSysMenu != IntPtr.Zero)
    {
        if (DeleteMenu(nSysMenu, SC_CLOSE, MF_BYCOMMAND))
        {
            //Done!
        }
    }
}