Using Window Handle to disable Mouse clicks and Keyboard Inputs using P/Invoke

Posted by srk on Stack Overflow See other posts from Stack Overflow or by srk
Published on 2010-05-21T05:44:53Z Indexed on 2010/05/22 12:00 UTC
Read the original article Hit count: 305

Filed under:
|
|

I need to disable the Mouse Clicks, Mouse movement and Keyboard Inputs for a specific windows for a Kiosk application. Is it feasible using .NET ?

I have removed the menu bar and title bar of a specific window, will that be a starting point to achieve the above 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)); 
        } 
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET