WPF Polygon and MouseDown Event in VB.NET

In this article you will learn that how to draw Polygon Dynamically and how you can handle its MouseDown Event.
  • 4384
 

Introduction 

Here in this article I am explaining that how you can draw Polygon and handle its Mouse Down Event in WPF. WPF offers a lot of control in its different layers. In top layer it offers shape object. But sometimes we need to draw shapes and objects Dynamically. As in this example we are drawing Polygon and also showing the Polygons MouseDown Event. When you click the Polygon a MessageBox will display the message. The implementation only needs a StackPanel control and a series of namespaces related to graphics and shapes.

Getting Started

  • Simply Create a new WPF application
  • Drag a StackPanel and dynamically draw a Ploygon on MainWindow. Your window will look like below.

    poly1.gif
     
  • Your MainWindow.xaml page will look like below.

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="275" Width
    ="278">
        <StackPanel Orientation="Horizontal" >
            <Polygon Fill="DeepSkyBlue"   Canvas.Left="100" Canvas.Top="30" Points=" 50 20 100 100 10 100"
     MouseDown
    =" Polygon_MouseDown"/>
        </StackPanel>
    </
    Window>
     
  • Then add the below code in code behind file.

        
    Private Sub Polygon_MouseDown(sender As Object, e As MouseButtonEventArgs)
            MessageBox.Show("polygon shape")
        End 
    Sub
     
  • Now run your application.
Output 

poly2.gif

poly3.gifpoly4.gif

Summary 

In this article you learned that how you can draw Polygon and handle its MouseDown Event.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.