C#wpf中的画布处理 [英] Canvas handling in C# wpf
本文介绍了C#wpf中的画布处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
任何人都可以帮助我一次如何选择多个帆布儿童
谢谢
Maheshwar
Hi
can anyone please help me on how to select multiple canvas children at a time
Thank you
Maheshwar
推荐答案
hi
问题是我在画布上有图像,我想在点击和拖动时选择图像...出于同样的目的,我使用了两个画布...
1.画布所在的画布(.png格式)
2.画布用于点击并拖动。
这里的代码是相同的
/////// ////////////////////////////////////////////////// ///////代码///////////////////////////
XAML;
< grid x:name =theGridxmlns:x =#unknown>
MouseDown = Grid_MouseDown
MouseUp =Grid_MouseUp
MouseMove =Grid_MouseMove
背景=透明
> ; <!---->
< canvas name =myCanvashorizontalalignment =Centerverticalalignment =Stretchallowdrop =Truedrop =DropImagemouserightbuttondown =myCanvas_MouseRightButtonDown_1 width =648margin =9,9,9,7grid.column =1grid.row =3>
<!---->
< image margin =2,0,0,10name =imgPhotomouseleftbuttondown =DragImageallowdrop =Truemouserightbuttondown =Image_MouseRightButtonDown_1>
Stretch =填充Grid.Row =1Grid.ColumnSpan =4Grid.Column =1Grid.RowSpan =2/>
< canvas.contextmenu>
< contextmenu foreground =White>
< menuitem header =删除click =Delete1>
< menuitem header =Clear Canvasclick =CLEAR_EVENT1>
< menuitem header =newclick =new_session>
< menuitem header =copycommand =ApplicationCommands.Copy>
< menuitem header =pastecommand =ApplicationCommands.Paste>
< /canvas.contextmenu>
<!---->
<!---->
< canvas>
<! - 这个画布覆盖在上一个画布上,用于
放置实现拖动选择框的矩形。 - >
< rectangle>
x:Name =selectionBox
可见性=折叠
Stroke =Black
StrokeThickness =1
/>
< / canvas>
<!---->
XAML.cs
bool mouseDown = false; //按住鼠标时设置为true。
Point mouseDownPos; //单击鼠标按钮的位置。
private void Grid_MouseDown(object sender,MouseButtonEventArgs e)
{
//捕获并跟踪鼠标。
mouseDown = true;
mouseDownPos = e.GetPosition(theGrid);
theGrid.CaptureMouse();
//拖动选择框的初始位置。
Canvas.SetLeft(selectionBox,mouseDownPos.X);
Canvas.SetTop(selectionBox,mouseDownPos.Y);
selectionBox.Width = 0;
selectionBox.Height = 0;
//使拖动选择框可见。
selectionBox .Visibility = Visibility.Visible;
myCanvas.Children.Contains(selectedElement);
}
private void Grid_MouseUp(object sender,MouseButtonEventArgs e)
{
//释放鼠标捕获并停止跟踪它。
mouseDown = false;
theGrid.ReleaseMouseCapture();
//隐藏拖动选择框。
selectionBox.Visibility = Visibility .Claplapsed;
Point mouseUpPos = e.GetPosition(theGrid);
}
private void Grid_MouseMove(object sender,System.Windows.Input.MouseEventArgs e)
{
if(mouseDown)
{
//按住鼠标时,重新定位拖动选择框。
Point mousePos = e.GetPosition(theGrid);
if(mouseDownPos.X< mousePos.X)
{
Canvas.SetLeft(selectionBox,mouseDownPos.X);
selectionBox.Width = mousePos.X - mouseDownPos。 X;
}
其他
{
Canvas.SetLeft(selectionBox,mousePos.X);
selectionBox.Width = mouseDownPos.X - mousePos.X;
}
if(mouseDownPos.Y< mousePos.Y)
{
Canvas.SetTop(selectionBox,mouseDownPos.Y);
selectionBox.Height = mousePos.Y - mouseDownPos。 Y;
}
其他
{
Canvas.SetTop(selectionBox,mousePos.Y);
selectionBox.Height = mouseDownPos.Y - mousePos.Y;
}
myCanvas.Children.Contains(selectedElement);
if(selected)
{
selected = false;
if(selectedElement!= null)
{
/ /从所选元素中删除装饰器
//<<我已经在添加删除按钮后证明了这一点,因为adorner layer issue>>
aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
selectedElement = null;
}
}
if(e.Source!= myCanvas)
{
_isDown = true;
_startPoint = e.GetPosition(myCan vas);
selectedElement = e.Source as UIElement;
_originalLeft = Canvas.GetLeft(selectedElement );
_originalTop = Canvas.GetTop(selectedElement);
aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
aLayer.Add(new ResizingAdorner(selectedElement));
selected = true;
e.Handled = true;
}
}
}
hi The problem is i have a images on the canvas and i want to select the images on click and drag... for the same purpose i have used two canvas...
1. the canvas on which images are there (.png format)
2. the canvas which is used for click and drag.
here i s the code for the same
////////////////////////////////////////////////////////////////Code///////////////////////////
XAML;
<grid x:name="theGrid" xmlns:x="#unknown">
MouseDown="Grid_MouseDown"
MouseUp="Grid_MouseUp"
MouseMove="Grid_MouseMove"
Background="Transparent"
> <!---->
<canvas name="myCanvas" horizontalalignment="Center" verticalalignment="Stretch" allowdrop="True" drop="DropImage" mouserightbuttondown="myCanvas_MouseRightButtonDown_1" width="648" margin="9,9,9,7" grid.column="1" grid.row="3">
<!---->
<image margin="2,0,0,10" name="imgPhoto" mouseleftbuttondown="DragImage" allowdrop="True" mouserightbuttondown="Image_MouseRightButtonDown_1">
Stretch="Fill" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="1" Grid.RowSpan="2" />
<canvas.contextmenu>
<contextmenu foreground="White">
<menuitem header="Delete" click="Delete1">
<menuitem header="Clear Canvas" click="CLEAR_EVENT1">
<menuitem header="new" click="new_session">
<menuitem header="copy" command="ApplicationCommands.Copy">
<menuitem header="paste" command="ApplicationCommands.Paste">
</canvas.contextmenu>
<!---->
<!---->
<canvas>
<!-- This canvas is overlaid over the previous canvas and is used to
place the rectangle that implements the drag selection box. -->
<rectangle>
x:Name="selectionBox"
Visibility="Collapsed"
Stroke="Black"
StrokeThickness="1"
/>
</canvas>
<!---->
XAML.cs
bool mouseDown = false; // Set to 'true' when mouse is held down.
Point mouseDownPos; // The point where the mouse button was clicked down.
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Capture and track the mouse.
mouseDown = true;
mouseDownPos = e.GetPosition(theGrid);
theGrid.CaptureMouse();
// Initial placement of the drag selection box.
Canvas.SetLeft(selectionBox, mouseDownPos.X);
Canvas.SetTop(selectionBox, mouseDownPos.Y);
selectionBox.Width = 0;
selectionBox.Height = 0;
// Make the drag selection box visible.
selectionBox.Visibility = Visibility.Visible;
myCanvas.Children.Contains(selectedElement);
}
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
// Release the mouse capture and stop tracking it.
mouseDown = false;
theGrid.ReleaseMouseCapture();
// Hide the drag selection box.
selectionBox.Visibility = Visibility.Collapsed;
Point mouseUpPos = e.GetPosition(theGrid);
}
private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (mouseDown)
{
// When the mouse is held down, reposition the drag selection box.
Point mousePos = e.GetPosition(theGrid);
if (mouseDownPos.X < mousePos.X)
{
Canvas.SetLeft(selectionBox, mouseDownPos.X);
selectionBox.Width = mousePos.X - mouseDownPos.X;
}
else
{
Canvas.SetLeft(selectionBox, mousePos.X);
selectionBox.Width = mouseDownPos.X - mousePos.X;
}
if (mouseDownPos.Y < mousePos.Y)
{
Canvas.SetTop(selectionBox, mouseDownPos.Y);
selectionBox.Height = mousePos.Y - mouseDownPos.Y;
}
else
{
Canvas.SetTop(selectionBox, mousePos.Y);
selectionBox.Height = mouseDownPos.Y - mousePos.Y;
}
myCanvas.Children.Contains(selectedElement);
if (selected)
{
selected = false;
if (selectedElement != null)
{
// Remove the adorner from the selected element
//<<i have justifed it after adding the delete button becasue of adorner layer issue>>
aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
selectedElement = null;
}
}
if (e.Source != myCanvas)
{
_isDown = true;
_startPoint = e.GetPosition(myCanvas);
selectedElement = e.Source as UIElement;
_originalLeft = Canvas.GetLeft(selectedElement);
_originalTop = Canvas.GetTop(selectedElement);
aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
aLayer.Add(new ResizingAdorner(selectedElement));
selected = true;
e.Handled = true;
}
}
}
这篇关于C#wpf中的画布处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文