mardi 31 mars 2015

How to get tap position to add control?

I have a Windows phone 8.0 project that has an image. When the user taps on the image, I have to get the position of the tap and add a text box to this location (above image).


For now, I use the following code, but I always get a wrong position



MessageBox.Show("vao day");
var mp = GetMousePoint(e);
MessageBox.Show("x=" + mp.X + "_" + "y=" + mp.Y);
//x: 258; y:97
double top= 0; double right = 0; double bottom =0; double left=0;
top = e.GetPosition(OrginalImage).Y; //(double)mp.Y - 50;
left = e.GetPosition(OrginalImage).X; //(double)mp.X - 50;

MessageBox.Show("x=" + left.ToString() + "_" + "y=" + top.ToString());
//add textbox to this position
TextBox newtext = new TextBox();
newtext.Text = "truongpm";
newtext.Margin = new Thickness(left, top, right, bottom);
newtext.Width = 124;
newtext.Height = 68;

//button.VerticalAlignment = 234;
//Add contentpanel to your page if there's not one already.
ContentPanel.Children.Add(newtext);
newtext.Visibility = System.Windows.Visibility.Visible;


enter image description here


Can anybody help me please! Thanks


This is my xaml code



<Grid x:Name="ContentPanel" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" MouseMove="Image_MouseMove" Name="OrginalImage" Stretch="Fill" Height="250" Source="/Assets/vd1.png"/>
<Button Grid.Row="1" Content="ALter" Width="100" Height="80" Click="AlterButton_Click" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="10,0,360,0"/>
<Button Grid.Row="1" Content="Save" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,255,0" Click="Button_Click"></Button>
<Button Grid.Row="1" Content="Text" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,055,0" Click="TextButton_Click"></Button>
<Image Grid.Row="2" Name="AlterImage" />

<!--<TextBox HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180" Margin="110,65,0,0" Grid.Row="0"/>-->
</Grid>

</Grid>

Aucun commentaire:

Enregistrer un commentaire