sexta-feira, 9 de agosto de 2013

Desabilitar o Botão Fechar

Neste artigo vou mostrar como desabilitar o botão fechar do Form com C#




1º - Passo:
using System.Runtime.InteropServices;

2º - Passo:


Adicione o codigo abaixo no Load

FormCloseButtonDisabler.DisableCloseButton(this.Handle.ToInt32());
2º - Passo Crie esta Class:

public class FormCloseButtonDisabler
        {

            private const int MF_BYPOSITION = 0x400;

            private const int MF_REMOVE = 0x1000;

            private const int MF_DISABLED = 0x2;

            [DllImport("user32.dll", EntryPoint = "DrawMenuBar")]

            static extern Int32 DrawMenuBar(

            Int32 hWnd

            );

            [DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]

            static extern Int32 GetMenuItemCount(

            Int32 hMenu

            );

            [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]

            static extern Int32 GetSystemMenu(

            Int32 hWnd,

            bool bRevert

            );

            [DllImport("user32.dll", EntryPoint = "RemoveMenu")]

            static extern Int32 RemoveMenu(

            Int32 hMenu,

            Int32 nPosition,

            Int32 wFlags

            );

            public static void DisableCloseButton(int hWnd)
            {

                int hMenu;

                int menuItemCount;

                hMenu = GetSystemMenu(hWnd, false);

                menuItemCount = GetMenuItemCount(hMenu);

                // Retira o fechado do menu

                RemoveMenu(hMenu, menuItemCount - 1, MF_DISABLED | MF_BYPOSITION);

                // Retira o Separado

                RemoveMenu(hMenu, menuItemCount - 2, MF_DISABLED | MF_BYPOSITION);


                DrawMenuBar(hWnd);

            }

        }

0 comentários:

Postar um comentário