-1

I want my Form frmMainMenu to disable stuffs. the first thing I did is set it to maximized and remove the minimize & maximize on control. When I drag the form it automatically minimized, also when I double click the controlBox it also minimize. I also want the form to prevent movement. and lastly i want to disable the alt + tab on my form.

I have some code here for disabling movement.

protected override void WndProc(ref Message message)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;
        switch (message.Msg)
        {
            case WM_SYSCOMMAND:
                int command = message.WParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                    return;
                break;
        }

        base.WndProc(ref message);
    }

here is for double click

 private const int WM_NCLBUTTONDBLCLK = 0x00A3; 

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_NCLBUTTONDBLCLK)
        {
            m.Result = IntPtr.Zero;
            return;
        }
        base.WndProc(ref m);
    }

i dont have a code for drag and alt + tab and i cant put those two in my form because the have the same parameter WndProc.

  • Disable what 'stuffs' ? Be specific. – Arghya C Sep 06 '15 at 15:28
  • is that not specific? disable drag, doubleclick, alt +tab and prevent movement? – anjohnnette Sep 06 '15 at 15:55
  • No very clear at least. Check http://stackoverflow.com/questions/704564/disable-drag-and-drop-on-html-elements – Arghya C Sep 06 '15 at 16:02
  • 1
    Very user-hostile, surely there's another way to accomplish whatever you are trying to achieve. If this is some kind of kiosk app then there are much better ways to lock down the machine. – Hans Passant Sep 06 '15 at 16:03
  • im using c#. new to programing, dont know anything about kiosk app. I just want the form to be ontop of the windows that only one button can close it. – anjohnnette Sep 06 '15 at 16:08
  • In that case you might want to use the Form.ShowDialog method to show the form. https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx This will limit the scope of the action to your own application. Capturing and denying access to standard windows actions is against almost every design guidelines, unless you're trying to build your own ATM user-interface or a UI for a cash register (these are generally called Kiosk apps). – jessehouwing Sep 06 '15 at 16:16
  • my form is an mdi dont need to open another form im opening form inside toolstripcontainer. – anjohnnette Sep 06 '15 at 16:36

1 Answers1

0

Do the following steps:

  1. Create a Form
  2. Set FormBorderStyle to FixedSingle
  3. Set MinimizeBox to false
  4. Set MaximizeBox to false
  5. Set TopMost to true
  6. Handle Load, FormClosing and DeActivate events of your form.
  7. Write this code in your form class:

Code:

public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // Structure contain information about low-level keyboard input event 
    [StructLayout(LayoutKind.Sequential)]
    private struct KBDLLHOOKSTRUCT
    {
        public Keys key;
        public int scanCode;
        public int flags;
        public int time;
        public IntPtr extra;
    }
    //System level functions to be used for hook and unhook keyboard input  
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool UnhookWindowsHookEx(IntPtr hook);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string name);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern short GetAsyncKeyState(Keys key);
    //Declaring Global objects     
    private IntPtr ptrHook;
    private LowLevelKeyboardProc objKeyboardProcess;

    private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
    {
        if (nCode >= 0)
        {
            KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

            // Disabling Windows keys 

            if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)
            {
                return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
            }
        }
        return CallNextHookEx(ptrHook, nCode, wp, lp);
    }

    bool HasAltModifier(int flags)
    {
        return (flags & 0x20) == 0x20;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
        objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
        ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }

    protected override void WndProc(ref Message message)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;

        switch (message.Msg)
        {
            case WM_SYSCOMMAND:
                int command = message.WParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                    return;
                break;
        }

        base.WndProc(ref message);
    }
    private void Form1_Deactivate(object sender, EventArgs e)
    {
        this.Activate();
    }
}

Notes:

  • The code for disable Win and Alt + Tab has taken from this post, Thanks to the author.

  • The code for preventing move, has taken from this post, Thanks to the author.

  • Don't Forgt to add usings:

Usings:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • alt + tab not working `marshal` does not exist,`process` does not exist, `DllImport` coul not be found etc also i cant add `WndProc` because i got 2 of the same type how should i join this three together? – anjohnnette Sep 06 '15 at 16:32
  • @anjohnnette Glad to hear that it worked. It's funny that when I was testing to post changes that you requested in previous comment, the form stayed at top in maximized state and I couldn't get rid of it and at last I restarted my laptop using power button! :)) – Reza Aghaei Sep 06 '15 at 16:58
  • haha sorry :) i remove `e.cancel = true` so that i can use the x button on `controlbox` – anjohnnette Sep 07 '15 at 03:15