Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi In Windows 7 when you press ctrl-alt-del it takes you to a different screen showing you options to display task manager, logoff etc.
how can I code something that is simlar to when you press ctrl-alt-del so that the window looks like that ?
Posted
Updated 16-Feb-11 3:06am
v3
Comments
Member 7654832 16-Feb-11 9:33am    
when you do ctrl-alt-del what sort of window is it ? that's what i want to know it's probably not another windows form window it's probably a different type but i want a similar microsoft window.
Richard MacCutchan 16-Feb-11 10:26am    
In general you cannot do this as Ctrl-Alt-Del is handled by the Windows kernel and not passed up the line to user code. However, I think there is something called GINA (http://msdn.microsoft.com/en-us/library/aa380543(VS.85).aspx) which may allow you to modify the UI that is shown.

You cannot do anything like that on Ctrl+Alt+Del.

This key event will not be routed to your application, so you cannot intercept it. You can use anything close, but never exactly this. This interesting exclusion is done to prevent any software to break Ctrl+Alt+Del Windows Security functionality.

Probably you need a kiosk more.
See this: http://helpdeskgeek.com/how-to/how-to-setup-windows-vista-and-7-as-a-kiosk/[^].

You can also Google http://en.lmgtfy.com/?q=%22Windows+7%22+kiosk+mode[^].

—SA
 
Share this answer
 
Comments
Laurence1234 16-Feb-11 11:45am    
Ah yeah, the kiosk link is what I was thinking in terms of the only application for that (see my comment to Answer 1). -5

Laurence
Sergey Alexandrovich Kryukov 16-Feb-11 11:47am    
Thank you.
Would you recommend this as a final answer to OP?
--SA
Laurence1234 16-Feb-11 11:52am    
Yes, I think so. It seems most relevant. Is there a special way to do this?

If not, OP I recommend this answer.

Laurence
Sergey Alexandrovich Kryukov 16-Feb-11 14:07pm    
No special way; I don't think so.
--SA
Nish Nishant 16-Feb-11 11:59am    
Voted 5, I think this is the response that best answers the OP's question.
Are you saying you want a window to look like the Windows Ctrl-Alt-Del screen? If so, I can only assume that you're trying to trick someone into thinking ther're doing that they're not actually doing, and that's called hacking, and we don't condone or advocate that practice. Correct me if I'm wrong.
 
Share this answer
 
Comments
Member 7654832 16-Feb-11 9:31am    
no i don't want it to look like the ctrl-alt-del i want my own screen that is the same type of window as ctrl-alt-del and i'm not using it for hacking.
Sergey Alexandrovich Kryukov 16-Feb-11 11:43am    
John, I think you know perfectly: it is not possible to intercept Ctrl+Alt+Del event.
OP probably needs kiosk mode. Please see my answer.
--SA
Hi Member 7654832,

Here is a partial solution for you: It should surpress Alt+F4, Windows-Key etc., But it doesn't surpress Ctrl+Alt+Del :doh: But maybe it points you in the right direction... Btw. "overriding" the Ctrl+Alt+Key hotkey wasn't possible (or I dont know how), so I was wrong on that.

I couldn't find out (quickly) how my framework provider surpressed all keyboard input. But I did the following: (The hook class is taken from my post on this article A Simple C# Global Low Level Keyboard Hook[^])

C#
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace IWillNotGoAway
{
    static class Program
    {
        static void Main()
        {
            Form form = new Form();
            form.ControlBox = false;
            form.TopMost = true;
            form.FormBorderStyle = FormBorderStyle.None;
            form.Size = Screen.PrimaryScreen.Bounds.Size;
            form.Click += new EventHandler(delegate(object sender, EventArgs e)
            {
                form.Close();
            });

            GlobalKeyboardHook hook = new GlobalKeyboardHook();
            hook.KeyDown += new KeyEventHandler(hook_KeyDown);
            hook.Hook();

            Application.Run(form);
        }

        static void hook_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            e.Handled = true;
        }
    }

    #region CLASS GlobalKeyboardHook

    /// <summary>
    /// Helper class for global (system-wide) keyboard hooks.
    /// </summary>
    public class GlobalKeyboardHook
    {
        #region TYPES

        #region CLASS KeyboardHookStruct

        /// <summary>
        /// Marshalling of the Windows-API KBDLLHOOKSTRUCT structure.#
        /// Contains information about a low-level keyboard input event.
        /// This is named "struct" to be consistent with the Windows API name,
        /// but it must be a class since it is passed as a pointer in SetWindowsHookEx.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        class KeyboardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        #endregion // CLASS KeyboardHookStruct

        #region DELEGATE HookProc

        /// <summary>
        /// Represents the method called when a hook catches a monitored event.
        protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);

        #endregion // DELEGATE HookProc

        #endregion // TYPES

        #region CONSTANTS

        const int WH_KEYBOARD_LL = 13;
        const int WH_KEYBOARD = 2;

        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_SYSKEYUP = 0x105;

        const byte VK_SHIFT = 0x10;
        const byte VK_CAPITAL = 0x14;
        const byte VK_NUMLOCK = 0x90;

        const byte VK_LSHIFT = 0xA0;
        const byte VK_RSHIFT = 0xA1;
        const byte VK_LCONTROL = 0xA2;
        const byte VK_RCONTROL = 0x3;
        const byte VK_LALT = 0xA4;
        const byte VK_RALT = 0xA5;

        // const byte LLKHF_ALTDOWN = 0x20; // not used

        #endregion // CONSTANTS

        #region VARIABLES

        /// <summary>
        /// Value indicating if hook is active.
        /// </summary>
        bool m_bHookActive;

        /// <summary>
        /// Stored hook handle returned by SetWindowsHookEx
        /// </summary>
        int m_iHandleToHook;

        /// <summary>
        /// Stored reference to the HookProc delegate (to prevent delegate from beeing collected by GC!)
        /// </summary>
        protected HookProc m_hookproc;

        #endregion // VARIABLES

        #region EVENTS

        /// <summary>
        /// Occurs when a key is pressed.
        /// </summary>
        public event KeyEventHandler KeyDown;
        /// <summary>
        /// Occurs when a key is released.
        /// </summary>
        public event KeyEventHandler KeyUp;
        /// <summary>
        /// Occurs when a character key is pressed.
        /// </summary>
        public event KeyPressEventHandler KeyPress;

        #endregion // EVENTS

        #region CONSTRUCTION & DESTRUCTION

        /// <summary>
        /// Dtor.
        /// </summary>
        ~GlobalKeyboardHook()
        {
            Unhook();
        }

        #endregion // CONSTRUCTION & DESTRUCTION

        #region PROPERTIES

        /// <summary>
        /// Gets a value indicating if hook is active.
        /// </summary>
        public bool HookActive
        {
            get { return m_bHookActive; }
        }

        #endregion // PROPERTIES

        #region METHODS

        /// <summary>
        /// Install the global hook.
        /// </summary>
        /// <returns> True if hook was successful, otherwise false. </returns>
        public bool Hook()
        {
            if(!m_bHookActive)
            {
                m_hookproc = new HookProc(HookCallbackProcedure);

                IntPtr hInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
                m_iHandleToHook = SetWindowsHookEx(
                    WH_KEYBOARD_LL,
                    m_hookproc,
                    hInstance,
                    0);

                if(m_iHandleToHook != 0)
                {
                    m_bHookActive = true;
                }
            }
            return m_bHookActive;
        }

        /// <summary>
        /// Uninstall the global hook.
        /// </summary>
        public void Unhook()
        {
            if(m_bHookActive)
            {
                UnhookWindowsHookEx(m_iHandleToHook);
                m_bHookActive = false;
            }
        }

        /// <summary>
        /// Raises the KeyDown event.
        /// </summary>
        /// <param name="kea"> KeyEventArgs </param>
        protected virtual void OnKeyDown(KeyEventArgs kea)
        {
            if(KeyDown != null)
                KeyDown(this, kea);
        }

        /// <summary>
        /// Raises the KeyUp event.
        /// </summary>
        /// <param name="kea"> KeyEventArgs </param>
        protected virtual void OnKeyUp(KeyEventArgs kea)
        {
            if(KeyUp != null)
                KeyUp(this, kea);
        }

        /// <summary>
        /// Raises the KeyPress event.
        /// </summary>
        /// <param name="kea"> KeyEventArgs </param>
        protected virtual void OnKeyPress(KeyPressEventArgs kpea)
        {
            if(KeyPress != null)
                KeyPress(this, kpea);
        }

        #endregion // METHODS

        #region EVENTHANDLER

        /// <summary>
        /// Called when hook is active and a key was pressed.
        /// </summary>
        int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            bool bHandled = false;

            if(nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
            {
                // Get keyboard data
                KeyboardHookStruct khs = (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                // Get key states
                bool bControl = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) || ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
                bool bShift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) || ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
                bool bAlt = ((GetKeyState(VK_LALT) & 0x80) != 0) || ((GetKeyState(VK_RALT) & 0x80) != 0);
                bool bCapslock = (GetKeyState(VK_CAPITAL) != 0);

                // Create KeyEventArgs
                KeyEventArgs kea = new KeyEventArgs((Keys) (khs.vkCode |
                        (bControl ? (int) Keys.Control : 0) |
                        (bShift ? (int) Keys.Shift : 0) |
                        (bAlt ? (int) Keys.Alt : 0)));

                // Raise KeyDown/KeyUp events
                if(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
                {
                    OnKeyDown(kea);
                    bHandled = kea.Handled;
                }
                else if(wParam == WM_KEYUP || wParam == WM_SYSKEYUP)
                {
                    OnKeyUp(kea);
                    bHandled = kea.Handled;
                }

                // Raise KeyPress event
                if(wParam == WM_KEYDOWN && !bHandled && !kea.SuppressKeyPress)
                {
                    byte[] abyKeyState = new byte[256];
                    byte[] abyInBuffer = new byte[2];
                    GetKeyboardState(abyKeyState);

                    if(ToAscii(khs.vkCode, khs.scanCode, abyKeyState, abyInBuffer, khs.flags) == 1)
                    {
                        char chKey = (char) abyInBuffer[0];
                        if((bCapslock ^ bShift) && Char.IsLetter(chKey))
                            chKey = Char.ToUpper(chKey);
                        KeyPressEventArgs kpea = new KeyPressEventArgs(chKey);
                        OnKeyPress(kpea);
                        bHandled = kea.Handled;
                    }
                }
            }

            if(bHandled)
                return 1;
            else
                return CallNextHookEx(m_iHandleToHook, nCode, wParam, lParam);
        }

        #endregion // EVENTHANDLER

        #region EXTERN

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        static extern int UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern int GetKeyboardState(byte[] pbKeyState);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern short GetKeyState(int vKey);

        [DllImport("user32.dll")]
        static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);

        #endregion // EXTERN
    }

    #endregion // CLASS GlobalKeyboardHook
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 11:46am    
My 5.
You're not wrong: it is not possible to intercept Ctrl+Alt+Del event on any level (I would think). OP probably needs kiosk mode. Please see my answer.
--SA
The type of window that you want is called a Modal window and you get this behaviour by using the ShowDialog() method instead of the normal Show() method.

C#
MyForm newForm = new MyForm();
newForm.ShowDialog();


This makes it Modal to your application although users would still be able to Alt-Tab to other Applications.

I have never needed to do what you want to do so I have never researched how to make it Modal systemwide. I will say, however, that if any application did this to me it would be removed from my computer the very next second.

It is one thing for the OS to do this sort of thing but an intrusion for an application to do so. Please rethink your scheme.
 
Share this answer
 
Comments
Laurence1234 16-Feb-11 10:17am    
The only application I can think of where you might want to do this would be on an embedded system such as WindowsPosReady, where you do not want the operator to be able to use any program other then the embedded application. However, as you point out, if an application created a system wide model on my personal computer I would remove it immediately. I think it is possible to disable low level keyboard hooks in C# such as alt-tab, but I wouldn't have any use for this and I don't think it's easy to do.
Member 7654832 16-Feb-11 10:50am    
so there isn't a way to create an application that runs like LogonUI.exe or consent.exe?
Henry Minute 16-Feb-11 11:37am    
I don't know.
As I said I have never researched it.
Laurence1234 16-Feb-11 10:52am    
I'm afraid that I don't know, nor would I like to comment as the only applications for that I can think of may be malicious.
Sergey Alexandrovich Kryukov 16-Feb-11 11:42am    
Henry, this is nice, but it is not possible to intercept Ctrl+Alt+Del event on any level (try it!). OP probably needs kiosk mode. See my answer, please.
--SA
Hotkey is what you are looking for.

Register a global (system wide) hotkey for you application
Show your Window when the hotkey is triggered.

If you need some code (e.g. for doing the marshalling) a quick search should give you something like this. Simple steps to enable Hotkey and ShortcutInput user control[^]
 
Share this answer
 
Comments
Member 7654832 16-Feb-11 9:30am    
thanks, i don't want to add an extra hotkey i just want my app to behave exactly how ctrl-alt-del does i want the exact same screen/window whatever you want to call it, and then have my program run in that window and i don't want the user to be able to do anything else.
johannesnestler 16-Feb-11 9:37am    
hmm - I'm a little confused.

You say: i just want my app to behave exactly how ctrl-alt-del does
I understand: If you are in any application (any app has focus) you want to "listen" for ctrl-alt-del
Solution: Hotkey

P.S. On the system I'm working right now, if I press ctrl-alt-del - it just opens the taskmanager... (XP Prof)
Member 7654832 16-Feb-11 9:41am    
I want to program an application in c# that launches a window which takes up the full screen and bloacks the user from doing anything like when you press ctrl-alt-del in Windows 7 and it takes you to that new window which takes up the fullscreen and asks you if you want to lock this screen, Switch User, Log Off, Change a password...,Start Task Manager. I don't want those options i just want to know how I can create a windows form that behaves like the window you see when you press ctrl-alt-del in Windows 7 i.e. it blocks the user from running any of their programs, it limits what the user can do etc.
johannesnestler 16-Feb-11 10:54am    
Ah ok - this was a big missunderstanding.
But the only solution that comes to my mind is a global keyboard hook to supress all hotkeys and a form with no control box and Top most window style (ControlBox=false, TopMost=false). I often have this problem when developing application for machines and plants where the user can't access the underlaying windows platform. But my framework handles this for me - if I find the time I have a look how they did that.
Sergey Alexandrovich Kryukov 16-Feb-11 11:44am    
I don't think hot key could possible intercept Ctrl+Alt+Del event (try it!) -- this is a special case in OS. OP probably needs kiosk mode. See my answer, please.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900