How to disable a window?

Asked

Viewed 96 times

-2

I am looking for some code that disables a window, and after the user enters a login and password in my form the window comes back to operate.

Being more specific and with a little detail, I’ve seen a program do such action, it worked by the PID process and blocked it. When the user tried to move the blocked application he could not, and windows gave that sound when we tried to click something that "can’t". I need something similar in my project.

I have tried to search on Kiosk Mode but was not successful.

  • Your question is not clear enough, it will probably be closed. Give more details with examples.

  • @Wesley-birth Do you want to disable a window in your application? or disable any other window that is open?

  • For example, the user is accessing Chrome, when my application runs I want it to block the Chrome window. There is no way to be more explanatory than this. I was looking over and saw that some Rat’s work with this ;-;

  • If people who probably have more knowledge than you are asking, it is because it is not clear.

1 answer

4


I was able to do what you want using the Kiosk Mode, as he had quoted in your other question. In Kiosk mode you disable the input of keys that can close your application, so the only way to maintain the behavior you want would be to maximize the form and put it as Topmost, below the code I did:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace KioskMode
{
    public partial class frmLogin : Form
    {
#region Imports da library user32.dll

        [DllImport("user32.dll")]
        private static extern int FindWindow(string cls, string wndwText);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int cmd);

        [DllImport("user32.dll")]
        private static extern long SHAppBarMessage(long dword, int cmd);

        [DllImport("user32.dll")]
        private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        private static extern int UnregisterHotKey(IntPtr hwnd, int id);

#endregion


#region Constantes referentes à teclas

        private const int USE_ALT = 1;
        private const int USE_CTRL = 2;
        private const int USE_SHIFT = 4;
        private const int USE_WIN = 8;
        // Hot key ID tracker
        short mHotKeyId = 0;

#endregion


        public frmLogin()
        {
            InitializeComponent();

            // Maximiza a tela
            this.WindowState = FormWindowState.Maximized;
            // Retira a borda do formulario para evitar que o usuario arraste o form.
            this.FormBorderStyle = FormBorderStyle.None;
            // Oculta o formulario da barra de tarefas
            this.ShowInTaskbar = false;
            // Indica que o formulario ficara sempre a frente
            this.TopMost = true;

            // Desabilita teclas de atalho de saída da aplicação
            RegisterGlobalHotKey(Keys.F4, USE_ALT);
            RegisterGlobalHotKey(Keys.W, USE_CTRL);
            RegisterGlobalHotKey(Keys.N, USE_CTRL);
            RegisterGlobalHotKey(Keys.S, USE_CTRL);
            RegisterGlobalHotKey(Keys.A, USE_CTRL);
            RegisterGlobalHotKey(Keys.C, USE_CTRL);
            RegisterGlobalHotKey(Keys.X, USE_CTRL);
            RegisterGlobalHotKey(Keys.V, USE_CTRL);
            RegisterGlobalHotKey(Keys.B, USE_CTRL);
            RegisterGlobalHotKey(Keys.F, USE_CTRL);
            RegisterGlobalHotKey(Keys.H, USE_CTRL);

            // oculta a barra de tarefas
            ShowWindow(FindWindow("Shell_TrayWnd", null), 0);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtLogin.Text == "Usuario" &&
                txtSenha.Text == "Senha")
                Close();
            else
                MessageBox.Show("Login inválido");
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void RegisterGlobalHotKey(Keys hotkey, int modifiers)
        {
            try
            {
                // increment the hot key value - we are just identifying
                // them with a sequential number since we have multiples
                mHotKeyId++;

                if(mHotKeyId > 0)
                {
                    // register the hot key combination
                    if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0)
                    {
                        // tell the user which combination failed to register -
                        // this is useful to you, not an end user; the end user
                        // should never see this application run
                        MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " +
                            Marshal.GetLastWin32Error().ToString(),
                            "Hot Key Registration");
                    }
                }
            }
            catch 
            {
                // clean up if hotkey registration failed -
                // nothing works if it fails
                UnregisterGlobalHotKey();
            }
        }

        private void UnregisterGlobalHotKey()
        {
            // loop through each hotkey id and
            // disable it
            for (int i = 0; i < mHotKeyId; i++)
            {
                UnregisterHotKey(this.Handle, i);
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            const int WM_HOTKEY = 0x312;
            if (m.Msg == WM_HOTKEY)
            {
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // unregister the hot keys
            UnregisterGlobalHotKey();

            // show the taskbar - does not matter really
            ShowWindow(FindWindow("Shell_TrayWnd", null), 1);

        }
    }
}

If the user logs in correctly, their application closes and frees the computer for use, otherwise it is on the login screen until the user logs in. I put a cancel button, because if the user can not login the application will run forever. More this goes according to your approach and what you really want to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.