locked
C++ whiteboard

    Question

  • How do you create a whiteboard application using C++.

    Wednesday, August 8, 2012 1:52 AM

Answers

  • Hello,

    Do you want to write an app like draw with mouse?
    If so, we can use a Canvas and use PointerMoved event and PointerPressed event to add mouse points to a line and add this line to canvas' children. There is the xaml file

            <Canvas x:Name="Scenario1OutputRoot" Background="White" Height="300" Width="500" PointerMoved="Scenario1OutputRoot_PointerMoved" PointerPressed="Scenario1OutputRoot_PointerPressed" PointerReleased="Scenario1OutputRoot_PointerReleased"/>
    

    Here the cpp file

    	// MainPage.xaml.h
    	const int SUPPORTEDCONTACTS = 5;
        const double STROKETHICKNESS = 5;
    	public ref class MainPage sealed
    	{
    	private:
    
            int numActiveContacts;
    		Platform::Collections::Map<int, Windows::Foundation::Point>^ contacts;
    
    
    
    
    // ManiPage.xaml.cpp
    double Distance(Point currentContact, Point previousContact)
    {
    	return std::sqrt(std::pow(currentContact.X - previousContact.X, 2) + std::pow(currentContact.Y - previousContact.Y, 2));
    }
    void Drawpath::MainPage::Scenario1OutputRoot_PointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	Windows::UI::Input::PointerPoint^ pt = e->GetCurrentPoint(Scenario1OutputRoot);
    	int ptrId = pt->PointerId;
    
    	if (contacts->HasKey(ptrId) )
    	{
    		Point currentContact = pt->Position;
    		Point previousContact = contacts->Lookup(ptrId);
    		if (Distance(currentContact, previousContact) > 4)
    		{
    			Windows::UI::Xaml::Shapes::Line^ l = ref new Windows::UI::Xaml::Shapes::Line();
    
    			l->X1 = previousContact.X;
    			l->Y1 = previousContact.Y;
    			l->X2 = currentContact.X;
    			l->Y2 = currentContact.Y;
    			l->StrokeThickness = STROKETHICKNESS;
    			l->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Red);
    			l->StrokeEndLineCap = PenLineCap::Round;
    
    
    			contacts->Insert(ptrId,currentContact);
    			Scenario1OutputRoot->Children->Append(l);
    		}
    	}
    
    	e->Handled = true;
    }
    
    
    void Drawpath::MainPage::Scenario1OutputRoot_PointerPressed(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	if (numActiveContacts >= SUPPORTEDCONTACTS)
    	{
    		// cannot support more contacts
    		return;
    	}
    	Windows::UI::Input::PointerPoint^ pt = e->GetCurrentPoint(Scenario1OutputRoot);
    	contacts->Insert(pt->PointerId, pt->Position);
    	e->Handled= true;
    	++numActiveContacts;
    }
    
    
    void Drawpath::MainPage::Scenario1OutputRoot_PointerReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	int ptrId = e->GetCurrentPoint((FrameworkElement^)sender)->PointerId;
    
    	if (contacts->HasKey(ptrId))
    	{
    
    		contacts->Remove(ptrId);
    		--numActiveContacts;
    	}
    	e->Handled = true;
    }
    


    Best regards,
    Jesse


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

    • Marked as answer by Jesse Jiang Monday, August 13, 2012 8:35 AM
    Thursday, August 9, 2012 11:48 AM