PDA

View Full Version : How to construct a Mouse object in .Net


Jon-Huhn
06-18-2008, 08:41 PM
Anyone know how to construct a Mouse object in .Net? I tried:

dotNetObject "System.Windows.Input.Mouse"

but it tells me "Runtime error: No constructor found which matched argument list".

Thanks

magicm
06-18-2008, 10:20 PM
I don't think this class can be instantiated. Instead use it's members directly;
(dotNetClass "System.Windows.Input.Mouse").GetPosition ...

I can't test it here, but I suppose that should work.

Here's the MS page listing all it's available members:
http://msdn.microsoft.com/en-us/library/system.windows.input.mouse_members.aspx

Martijn

Jon-Huhn
06-18-2008, 11:45 PM
Thanks, Martijn

Maybe I was asking the wrong question entirely, so now I'll be specific to what I'm trying to do.

I'm wanting to make a system that allows dialogs to be resized only when the user pulls specific corners/edges of the dialog, so that, for instance, I can lock the width of a dialog and only allow the user to pull the bottom edge to make it longer.

So what I have is a .net button stretched out long and skinny and placed at the bottom edge of the dialog. My intention is to have the user click and drag on that bar in order to resize the dialog.

So what I need now is the ability to track the change in mouse position once the user presses the button and starts dragging. I know I can use a maxscript or .net timer to check the mouse position every few milliseconds, but I'd really rather just have the function called when the user moves the mouse.

As far as I can tell, I've exhausted the possibilities of maxscript alone to track the mouse position for me, so now I'm looking to .net.

Does anyone know how to setup the .net mouse object so it calls a function every time the mouse is moved?

Thanks!

magicm
06-19-2008, 12:02 AM
You could use the floating dialog events for this (see the CreateDialog topic in the online reference). Here's a quick example:

(
rollout ResizeTest "Vertical Resize" width:200 height:200
(
local IsResizing = false -- true if the dialog is being resized
local StartPosition -- mouse position at 'lbuttondown' event
local StartHeight -- dialog height at 'lbuttondown' event

on ResizeTest lbuttondown pos do
(
-- mouse down near the bottom?
if pos.y >= ResizeTest.height - 10 then
(
-- store mouse position and dialog height
StartPosition = pos
StartHeight = ResizeTest.height

-- set resizing flag
IsResizing = true
)
)

on ResizeTest mousemove pos do
(
-- resize flag set?
if IsResizing then
(
-- resize the dialog, clamping its height between 40 and 400
ResizeTest.height = amin 400 (amax 40 (StartHeight + (pos - StartPosition).y))
)
)

on ResizeTest lbuttonup pos do
(
-- unset resizing flag
IsResizing = false
)

on ResizeTest rbuttondown pos do
(
-- resize flag set?
if IsResizing then
(
-- revert to initial height
ResizeTest.height = StartHeight

-- unset resizing flag
IsResizing = false
)
)
)

createDialog ResizeTest
)
EDIT: Added 'rbuttondown' event: Clicking the right mousebutton while resizing stops the action and reverts the dialog to its initial size.

Hope this helps,
Martijn

Jon-Huhn
06-19-2008, 12:12 AM
Martijn,

That's a nice solution, but I'd really like to have a graphic representation of some kind over the "hot" area so the user knows that's where they should start dragging, and of course, the mouseMove event only fires when the cursor is over the bare rollout (it's these technicalities that are causing me to look at the .net solution now)

Thanks!

ZeBoxx2
06-19-2008, 12:30 AM
-- no .net info in this post --
- you could use an imgtag in the bottom, that's got its own set of mouse events
- if you only want to lock width/height, why not do so in the createDialog call? See: lockWidth/lockHeight. I'm guessing there's more to what you want than that..

magicm
06-19-2008, 12:31 AM
Here's a version that uses a dotNet label control instead. You could set the label's background property or add an image to it to make it look like a resize grip. Or you could even use the Paint event to manually draw its contents.

(
rollout ResizeTest "Vertical Resize" width:200 height:200
(
local IsResizing = false -- whether the dialog is being resized
local StartPosition -- mouse position at 'lbuttondown' event
local StartHeight -- dialog height at 'lbuttondown' event

dotNetControl ResizeLabel "System.Windows.Forms.Label" pos:[0,192] width:200 height:8

on ResizeLabel MouseDown EventArgs do
(
-- left mousebutton pressed?
if EventArgs.Button == EventArgs.Button.Left then
(
-- store mouse position and dialog height
StartPosition = [EventArgs.X, EventArgs.Y]
StartHeight = ResizeTest.height

-- set resizing flag
IsResizing = true
)
else if IsResizing and EventArgs.Button == EventArgs.Button.Right then
(
-- revert to initial height
ResizeTest.height = StartHeight
ResizeLabel.pos.y = ResizeTest.height - ResizeLabel.height

-- unset resizing flag
IsResizing = false
)
)

on ResizeLabel MouseMove EventArgs do
(
-- resize flag set?
if IsResizing then
(
local MousePosition = [EventArgs.X, EventArgs.Y]

-- resize the dialog, clamping its height between 40 and 400
ResizeTest.height = amin 400 (amax 40 (StartHeight + (MousePosition - StartPosition).y))

-- update the label's position
local LabelPosition = ResizeTest.height - ResizeLabel.height
StartPosition -= LabelPosition - ResizeLabel.pos.y
ResizeLabel.pos.y = LabelPosition
)
)

on ResizeLabel MouseUp EventArgs do
(
-- unset resizing flag
if EventArgs.Button == EventArgs.Button.Left then IsResizing = false
)

on ResizeTest open do
(
ResizeLabel.Cursor = (dotNetClass "System.Windows.Forms.Cursors").SizeNS
)
)

createDialog ResizeTest
)
Cheers,
Martijn

Jon-Huhn
06-19-2008, 01:10 AM
Richard,

I had tried imgTag, but was having problems since that control doesn't detect the mouseup event if the cursor is off of it at the time the mouse button is released. But thanks so much for pointing me to the lockWidth/height keywords. Even though I've been using createDialog() for years, for some reason I never noticed those two options. You are right, however, that what I'm wanting to do is a little more complicated than just locking the width or height (although I will use these keywords in other situations from now on.... thanks a bunch!)

Martijn,

Your example script totally nailed it. I'll need to adapt it to work with my stuff (or maybe I should just adapt my stuff to work with your script ;) ), but it's enough to get me going. And changing the cursor to the resize arrow cursor was also on my list of things to work out... so you just saved me a few more hours of research.

Thanks again both of you.

CGTalk Moderation
06-19-2008, 01:10 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.