?
Solved

Form disable context menu items

Posted on 2007-11-28
1
Medium Priority
?
680 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
[X]
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
1 Comment
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

777 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