WPF コントロール移動 Thickness

Site Index



<Grid>
<GroupBox Header="groupBox1"
Name="groupBox1"
Height="100"
Width="200"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="150,50,0,0"
Background="AliceBlue"
PreviewMouseDown="groupBox1_PreviewMouseDown"
PreviewMouseMove="groupBox1_PreviewMouseMove"
PreviewMouseUp="groupBox1_PreviewMouseUp">
</GroupBox>
<StackPanel Height="100" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="80">
<TextBlock Height="20" Name="textBlockX" Text="TextBlock" />
<TextBlock Height="20" Name="textBlockY" Text="TextBlock" />
<TextBlock Height="20" Name="textBlock_move_X" Text="TextBlock" />
<TextBlock Height="20" Name="textBlock_move_Y" Text="TextBlock" />
</StackPanel>
<Button Content="Button" Height="30" HorizontalAlignment="Left" Margin="420,20,0,0"
Name="button1" VerticalAlignment="Top" Width="60" Click="button1_Click" />
</Grid>



double z_point_x_start_mouse = 0;
double z_point_y_start_mouse = 0;

double z_left = 0;
double z_top = 0;

Boolean z_mouse_down = false;

private void groupBox1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Point w_point = e.GetPosition(this);
this.textBlockX.Text = "x-->" + w_point.X;
this.textBlockY.Text = "y-->" + w_point.Y;
z_point_x_start_mouse = w_point.X;
z_point_y_start_mouse = w_point.Y;

Thickness w_margin = this.groupBox1.Margin;
z_left = w_margin.Left;
z_top = w_margin.Top;

z_mouse_down = true;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
double x = 100;
double y = 100;
this.groupBox1.SetValue(Grid.MarginProperty, new Thickness(x,y,0,0));
}
private void groupBox1_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (z_mouse_down)
{
Point w_point = e.GetPosition(this);
double w_new_x = w_point.X;
double w_new_y = w_point.Y;
this.textBlock_move_X.Text = "x-->" + w_new_x;
this.textBlock_move_Y.Text = "y-->" + w_new_y;

double w_set_x = z_left + (w_new_x - z_point_x_start_mouse);
double w_set_y = z_top + (w_new_y - z_point_y_start_mouse);

this.groupBox1.SetValue(Grid.MarginProperty, new Thickness(w_set_x, w_set_y, 0, 0));
}
}
private void groupBox1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
z_mouse_down = false;
}




AX