Grasshopper

algorithmic modeling for Rhino

hello guys,

i'm currently trying to implement a mouselistener into an grasshopper component, but i'm a little bit lost.

i wrote a small testclass and implemented it in my component and everything works just fine when the mouslistener is out-commentated. when not, grasshopper throws an exception by the target of inovacation at startup.

below the mouselistener test class:

class test
    {
        private String state= "start";

        private System.Windows.Forms.MouseEventHandler MouseDown;
     
        public test()
        {
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
        }

        public string getState()
        {
            return state;
        }

        protected void Form_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case System.Windows.Forms.MouseButtons.Left:
                    this.state= "left last";
                    break;
                case System.Windows.Forms.MouseButtons.Right:
                    this.state= "right last";
                    break;
                case System.Windows.Forms.MouseButtons.Middle:
                    this.state= "middle last";
                    break;
            }
        }
    }

every suggestion would be great

Views: 2007

Replies to This Discussion

Hi Johannes,

which mouse events do you want to catch? Rhino Viewport? Grasshopper Canvas? Only mouse events within the bounds of your Component? All windows mouse events?

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hi David,

I basically want to catch all windows mouse events. So in the test class I just tryed to an overall buttonPressed event that saves the last pressed button in the state string. My component the calls the last pressed Button with getState() and sends it to data out. With some delay the component should get reexecuted.
There is no mechanism in DotNET for responding to all mouse events. When your code is running inside a Windows.Forms.Form or Windows.Forms.Control you can get the events that go into that one object.

If you need to listen to all mouse events everywhere, you'll need to PInvoke the Windows API directly and probably register message hooks. I'm not going down this path.

Another solution is to have a very tight timer that tests the location of a button state of the active cursor. You could then decide on a 5ms or 10ms basis if the state has changed and whether or not you care about that change. I'm not a big fan of timers either though, as they are processor intensive, wasteful, difficult to debug and unreliable.

Cursor location can be found via:
Windows.Forms.Cursor.Position

Mouse button state via:
Windows.Forms.Control.MouseButtons

Both of these are static properties.

GH_Component (or any of the classes it derives from) does not have a MouseDown event, so you cannot register it using "this.MouseDown += ".

--
David Rutten
david@mcneel.com
Poprad, Slovakia
here is some code i use to trap all double click events. i use this in a special case so it may or may not work for you

protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
const long WM_NCLBUTTONDBLCLK = 163;

if ((long)m.Msg == WM_NCLBUTTONDBLCLK) //We trapped DblClick or cange to whatever
{
//add whatever you want to happen on double click here
}
//Continue processing the message
base.DefWndProc(ref m);
}
@ David:
Working with timers will not work for me, because later I would like to try to do some kind of MOuse-Logging and store some click positions in a list.

Therefore I tried now to P/Invoke user32 and kernel32. In a dummy project this works now just fine, but when i add this to my component i get a b77a5c561934e089 error in Windows.Forms. I know that this is some kind of permission problem within the Mouselistener, but the grasshopper error tells me not a missing permission but that he can not find the file?

I already tried out to set [assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")] but it still does not work.

@Robert:
Did you use this code within a grasshopper component? 'cause when I use this, i have to extend the namespace of the class to Windows.Forms or Control so that I can use the base.DefWndProc(ref m);

But this throws then an error when grasshopper tries to initialize the component on startup.
yes i did use this in a grasshopper def. the base.defwndProc works because the function is inside a class that inherits form window.forms
ok, thanks, i'll try that

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service