Click here to Skip to main content
15,898,035 members
Home / Discussions / C#
   

C#

 
AnswerRe: BackColor issue with main form and the controls Pin
buachaill cliste7-May-09 9:17
buachaill cliste7-May-09 9:17 
GeneralRe: BackColor issue with main form and the controls Pin
Blubbo7-May-09 9:26
Blubbo7-May-09 9:26 
GeneralRe: BackColor issue with main form and the controls Pin
Henry Minute7-May-09 10:22
Henry Minute7-May-09 10:22 
AnswerRe: BackColor issue with main form and the controls Pin
Jabbar_espania8-May-09 1:51
Jabbar_espania8-May-09 1:51 
Question[Message Deleted] Pin
Ankit Aneja7-May-09 7:41
Ankit Aneja7-May-09 7:41 
AnswerRe: problem with datatype Pin
Dave Kreskowiak7-May-09 10:01
mveDave Kreskowiak7-May-09 10:01 
QuestionFramework 3.5 AccountManagement Pin
Jacob Dixon7-May-09 7:15
Jacob Dixon7-May-09 7:15 
QuestionHook on right button mouse Pin
teycir7-May-09 6:52
teycir7-May-09 6:52 
Hello;
I'm building an application in winforms c# 3.0. I want to disable the mouses's right button click when the application is running on all the system (in order to prevent copy/paste).
I succeeded in blocking the keyboard keys but not the mouse button. Can you figure out why the code for the mouse does not work?
The problem comes from LowLevelMouseProc()
Thanks for help.

#region
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace GlobalHook
{
    public class UserActivityHook
    {
        private const int VmRButton = 0x02;
        private const int VkLControl = 0xA2;
        private const int VkRControl = 0xA3;
        private const int VkControl = 0x11;
        private const int VkSnapshot = 0x2C;
        private const int WhKeyboardLl = 13;
        private const int WhMouseLl = 14;
        private const int WmKeydown = 0x0100;
        private const int WmMousedown =0x0201;
        private static IntPtr kbh_Handle;
        private static IntPtr kbh_Handle1;
 
        private static HookProc KeyboardHookProcedure;
        private static HookProc MouseHookProcedure;     
        private int HMouseHook;        
        private int HKeyboardHook;
 
        #region Windows Function Imports
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
            SetLastError = true)]
        private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
 
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
            SetLastError = true)]
        public static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);
 
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
            SetLastError = true)]
        private static extern int UnhookWindowsHookEx(int idHook);
 
 
        private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
 
        #endregion
 
        #region Windows constants
 
        #endregion
 
        ~UserActivityHook()
        {
            //uninstall hooks and do not throw exceptions
            Stop(true, true, false);
           
        }
 
        public void Start()
        {
 
            if (HMouseHook == 0)
            {
                // Create an instance of HookProc.
                MouseHookProcedure = new HookProc(LowLevelMouseProc);
                //install hook
                HMouseHook = SetWindowsHookEx(WhMouseLl, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                //If SetWindowsHookEx fails.
                if (HMouseHook== 0)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                    int errorCode = Marshal.GetLastWin32Error();
                    //do cleanup
                    Stop(true, false, false);
                    //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                    throw new Win32Exception(errorCode);
                }
            }
 
 
 
            // install Keyboard hook only if it is not installed and must be installed
            if (HKeyboardHook == 0)
            {
                // Create an instance of HookProc.
                KeyboardHookProcedure = new HookProc(LowLevelKeyboardProc);
                //install hook
                HKeyboardHook = SetWindowsHookEx(WhKeyboardLl, KeyboardHookProcedure,
                                                 Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                                                 0);
                //If SetWindowsHookEx fails.
                if (HKeyboardHook == 0)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                    int errorCode = Marshal.GetLastWin32Error();
                    //do cleanup
                    Stop(false, true, false);
                    //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                    throw new Win32Exception(errorCode);
                }
            }
        }
 
        public void Stop()
        {
            Stop(true, true, true);
        }
 
 
        public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook, bool throwExceptions)
        {
            if (HMouseHook != 0 && uninstallMouseHook)
            {
                //uninstall hook
                int retMouse = UnhookWindowsHookEx(HMouseHook);
                //reset invalid handle
                HMouseHook = 0;
                //if failed and exception must be thrown
                if (retMouse == 0 && throwExceptions)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                    int errorCode = Marshal.GetLastWin32Error();
                    //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                    throw new Win32Exception(errorCode);
                }
            }
 
            //if keyboard hook set and must be uninstalled
            if (HKeyboardHook != 0 && uninstallKeyboardHook)
            {
                //uninstall hook
                int retKeyboard = UnhookWindowsHookEx(HKeyboardHook);
                //reset invalid handle
                HKeyboardHook = 0;
                //if failed and exception must be thrown
                if (retKeyboard == 0 && throwExceptions)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                    int errorCode = Marshal.GetLastWin32Error();
                    //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                    throw new Win32Exception(errorCode);
                }
            }
        }
 
 
     
 
        private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
                return 0;
            }
 
            if (wParam == WmKeydown)
            {
                IntPtr kbdll = lParam;
                Kbdllhookstruct kbdllstruct = (Kbdllhookstruct) Marshal.PtrToStructure(kbdll, typeof (Kbdllhookstruct));
 
                if ((kbdllstruct.VkCode == VkSnapshot) || (kbdllstruct.VkCode == VkControl) || (kbdllstruct.VkCode == VkLControl) || (kbdllstruct.VkCode == VkRControl))
                    return -1;
            }
 
            return CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
        }
 
 
 
 
        private static int LowLevelMouseProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
                return 0;
            }
 
            if (wParam == WmMousedown)
            {
                IntPtr msdll = lParam;
                MouseLlHookStruct msdllstruct = (MouseLlHookStruct)Marshal.PtrToStructure(msdll, typeof(MouseLlHookStruct));
 
                if (msdllstruct.MouseData == VmRButton)
                    return -1;
            }
 
            return CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
        }
 
 
        [StructLayout(LayoutKind.Sequential)]
        private struct Point
        {
            public int X;
            public int Y;
        }
 
 
 
        [StructLayout(LayoutKind.Sequential)]
        private struct MouseLlHookStruct
        {
            public Point Point;
            public int MouseData;
            public int Flags;
            public int Time;
            public int ExtraInfo;
        }
    
 
        #region Nested type: Kbdllhookstruct
 
        [StructLayout(LayoutKind.Sequential)]
        public struct Kbdllhookstruct
        {
            public int VkCode;
            public int ScanCode;
            public int Flags;
            public int Time;
            public int ExtraInfo;
        }
 
        #endregion
    }
}

AnswerRe: Hook on right button mouse Pin
Dave Kreskowiak7-May-09 7:24
mveDave Kreskowiak7-May-09 7:24 
AnswerRe: Hook on right button mouse Pin
harold aptroot7-May-09 7:42
harold aptroot7-May-09 7:42 
AnswerRe: Hook on right button mouse Pin
Dan Neely7-May-09 9:31
Dan Neely7-May-09 9:31 
AnswerRe: Hook on right button mouse Pin
Member 10339077-May-09 12:57
Member 10339077-May-09 12:57 
GeneralRe: Hook on right button mouse Pin
teycir8-May-09 4:52
teycir8-May-09 4:52 
GeneralRe: Hook on right button mouse Pin
Member 10339078-May-09 4:58
Member 10339078-May-09 4:58 
GeneralRe: Hook on right button mouse Pin
teycir8-May-09 5:07
teycir8-May-09 5:07 
GeneralRe: Hook on right button mouse Pin
Member 10339078-May-09 5:18
Member 10339078-May-09 5:18 
GeneralRe: Hook on right button mouse Pin
teycir8-May-09 5:23
teycir8-May-09 5:23 
GeneralRe: Hook on right button mouse Pin
Member 10339078-May-09 7:39
Member 10339078-May-09 7:39 
AnswerRe: Hook on right button mouse Pin
teycir9-May-09 0:17
teycir9-May-09 0:17 
QuestionLane Detection Algorithm Pin
sebogawa7-May-09 6:08
sebogawa7-May-09 6:08 
AnswerRe: Lane Detection Algorithm Pin
Luc Pattyn7-May-09 6:21
sitebuilderLuc Pattyn7-May-09 6:21 
GeneralRe: Lane Detection Algorithm Pin
sebogawa7-May-09 6:26
sebogawa7-May-09 6:26 
GeneralRe: Lane Detection Algorithm Pin
Luc Pattyn7-May-09 6:48
sitebuilderLuc Pattyn7-May-09 6:48 
QuestionReturn int* to C# from C++ DLL? Pin
Alan Balkany7-May-09 5:35
Alan Balkany7-May-09 5:35 
AnswerRe: Return int* to C# from C++ DLL? Pin
Luc Pattyn7-May-09 6:16
sitebuilderLuc Pattyn7-May-09 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.