0


I am developing a windows based application in which i want that whenever my application get started it should disable mouse click events outside the my application window form.

Can anyone please tell me, how can i achieve that?

Thanks in advance.

Edit :
Catching the mouse click event within the form and suppressing the click action is easy, for that we just use this :

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)MouseMessages.WM_LBUTTONDOWN || m.Msg == (int)MouseMessages.WM_LBUTTONUP)
            MessageBox.Show("Click event caught!");  //return; --for suppress the click event action.
        else
            base.WndProc(ref m);
    }

but how to catch the mouse click event outside of the my app form?

BhushanK
  • 1,205
  • 6
  • 23
  • 39
  • kay rao balach cheshta karata ka aamachi – C Sharper Oct 01 '13 at 12:01
  • 3
    The icing on the cake would be a dialog popping up each time they try. – Adam Kewley Oct 01 '13 at 12:01
  • 2
    Read about mouse hooks... http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959%28v=vs.85%29.aspx – tim Oct 01 '13 at 12:11
  • Actually i wanted to capture mouse click events and get the handler of the clicked element and if clicked element handler is not my app form handler then suppress that event. – BhushanK Oct 01 '13 at 12:40
  • 3
    You'll need to use a low level mouse hook via WH_MOUSE_LL and trap WM_LBUTTONDOWN messages (and other appropriate mouse messages as needed). IF the cursor is not currently within the bounds of your app then you'd "suppress" that mouse event by returning 1 in the [hook procedure](http://msdn.microsoft.com/en-us/library/ms644986(VS.85).aspx). – Idle_Mind Oct 01 '13 at 12:51
  • 1
    Why don't you just force your application to be fullscreen and on top all the time? Having UI elements from other applications or the OS visible (and responding to mouseover etc.) that the user can't actually click on will be vastly confusing at the least. Unless you're creating some sort of virus/joke program where that is the exact purpose, of course... – Mels Oct 03 '13 at 07:47
  • just forcing my app to be full-screen isn't the solution according to my app requirements, i need to catch the mouse click events occurring anywhere in the windows. so my main question is how to handle global mouse click events from my app? – BhushanK Oct 03 '13 at 08:19
  • Whoever wrote your requirements needs to rethink their approach then. Allowing the user to see elements that they know to be interactive and not letting them interact with them will not bode well for your application. I give it 5 minutes before somebody gives up and uninstalls it, vowing never to return. – Sean Airey Oct 03 '13 at 08:44
  • 1
    And I don't think you can without going down in to the low lever hardware c++ things. The message queue from vista and up is not global but private per application. – Archlight Oct 03 '13 at 08:48
  • @Archilight : you want to say it is not possible in c# to catch the mouse events globally and suppress it's action. is it so? – BhushanK Oct 03 '13 at 09:12

1 Answers1

4

This way it can be done. It uses the win API function BlockInput.

NOTE: CTRL + ALT + DELETE enables the input again. But other mouse and keyboard input is blocked.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void BlockInput([In, MarshalAs(UnmanagedType.Bool)]bool fBlockIt);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Show();
            //Blocks the input
            BlockInput(true);
            System.Threading.Thread.Sleep(5000);
            //Unblocks the input
            BlockInput(false); 
        }
    }
}
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76