How to remove the MenuBar of an application using windows API?

后端 未结 1 1841
孤独总比滥情好
孤独总比滥情好 2020-12-31 18:07

I am using the below code to remove the Title Bar of an application, which is working perfectly for notepad. Now i want to remove the Menu Bar also. How to achieve it ?

1条回答
  •  隐瞒了意图╮
    2020-12-31 19:02

    You can't hide them, you will need to remove them using the API instead, YMMV on the result of doing this;

        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [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);
    
        public static uint MF_BYPOSITION = 0x400;
        public static uint MF_REMOVE = 0x1000;
    
        public static void WindowsReStyle() {
            Process[] Procs = Process.GetProcesses();
            foreach (Process proc in Procs) {
    
                if (proc.ProcessName.StartsWith("notepad")) {
                    //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);
                }
            }
        }
    

    0 讨论(0)
提交回复
热议问题