memcpy of a MouseEventArgs

J

jleslie48

any idea how to accomplish this:

public MouseEventArgs mousedown_event = new MouseEventArgs();

private void panel1_MouseDown(object sender,MouseEventArgs e) {

//the next line needs a C# version.
memcpy(mousedown_event,e,sizeof(e));



}//panel1_MouseDown
 
J

jleslie48

any idea how to accomplish this:

public MouseEventArgs mousedown_event = new MouseEventArgs();

private void panel1_MouseDown(object sender,MouseEventArgs e) {

        //the next line needs a C# version.
        memcpy(mousedown_event,e,sizeof(e));

         }//panel1_MouseDown

apparently this will work:



public MouseEventArgs mousedown_event;

private void panel1_MouseDown(object sender,MouseEventArgs e) {

//the next line needs a C# version.
//memcpy(mousedown_event,e,sizeof(e));
md_e = new MouseEventArgs(e.Button,e.Clicks,e.X,e.Y,e.Delta);

}//panel1_MouseDown


but I'm not thrilled. I don't like having to call out each of the
members. any better way to do it.?
 
J

jleslie48

jleslie48 said:
[...]
private void panel1_MouseDown(object sender,MouseEventArgs e) {
        //the next line needs a C# version.
        //memcpy(mousedown_event,e,sizeof(e));
        md_e = new MouseEventArgs(e.Button,e.Clicks,e.X,e.Y,e..Delta);
         }//panel1_MouseDown
but I'm not thrilled.  I don't like having to call out each of the
members.  any  better way to do it.?

What are you _actually_ trying to do?  Why do you want a new instance of
MouseEventArgs in the first place?  Why not just keep a reference to the
instance passed to you?

Pete

I need to record the mouse down for later use. if the user mouses up
at a different location (drags mouse) I need to know. at the later
date the
e data will be overwritten.
 
J

jleslie48

jleslie48 said:
jleslie48 wrote:
[...]
private void panel1_MouseDown(object sender,MouseEventArgs e) {
        //the next line needs a C# version.
        //memcpy(mousedown_event,e,sizeof(e));
        md_e = new MouseEventArgs(e.Button,e.Clicks,e.X,e.Y,e..Delta);
         }//panel1_MouseDown
but I'm not thrilled.  I don't like having to call out each of the
members.  any  better way to do it.?
What are you _actually_ trying to do?  Why do you want a new instance of
MouseEventArgs in the first place?  Why not just keep a reference tothe
instance passed to you?
Pete
I need to record the mouse down for later use.  if the user mouses up
at a different location (drags mouse) I need to know. at the later
date the
e data will be overwritten.

Typically, you would just save the particular piece of data of interest.
  For example, have a Point field and copy the Location from the mouse
event to that field as needed.

But regardless, it's still not clear why you want to make a copy of the
MouseEventArgs.  Even if we assume that there's a good reason to have
the entire MouseEventArgs object, why (as I asked in my previous
message) don't you just keep a reference to the MouseEventArgs instance
that was passed to you?

Pete

the reference will have its value change as other mouse events occur
won't it?

When I go to program MouseMove it want to know the starting postion
that I saved in md_e,

IN your way, all I saved was the pointer to the mouseeventargs and the
data that it points to will be destroyed.
 
F

Felix Palmen

* jleslie48 said:
the reference will have its value change as other mouse events occur
won't it?

No. When an Event is raised in .NET, there's always a /new/ instance of
EventArguments created. Btw, you should do the same when raising an
event yourself.

The individual fields of an EventArguments object /could/ reference
other objects that are modified later by some other code, but the fields
of EventArguments itself will not be changed.
IN your way, all I saved was the pointer to the mouseeventargs and the
data that it points to will be destroyed.

Oh and reading this, I get the impression you're thinking in terms of
explicit memory management and pointer ownership. Always keep in mind
that .NET employs a garbage collector that will only destroy objects
when there's no reference left. As long as YOU keep one, the object
stays alive.

Having said this, there IS a way of using explicit memory management and
explicit pointers (and even a call like memcpy() if you like) in C#,
look for the "unsafe" keyword. It won't help you with your problem here,
because you can't assume anything about memory layout of managed objects
(see (un-)marshalling and platform-invoke for that), but I thought you
might be interested to know.

Regards,
Felix
 
J

jleslie48

No. When an Event is raised in .NET, there's always a /new/ instance of
EventArguments created. Btw, you should do the same when raising an
event yourself.

The individual fields of an EventArguments object /could/ reference
other objects that are modified later by some other code, but the fields
of EventArguments itself will not be changed.
IN your way, all I saved was the pointer to the mouseeventargs and the
data that it points to will be destroyed.

Oh and reading this, I get the impression you're thinking in terms of
explicit memory management and pointer ownership. Always keep in mind
that .NET employs a garbage collector that will only destroy objects
when there's no reference left. As long as YOU keep one, the object
stays alive.

Having said this, there IS a way of using explicit memory management and
explicit pointers (and even a call like memcpy() if you like) in C#,
look for the "unsafe" keyword. It won't help you with your problem here,
because you can't assume anything about memory layout of managed objects
(see (un-)marshalling and platform-invoke for that), but I thought you
might be interested to know.

Regards,
Felix

--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE39 32F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683

Ahhh!!! now this makes a bit more sense. so what your saying is a
new mouseeventargs instance (variable name e by gui template
creation)
is spontaniously created for every time the event handler is called,
and left on its own, destroyed/freed when the event handler finishes.
If I simply set
an external pointer to e the fact that an active pointer is still
looking at the instance will prevent the garbage collector from
freeing that memory. any further call to the event handler will
create a new "e" instance, and the old one that I have pointed to by
my external pointer mp_e still has the old instance. ( I know, the
word pointer is a dirty word in the C# world but hey lets call a spade
a spade.)

I did hear about using the unsafe explicit memory management, but that
defeats me using C# in the first place. I'm trying to work within the
paradigm of C# and not fight it with my old straight C skillset. I
knew that C# must have a way to effectively let me manage copies/
temporary variables of things.

I assume that once I take mp_e and assign it a new value the old e
instance that it was pointing to is taken care of by the garbage
collector in the background correct?
 
F

Felix Palmen

* jleslie48 said:
If I simply set
an external pointer to e the fact that an active pointer is still
looking at the instance will prevent the garbage collector from
freeing that memory. any further call to the event handler will
create a new "e" instance, and the old one that I have pointed to by
my external pointer mp_e still has the old instance. ( I know, the
word pointer is a dirty word in the C# world but hey lets call a spade
a spade.)

Better call it reference. Although it is the same when it comes to
implementation in machine code, it is NOT the same in program code. A
pointer is explicitly used as such while a reference is dereferenced
automatically when needed, hidden from the programmer. Because both
concepts exist in C# (and, btw, also in C++), it makes sense to
distinguish them.
I assume that once I take mp_e and assign it a new value the old e
instance that it was pointing to is taken care of by the garbage
collector in the background correct?

It will eventually. A conservative garbage collector will look for
abandoned objects as soon as new memory is to be allocated. I don't know
the implementation details of the .NET garbage collector. The price you
pay for garbage collection is never knowing exactly when your object
will be destroyed. Most of the time, it doesn't matter.

Regards,
Felix
 
J

jleslie48

* jleslie48 said:
If I simply set
an external pointer to e  the fact that an active pointer is still
looking at the instance will prevent the garbage collector from
freeing that memory.  any further call to the event handler will
create a new "e" instance, and the old one that I have pointed to by
my external pointer mp_e still has the old instance.  ( I know, the
word pointer is a dirty word in the C# world but hey lets call a spade
a spade.)

Better call it reference. Although it is the same when it comes to
implementation in machine code, it is NOT the same in program code. A
pointer is explicitly used as such while a reference is dereferenced
automatically when needed, hidden from the programmer. Because both
concepts exist in C# (and, btw, also in C++), it makes sense to
distinguish them.
I assume that once I take mp_e and assign it a new value the old e
instance that it was pointing to is taken care of by the garbage
collector in the background correct?

It will eventually. A conservative garbage collector will look for
abandoned objects as soon as new memory is to be allocated. I don't know
the implementation details of the .NET garbage collector. The price you
pay for garbage collection is never knowing exactly when your object
will be destroyed. Most of the time, it doesn't matter.

Regards,
Felix

--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE39 32F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683

Thank you for the clarification. I'll put it to the test on Monday.
When the garbage collector frees the memory is not an issue. So long
as it doesn't free memory that still has an active "reference"
 
J

jleslie48

Better call it reference. Although it is the same when it comes to
implementation in machine code, it is NOT the same in program code. A
pointer is explicitly used as such while a reference is dereferenced
automatically when needed, hidden from the programmer. Because both
concepts exist in C# (and, btw, also in C++), it makes sense to
distinguish them.
It will eventually. A conservative garbage collector will look for
abandoned objects as soon as new memory is to be allocated. I don't know
the implementation details of the .NET garbage collector. The price you
pay for garbage collection is never knowing exactly when your object
will be destroyed. Most of the time, it doesn't matter.
Regards,
Felix

--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE39 32F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683

Thank you for the clarification.  I'll put it to the test on Monday.
When the garbage collector frees the memory is not an issue.  So long
as it doesn't free memory that still has an active "reference"

Tested and confirmed true. By storing off the MouseEventArgs e of
the:
mousedown,
mouseup,
mousemove

in a public variable of the main form, I now can track all the users
mouse commands completely from start of click through drag, until the
user releases the mouse button.

Thanks everybody for a very enlightening conversation.
 
J

jleslie48

On Sep 18, 1:49 pm, (e-mail address removed) (Felix Palmen) wrote:
* jleslie48 <[email protected]>:
If I simply set
an external pointer to e  the fact that an active pointer is still
looking at the instance will prevent the garbage collector from
freeing that memory.  any further call to the event handler will
create a new "e" instance, and the old one that I have pointed to by
my external pointer mp_e still has the old instance.  ( I know, the
word pointer is a dirty word in the C# world but hey lets call a spade
a spade.)
Better call it reference. Although it is the same when it comes to
implementation in machine code, it is NOT the same in program code. A
pointer is explicitly used as such while a reference is dereferenced
automatically when needed, hidden from the programmer. Because both
concepts exist in C# (and, btw, also in C++), it makes sense to
distinguish them.
I assume that once I take mp_e and assign it a new value the old e
instance that it was pointing to is taken care of by the garbage
collector in the background correct?
It will eventually. A conservative garbage collector will look for
abandoned objects as soon as new memory is to be allocated. I don't know
the implementation details of the .NET garbage collector. The price you
pay for garbage collection is never knowing exactly when your object
will be destroyed. Most of the time, it doesn't matter.
Regards,
Felix
--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE3932F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683
Thank you for the clarification.  I'll put it to the test on Monday.
When the garbage collector frees the memory is not an issue.  So long
as it doesn't free memory that still has an active "reference"

Tested and confirmed true.  By storing off the  MouseEventArgs e of
the:
mousedown,
mouseup,
mousemove

in a public variable of the main form, I now can track all the users
mouse commands completely from start of click through drag, until the
user releases the mouse button.

Thanks everybody for a very enlightening conversation.

Here is the code snippets of how it works:

namespace WindowsFormsApplication01
{
public partial class Form1 : Form
{


public MouseEventArgs md_e;
public MouseEventArgs mm_e;
public bool draw_crosshairs;


....
private void panel1_MouseDown(object sender,MouseEventArgs e) {

md_e = e;
....
draw_crosshairs = true;
}//panel1_mousedown

private void panel1_MouseMove(object sender,MouseEventArgs e) {

mm_e = e;

panel1.Invalidate(); //this will force the crosshairs to be
//drawn at the mouse pointer location.
}//panel1_MouseMove

private void panel1_MouseUp(object sender,MouseEventArgs e) {
writeln_output02(
"click value: " +

md_e.X +"x, " +
md_e.Y +"y ---> "+
e.X+ "x, " +
e.Y + "y " +

"");
draw_crosshairs = false;
} //panel1_MouseUp

....

protected override void OnPaint(PaintEventArgs pe) {

Pen a_sharpie = new Pen(Brushes.Black);


if (myform1ptr.draw_crosshairs) {
pe.Graphics.DrawLine(a_sharpie,
new
Point(myform1ptr.mm_e.X-90, myform1ptr.mm_e.Y),
new Point(myform1ptr.mm_e.X
+90, myform1ptr.mm_e.Y)
); //drawline horizontal
crosshair

pe.Graphics.DrawLine(a_sharpie,
new Point(myform1ptr.mm_e.X,
myform1ptr.mm_e.Y-60),
new Point(myform1ptr.mm_e.X,
myform1ptr.mm_e.Y+60)
); //drawline vertical
crosshair
}// mousemovement while button is down

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top