Very basic event handling question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Very basic event handling question

  1. #1
    Join Date
    Feb 2011
    Posts
    3

    Very basic event handling question

    I am very new to C++/CLI. So new in fact, that I don't even know C++. For a project in school, I have to make a basic program where you click on a shape and it creates a shape that you can move around the screen and such.

    The basic idea is that there is a class called shapeButton and when you left click on it, it creates an instance of a shape that when left clicked and dragged moves, and when right clicked is deleted. All this is to be done in a form.

    I've looked around and I can't find much help. The only real event handling I've done is basically painting stuff to the form. If I want to dynamically create instances, how would I set up those event handlers? Would they be in the individual .h files, or do I somehow have to set them up under the forms event handlers?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Very basic event handling question

    That's a pretty tough assignment for someone who is new to C++/CLI and doesn't even have a basis of C++. And I would definitely not call that "very basic". I hope you're at least not entirely new to programming at all.

    I managed to implement what you describe but it took me some time. The draggable shape (a pentagram) is a class derived from PictureBox. As an orientation: The class' .h file is 32 lines and the .cpp file is 45 lines. This does not include drawing the actual pentagram which is done in a separate class, but that class isn't really large. The isolation of the drawing code wouldn't actually have been necessary but I already had the pentagram drawing class from earlier experiments and simply reused it.

    The PentagramBox class implements five member functions, four of which are event handlers and the remaining one is the constructor, and has three data members. It handles all its events on its own except the right click to destroy the shape object, which is an event of the PentagramBox but is handled in the form class.

    The button that creates the shape is not a class I created myself but an ordinary Windows Forms button.

    To learn about event handling in .NET, Handling and Raising Events might be a good place to start, in particular the part about Events and Delegates. (You also can have a look at how a form class assotiates event handlers to its controls in the InitializeComponent() member function.) But note that this documentation is not specific to C++/CLI but to .NET in general and therefore be prepared that a considerable part of the sample code probably is only available in C#. Translating that or at least understanding what's ging on is another thing that's not that easy if you don't already know C# or at least have a solid general programming background, preferably including OOP.

    Though this is not a mandatory requirement, I strongly recommend separating classes into their own .cpp files (generally - in some rare cases I have closely related classes in a common file too) and having the declaration separate prom the implementation in their own .h files. This is simply the canonical way to do it in C++, and also leads to easier maintainable code and can also speed up compilation if used properly. Unfortunately the VC++ IDE doesn't really support that concept for C++/CLI: It puts the (empty) function bodies of event handlers it creates right into the class body in the form class' .h file. (For the initial form of a Windows Forms project, Form1, it doesn't even create a .cpp file; you have to do that yourself.) I always have to move them out of there manually, and though it's tideous I recommend it to anyone.

    Ah, and... Welcome to CodeGuru!
    Last edited by Eri523; February 26th, 2011 at 12:34 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2011
    Posts
    3

    Re: Very basic event handling question

    Thanks for the help!

    Yeah, this is my next CS class at the university of alabama. The 100 and 200 level classes have us using python, whereas the 300 levels and up are C++/CLI. While the engineering program is pretty big here, they haven't been too kind to the CS folk and at the moment the entire department is fairly fragmented. Popular theory is that they switched the 100 and 200 level classes over from C++ to python to ease the wash out rate, however they have not altered the curriculum for the higher level classes. As a result, CS 351 is using a legacy curriculum that basically expects us to have the same skillset as the juniors from several years ago. Its a fun situation. I'll post again if I have any more questions.

  4. #4
    Join Date
    Feb 2011
    Posts
    3

    Re: Very basic event handling question

    Ok, so I've basically just been experimenting around, trying to get some of this to work.

    I've started out by just kinda moving around a button. I just want to drag it around, but It is very unpredictable as to where it ends up.

    my code for my dragging event is:

    private: System::Void button1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
    if (e->Button == System::Windows::Forms::MouseButtons::Left ) {
    int x = e->X;
    int y = e->Y;
    this->button1->Location = System:rawing::Point(x, y);
    Invalidate();
    }

    I have an event on the the bottom of the form, also, that constantly tells me the x and y coordinates of the mouse. When I go to drag the button, the button will move, but will generally end up nowhere near where I try to put it.

    I know I'm missing something thats probably pretty obvious, but for the life of me, I just can't figure it out.

    My plan is to first just figure out how to drag things, then to dynamically create shapes.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Very basic event handling question

    First: Note that the mouse coordinates passed in the e parameter to the the button's MouseMove event handler are relative to the button while the button's Location property is relative to the form.

    Second: You can't determine the target coordinates of a drag operation in one reference frame (the form) from absolute mouse coordinates in another one (the button). You need to compute a distance vector in frame 1 and apply that to the coordinates in frame 2.

    Also, it looks like you're uncondintionally interpreting MouseMove events received by the button as a reason for the button to move. Even if you take the two objections mentioned above into account, this would lead to a pretty hard to control behaviour of the movable button. But maybe as a first quick-shot experiment... (Actually, this reminds me to a fun implementation of a button that "flees" from the mouse in order to avoid being clicked. This is a really old gag I already have seen back in the days of Windows 3.1. )

    My PentagramBox class has thee mouse-related event handlers:
    • MouseDown sets a boolean dragging flag and stores the initial mouse coordinates.
    • MouseMove computes a distance vector from the stored initial coordinates and the current ones, and then executes the move of the object. Of course it only does this if the dragging flag is set.
    • MouseUp resets the dragging flag.
    Last edited by Eri523; March 5th, 2011 at 11:29 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center