1

I need to disable the Mouse Clicks, Mouse movement for a specific windows for a Kiosk application. Is it Feasible in C# ?

I have removed the menu bar and title bar of a specific window, will that be a starting point to achieve the above requirement ? How can i achieve this requirement.

The code for removing the menu bar and title bar using window handle :

 #region Constants
    //Finds a window by class name
    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    //Sets a window to be a child window of another window
    [DllImport("USER32.DLL")]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    //Sets window attributes
    [DllImport("USER32.DLL")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    //Gets window attributes
    [DllImport("USER32.DLL")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll")]
    static extern IntPtr GetMenu(IntPtr hWnd);

    [DllImport("user32.dll")]
    static extern int GetMenuItemCount(IntPtr hMenu);

    [DllImport("user32.dll")]
    static extern bool DrawMenuBar(IntPtr hWnd);

    [DllImport("user32.dll")]
    static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);

    //assorted constants needed
    public static uint MF_BYPOSITION = 0x400;
    public static uint MF_REMOVE = 0x1000;
    public static int GWL_STYLE = -16;
    public static int WS_CHILD = 0x40000000; //child window
    public static int WS_BORDER = 0x00800000; //window with border
    public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
    public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 
    public static int WS_SYSMENU = 0x00080000; //window menu  
    #endregion

    public static void WindowsReStyle()
    { 
        Process[] Procs = Process.GetProcesses();
        foreach (Process proc in Procs)
        {
            if (proc.ProcessName.StartsWith("notepad"))
            {
                IntPtr pFoundWindow = proc.MainWindowHandle;
                int style = GetWindowLong(pFoundWindow, GWL_STYLE);

                //get menu
                IntPtr HMENU = GetMenu(proc.MainWindowHandle);
                //get item count
                int count = GetMenuItemCount(HMENU);
                //loop & remove
                for (int i = 0; i < count; i++)
                    RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));

                //force a redraw
                DrawMenuBar(proc.MainWindowHandle);
                SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_SYSMENU)); 
                SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION)); 
            } 
        }
    }
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • 5
    Unplug the mouse and keyboard from the kiosk? – Jay May 21 '10 at 01:33
  • want to do this programatically – Anuya May 21 '10 at 02:11
  • Are you writing the application ?...if so: What OS version ? What .net Version ? What client technology, winforms ? WPF ? – Rusty May 21 '10 at 02:27
  • @ Rusty, It is WinXp, Dotnet Visual studio c# 2008, Framework 3.5, Winforms. – Anuya May 21 '10 at 02:51
  • @srk, And you are writing the Kiosk application or trying to prevent input to an existing application ? – Rusty May 21 '10 at 03:05
  • @Rusty, My application "A" will be getting the handle for another application "B" and remove the titlebar, menubar using the above code. Here i want to disable the Mouse and keyboards inputs for that application "B". In the above sample i do it for notepad. Objective is, i dont want the user to key in any thing or click in that application "B" which has only one form. – Anuya May 21 '10 at 03:12
  • @Rusty, I tried BlockInput Function, it disables the mouse and keyboard for whole system. i want to do it for specific FORM – Anuya May 21 '10 at 03:14
  • @srk Got it :)...You are on the right track. SLaks just posted an answer that should get you there...so I won't post :) Good luck. – Rusty May 21 '10 at 03:43

3 Answers3

4

It sounds like you are looking for EnableWindow. The description is:

Enables or disables mouse and keyboard input to the specified window or control. When input is disabled, the window does not receive input such as mouse clicks and key presses. When input is enabled, the window receives all input.

So you would add

[DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);

and

EnableWindow(pFoundWindow, false); 

This is equivalent to setting the Enabled property on a Windows Forms Form/Control.

shf301
  • 31,086
  • 2
  • 52
  • 86
2

You can try to override the WndProc and check for WM_MOUSE* events there. If you dont call the base WndProc for these handled events, it should work. A point to consider here is that since yours is a kiosk application, will your special mouse handling lead to any problems for Touch screen.

Sudesh Sawant
  • 147
  • 1
  • 4
1

To prevent keyboard input in a window in another process, you need to make a keyboard hook.
You can then check GetForegroundWindow() and suppress the input.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • can be elaborate ? Now i set the forgroundwindow to my application. How to suppress the input in the window now ? – Anuya May 21 '10 at 04:14