Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Form disable context menu items

Posted on 2007-11-28
1
662 Views
Last Modified: 2013-12-16
How do I disable specific items (i.e. Close) in the context menu of the Windows form? Or if that's tricky to do, how do I disable the context menu?

Thanks
0
Comment
Question by:gromul
1 Comment
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20368679
Yes, it is pretty tricky. You will need some Interop. First define a helper class:

   public class WinApi
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("user32.dll")]
        public static extern bool RemoveMenu(IntPtr hMenu, UInt32 uPosition, UInt32 uFlags);

        public const Int32 WM_SYSCOMMAND = 0x0112;
        public const Int32 MF_SEPARATOR = 0x0800;
        public const Int32 MF_BYPOSITION = 0x0400;
        public const Int32 MF_BYCOMMAND = 0x0000;
        public const Int32 MF_POPUP = 0x0010;

        public const Int32 SC_CLOSE = 0xF060;
        public const Int32 SC_MOVE = 0xF010;
        public const Int32 SC_MINIMIZE = 0xF020;
 }

then, inside your form's constructor:

            // Remove most system menu items
            IntPtr hMenu = WinApi.GetSystemMenu(this.Handle, false);
            if (hMenu != IntPtr.Zero)
            {
                    WinApi.RemoveMenu(hMenu, WinApi.SC_CLOSE, WinApi.MF_BYCOMMAND);
             }

you can remove other menu options as define in WinApi class
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question