使用窗口句柄使用c#禁用鼠标单击

时间:2010-05-21 01:14:16

标签: c#

我需要为Kiosk应用程序禁用特定窗口的鼠标单击,鼠标移动。它在C#中是否可行?

我删除了特定窗口的菜单栏和标题栏,这是否是实现上述要求的起点?我怎样才能达到这个要求。

使用窗口句柄删除菜单栏和标题栏的代码:

 #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)); 
            } 
        }
    }

3 个答案:

答案 0 :(得分:2)

听起来你正在寻找EnableWindow。描述是:

  

启用或禁用鼠标和键盘   输入到指定的窗口或   控制。当输入被禁用时,   窗口不接收诸如的输入   鼠标点击和按键。什么时候   输入已启用,窗口接收   所有输入。

所以你要添加

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

EnableWindow(pFoundWindow, false); 

这相当于在Windows窗体表单/控件上设置Enabled属性。

答案 1 :(得分:2)

您可以尝试覆盖WndProc并检查那里的WM_MOUSE *事件。如果您没有为这些处理过的事件调用基本WndProc,它应该可以工作。 这里要考虑的一点是,既然你的是一个自助服务终端应用程序,你的特殊鼠标处理会导致触摸屏出现任何问题。

答案 2 :(得分:1)

要防止在其他进程的窗口中输入键盘,您需要make a keyboard hook 然后,您可以检查GetForegroundWindow()并禁止输入。