curiosity
curiosity

Reputation: 1233

To restrict mouseclick

I want to restrict the mouseclick within a region and if any control are there within the region, it should allow mouseclick.

How to do that

Upvotes: 1

Views: 648

Answers (3)

ileon
ileon

Reputation: 1508

You probably need to capture the mouse, besides handling mouse down/up messages.

Unfortunately, the best way to capture the mouse, is to watch for the WM_CAPTURECHANGED message, meaning that we need to go down into the Win32 API to watch for this event, since Windows Forms don't define a corresponding event. For example, in order to handle this Win32 message, first you define an inner class to handle the low-level message WM_CAPTURECHANGED, and define the event handler:

public partial class Form1 : Form
{
    class CaptureChangedWindow : NativeWindow
    {
        public CaptureChanged OnCaptureChanged;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 533)     // WM_CAPTURECHANGED
                OnCaptureChanged();
            base.WndProc(ref m);
        }
    }
    public delegate void CaptureChanged();
    ...
}

Next declare a member field to hold the CaptureChangedWindow instance:

public partial class Form1 : Form
{
    CaptureChangedWindow ccw;
    ...
}

Next define a method that will be invoked when CaptureChanged delegate is invoked:

public partial class Form1 : Form
{

    private void CaptureChangedEventHandler()
    {
        // your code
        // e.g. now it's safe to assume that mouse is captured
    }
    ...
}

Finally, modify the constructor to create and initialize the nested class:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ccw = new CaptureChangedWindow();
        ccw.AssignHandle(Handle);
        ccw.OnCaptureChanged +=
                  new CaptureChanged(CaptureChangedEventHandler);
    }
    ...
}

That's all you need. You can then process with other mouse events as usual.

Upvotes: 0

Sean Xiong
Sean Xiong

Reputation: 125

You just don't response to the mouseclick event for the region and implement the event of its children controls.

Or I don't get you.

Upvotes: 1

Timores
Timores

Reputation: 14589

In your form, override the WndProc method, handle the Mouse left message and if it is in the accepted region, call the base class, otherwise swallow the message (i.e. do not call the base class)

Upvotes: 1

Related Questions