4

我想知道是否可以删除使用 C# 关闭其他窗口的功能?

我知道您可以覆盖 Windows 的 close() 方法,但这也适用于其他进程吗?那么如何将另一个进程的窗口样式更改为固定___,使其无法调整大小?

到目前为止,我已经获得了应用程序的主窗口句柄,并且删除了所有按钮和菜单,但我仍然需要弄清楚如何使它无法关闭和无法调整大小。

这是我所拥有的:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ThirdTest
{
    class Program
    {
        #region Constants
        //Finds a window by class name
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //Sets a window to be a child window of another window
        [DllImport("USER32.DLL")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        //Sets window attributes
        [DllImport("USER32.DLL")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        //Gets window attributes
        [DllImport("USER32.DLL")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr GetMenu(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern int GetMenuItemCount(IntPtr hMenu);

        [DllImport("user32.dll")]
        static extern bool DrawMenuBar(IntPtr hWnd);

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

        //assorted constants needed
        public static uint MF_BYPOSITION = 0x400;
        public static uint MF_REMOVE = 0x1000;
        public static int GWL_STYLE = -16;
        public static int WS_CHILD = 0x40000000; //child window
        public static int WS_BORDER = 0x00800000; //window with border
        public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
        public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 
        public static int WS_SYSMENU = 0x00080000; //window menu
        #endregion

        public static void WindowsReStyle()
        {
            Process[] Procs = Process.GetProcesses();
            foreach (Process proc in Procs)
            {
                Console.WriteLine("Found process: " + proc.ProcessName.ToString());
                if (proc.ProcessName.StartsWith("notepad"))
                {
                    IntPtr pFoundWindow = proc.MainWindowHandle;
                    int style = GetWindowLong(pFoundWindow, GWL_STYLE);

                    //get menu
                    IntPtr HMENU = GetMenu(proc.MainWindowHandle);
                    //get item count
                    int count = GetMenuItemCount(HMENU);
                    //loop & remove
                    for (int i = 0; i < count; i++)
                        RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));

                    //force a redraw
                    DrawMenuBar(proc.MainWindowHandle);
                    SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_SYSMENU));
                    SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION));

                }
            }
        }  

        static void Main(string[] args)
        {
            WindowsReStyle();
        }
    }
}

有任何想法吗?(:

正如我在评论中所说,这里有一些关于这个问题的更多细节:

我需要两个应用程序在显示器上并排显示。它们都不能关闭或调整大小。一个是浏览器,另一个是名为“Z-tree”的应用程序。我已经解决了 Z-tree 的问题,因为它默认运行时没有关闭按钮,也没有调整大小,您可以在命令行中指定它的大小和位置。

4

2 回答 2

2

这是另一个想法,创建一个winforms项目并设置窗口使其无法调整大小。然后在表单中嵌入一个 WebBrowser 控件并在表单加载中导航到您的页面:

private void Form1_Load(object sender, EventArgs e)
{
     //catch form closing event to prevent form being closed using alt-f4
     FormClosing += Form1_FormClosing;

     //remove close button from toolbar and remove window border to prevent
     //moving and resizing
     this.ControlBox = false;
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

     //set position to half of the screen
     this.Left = Screen.PrimaryScreen.Bounds.Width / 2;
     this.Top = 0;
     this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
     this.Height = Screen.PrimaryScreen.Bounds.Height;

     //mark the window as a top level window, reducing users ability to alt-tab away
     TopMost = true;

     webBrowser1.Navigate("www.google.com");
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
     //prevent form being closed
     e.Cancel = true;
}

//the only way to close the form
void DoExit()
{    
     //remove the closing handler first or it won't close
     FormClosing -= Form1_FormClosing;
     Close();
}
于 2013-10-04T10:19:29.513 回答
0

如果您启动它,您可以强制 Internet Explorer 进入“无法退出”的全屏模式:

iexplore -k www.google.com

这就是商店和其他东西让它运行的方式,因此没有人可以关闭它。当然你可以通过任务管理器来关闭它,但这只是让大多数用户很难关闭它。

(CTRL-W 将关闭它 <--- 密钥!) http://support.microsoft.com/kb/154780

于 2013-10-03T17:03:13.107 回答