Solved

Form disable context menu items

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

760 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

21 Experts available now in Live!

Get 1:1 Help Now