locked
how to implement drawing polygons using mouse events such as throw mouseEntered mouseMoved and mouseexited events in metro app using c++\cx language.

    Question

  • how to implement drawing polygons using mouse events such as throw mouseEntered mouseMoved and mouseexited events in metro app using c++\cx language.
    Thursday, July 26, 2012 12:25 PM

All replies

  • How to draw polygons programally in metro app using c++ language...
    Wednesday, July 25, 2012 11:29 AM
  • Hi,

    There are two ways to achieve this,
    You can add the xaml code in the design tool

    <Polygon Fill="LightBlue"
             Points="10,200,60,140,130,140,180,200" />

    Or you can add codes into your cpp file.

    	Polygon^ polygon=ref new Polygon();
    	PointCollection pointcol;
    	pointcol.Append(Point(10,200));
    	pointcol.Append(Point(60,140));
    	pointcol.Append(Point(140,130));
    	pointcol.Append(Point(180,200));
    	polygon->Points=%pointcol;
    	polygon->Fill=ref new SolidColorBrush(Windows::UI::Colors::White);
    	this->Gid1->Children->Append(polygon);
    

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Rebecca Liu Friday, August 03, 2012 1:56 AM
    Thursday, July 26, 2012 8:41 AM
  • hi

    but I actually need the code for drawing a simple poly gon throw the mouse pointer.that means throw pointerEntered(),pointerMoved(),pointerExited() events .please help me for that.and also thankz jesse for providing above codes.

    Regards ,

    sarath s

    Thursday, July 26, 2012 9:23 AM
  • Metro style apps use Pointer messages instead of Mouse messages. See Responding to user interaction . You can use pointer events to generate the points for a Windows::UI::Xaml::Shapes::Polygon object.

    --Rob

    • Proposed as answer by Rebecca Liu Friday, August 03, 2012 1:56 AM
    Thursday, July 26, 2012 5:19 PM
    Owner
  • hi ,

    But I am trying to draw polygon throw the mouse movement in the grid.

    --sarath s

    Friday, July 27, 2012 3:31 AM
  • Hi Sarath,

    What sort of problem are you having doing so? Do you not understand how to handle Pointer events? Do you still not understand how to create a Polygon? Do you not understand how to connect the two?

    The specific details will depend on exactly what behavior you want from your app. How exactly do you expect the user to define the polygon with the mouse? If you want the user to click in several locations to set the points for the polygon then you can do that by handling PointerUp events, getting the location that was clicked, and then adding that point to the polygon. You may want to do some extra handling to provide good feedback to the user. For example, you might draw a small box at each point so the user can see where they have clicked as they move along.

    --Rob

    Saturday, July 28, 2012 1:47 AM
    Owner
  • Hi..

    please provide me a example code for that....

    Regards,

    sarath s

    Monday, July 30, 2012 3:52 AM
  • Hi sarath,

    I have tried your idea on my machine with VS2012 Ultimate version. I created a blank Metro style app, dragged a Rectangle from Toolbox onto the mainpage.xmal. Then I declared two objects in the MainPage.xaml.h and initialized them in the .cpp file like this:

    //in MainPage.xaml.h 	
    private:
    	Polygon^ polygon;
    private: 
    	PointCollection^ pointcol;
    
    //in the MainPage constructor in MainPage.xaml.cpp
    polygon=ref new Polygon();
    pointcol=ref new PointCollection();

    Add event handlers for the rectangle: PointerEntered, PointerExited, and PointerMoved. The code snippets are as follows:

    void App3::MainPage::Rectangle_PointerEntered_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	pointcol->Append(Point(e->GetCurrentPoint(this)->Position.X, e->GetCurrentPoint(this)->Position.Y));	
    	
    }
    
    void App3::MainPage::Rectangle_PointerExited_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	polygon->Points=pointcol;
    	polygon->Fill=ref new SolidColorBrush(Windows::UI::Colors::Black);
    	this->Gid1->Children->Append(polygon);
    
    }
    
    void App3::MainPage::Rectangle_PointerMoved_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	pointcol->Append(Point(e->GetCurrentPoint(this)->Position.X, e->GetCurrentPoint(this)->Position.Y));	
    
    }

    After I built App3 and ran it, I moved the mouse in the rectangle, and a black polygon was generated.

    I hope this reply is helpful to you.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Rebecca Liu Friday, August 03, 2012 1:56 AM
    Monday, July 30, 2012 9:43 AM
  • hi.

    there is an error after executing the code that the element is already child of an anther .After removing the statement  (this->Gid1->Children->Append(polygon); ).I can't draw a polygon with Black color....

    Monday, July 30, 2012 1:06 PM
  • Hi sarath.satheesh,

    You can only draw one polygon in the rectangle after the application runs. If you want to draw more than one polygons, you can put the initialization lines of Polygon and PointCollection into the PointerEntered event. Here are the examlpes you can take of:

    void App3::MainPage::Rectangle_PointerEntered_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	polygon=ref new Polygon();
    	pointcol=ref new PointCollection();
    	pointcol->Append(Point(e->GetCurrentPoint(this)->Position.X, e->GetCurrentPoint(this)->Position.Y));	
    }
    Best regards,
    Helen Zhao

    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 31, 2012 3:10 AM
  • hi...

    there is an exception showing for this code that.

    "Win RT information: Element is already the child of another element"

    How it can be solved..?

    Tuesday, July 31, 2012 6:30 AM
  • Hi sarath.satheesh, 

    You got this exception because that the polygon and PointCollection objects were defined in the MainPage constructot. There is only one pair of Polygon and PointColletion. If we put the initialization of them into the PointerEntered event, there is one pair created every time the mouse enteres the rectangle. Here is the whole code snippets you can try. I have tested and it can run without exceptions on my side:

    //in MainPage.xaml.h 	
    private:
    	Polygon^ polygon;
    private: 
    	PointCollection^ pointcol;

    //in MainPage.xaml.cpp file. void App3::MainPage::Rectangle_PointerEntered_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) { polygon=ref new Polygon();
    pointcol
    =ref new PointCollection();
    pointcol->Append(Point(e->GetCurrentPoint(this)->Position.X, e->GetCurrentPoint(this)->Position.Y)); } void App3::MainPage::Rectangle_PointerExited_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) { polygon->Points=pointcol; polygon->Fill=ref new SolidColorBrush(Windows::UI::Colors::Black); this->Gid1->Children->Append(polygon); } void App3::MainPage::Rectangle_PointerMoved_1(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) { pointcol->Append(Point(e->GetCurrentPoint(this)->Position.X, e->GetCurrentPoint(this)->Position.Y)); }


    Best regards,
    Helen Zhao

    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Helen Zhao Thursday, August 02, 2012 7:18 AM
    Thursday, August 02, 2012 7:16 AM
  • hi...

    its throwing same error which is mentioned above.....even drawing with black color is not possible.

    Thursday, August 02, 2012 9:22 AM