2010-05-21 36 views
1

我需要爲Kiosk應用程序的特定窗口禁用鼠標點擊,鼠標移動和鍵盤輸入。使用.NET可行嗎?使用窗口句柄禁用鼠標點擊和鍵盤輸入使用P/Invoke

我已經刪除了特定窗口的菜單欄和標題欄,這是否會成爲達到上述要求的起點?

去除菜單欄和使用窗口句柄標題欄的代碼:

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

用戶如何在沒有鍵盤或鼠標的情況下與應用程序進行交互? – 2010-05-21 05:49:01

+0

@ Carlos Loth,用戶不需要互動。此應用程序僅用於查看。雖然它與鼠標和打字有關,但我需要禁用該窗口的鼠標和鍵盤。 – Anuya 2010-05-21 05:57:00

+0

如果可以刪除外部應用程序的標題欄和菜單欄,我想即使停用鼠標也是可以的。是啊? – Anuya 2010-05-21 05:57:49

回答

1

使用EnableWindow API調用做到這一點。我很多年沒有使用過這個,但我不認爲這有任何交叉過程問題。