Solved

Form disable context menu items

Posted on 2007-11-28
1
653 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now