I was able to do what I wanted by using Kiosk Mode
, as I had quoted in #. In Kiosk mode you disable the input of keys that can close your application, so the only way to keep 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 properly, their application closes and releases the computer for use, otherwise it will remain on the login screen until the user logs in. I put a cancel button, because if the user can not log in the application will be running forever. More will go according to your approach and what you really want to do.