Silverlight系列(右键菜单、控件移动的实现)
2010-03-10 14:03
555 查看
1.右键菜单
第一步在html页添加如下代码:<param name="windowless" value="true" />
第二步在xaml页添加右键菜单呈现的内容:
<Border x:Name="brrightMenu" BorderBrush="Black" BorderThickness="2" Background="Green" Width="100" HorizontalAlignment="Left" Visibility="Collapsed"> <StackPanel> <HyperlinkButton x:Name="btnAddElement" Foreground="Black" Content="添加新图片元素" Margin="5" Click="btnAddElement_Click" ></HyperlinkButton> <HyperlinkButton x:Name="btnAddTextElement" Foreground="Black" Content="添加新文字元素" Margin="5" Click="btnAddTextElement_Click" ></HyperlinkButton> </StackPanel> <!--右键菜单的内容--> <!--右键菜单的位置--> <Border.RenderTransform> <TranslateTransform x:Name="tt" /> </Border.RenderTransform> </Border>
第三步cs页面添加如下代码:
public partial class MainPage : UserControl { ContextMenu menu = null; public MainPage() { InitializeComponent(); menu = new ContextMenu(new RightElement(tt, brrightMenu, 255, 12, 0, 0)); }
ContextMenu.cs:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Browser; namespace DragElement { /// <summary> /// 鼠标右键菜单实现 /// </summary> public class ContextMenu { RightElement Element; public ContextMenu(RightElement element) { Element = element; HtmlPage.Document.AttachEvent("oncontextmenu", this.onContextMenu); } private void onContextMenu(object sender, HtmlEventArgs e) { if (Math.Abs(Element.RX) <= 40 && Math.Abs(Element.RY) <= 10) { Element.BD.Visibility = Visibility.Visible; //Element.TT.X = e.OffsetX - Element.X; //Element.TT.Y = e.OffsetY - Element.Y; Element.TT.X = 350; Element.TT.Y = 205; e.PreventDefault(); //阻止默认DOM } } } }
运行效果:
2.控件移动的实现
DragElement.xaml:<Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="Auto" Height="Auto" ShowGridLines="True" VerticalAlignment="Top" Margin="5,5,5,5" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown" MouseMove="LayoutRoot_MouseMove"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="250" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <layoutToolkit:Accordion Grid.Column="0" Width="240" Background="LightGreen" FontSize="14"> <layoutToolkit:AccordionItem Header="选项"> <ContentControl> <StackPanel> <HyperlinkButton x:Name="ResumeLayout" Foreground="Green" Content="还原设计器元素布局" Margin="5" Click="ResumeLayout_Click" Width="220"></HyperlinkButton> <HyperlinkButton x:Name="UpdateLayout" Foreground="Green" Content="更改设计器元素布局" Margin="5" Click="UpdateLayout_Click" Width="220"></HyperlinkButton> <HyperlinkButton x:Name="FixedLayout" Foreground="Green" Content="固定设计器元素布局" Margin="5" Click="FixedLayout_Click" Width="220"></HyperlinkButton> <HyperlinkButton x:Name="SaveLayout" Foreground="Green" Content="保存设计器元素布局" Margin="5" Click="SaveLayout_Click" Width="220"></HyperlinkButton> <HyperlinkButton x:Name="btnFullScreen" Foreground="Green" Content="全屏" Margin="5" Click="btnFullScreen_Click" Width="220"></HyperlinkButton> </StackPanel> </ContentControl> </layoutToolkit:AccordionItem> <layoutToolkit:AccordionItem Content="设置" Header="设置" > </layoutToolkit:AccordionItem> </layoutToolkit:Accordion> <Canvas Background="LightCyan" x:Name="canvas" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1000" Height="750" Grid.Row="0" Grid.Column="1" > <TextBlock x:Name="tb1" Foreground="Green" Width="Auto" Height="25" Canvas.Top="30" ></TextBlock> <TextBlock x:Name="tb2" Foreground="Red" Width="Auto" Height="25" ></TextBlock> <StackPanel> <Border x:Name="brrightMenu" BorderBrush="Black" BorderThickness="2" Background="Green" Width="100" HorizontalAlignment="Left" Visibility="Collapsed"> <StackPanel> <HyperlinkButton x:Name="btnAddElement" Foreground="Black" Content="添加新图片元素" Margin="5" Click="btnAddElement_Click" ></HyperlinkButton> <HyperlinkButton x:Name="btnAddTextElement" Foreground="Black" Content="添加新文字元素" Margin="5" Click="btnAddTextElement_Click" ></HyperlinkButton> </StackPanel> <!--右键菜单的内容--> <!--右键菜单的位置--> <Border.RenderTransform> <TranslateTransform x:Name="tt" /> </Border.RenderTransform> </Border> </StackPanel> </Canvas> </Grid> <!-- 为 Silverlight 插件配置参数,windowless="true" -->
DragElement.cs:
bool trackingMouseMove = false; Point mousePostion; private void UpdateLayout_Click(object sender, RoutedEventArgs e) { foreach (UIElement ui in canvas.Children) { StackPanel mysp = ui as StackPanel; if (mysp != null) { AddHandles(mysp); } } } /// <summary> /// 鼠标移动实现 /// </summary> private void AddHandles(StackPanel myPanel) { myPanel.MouseLeftButtonDown += new MouseButtonEventHandler(MouseDown); myPanel.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUp); myPanel.MouseMove += new MouseEventHandler(MouseMove); } private void MouseDown(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; mousePostion = e.GetPosition(null); trackingMouseMove = true; if (null != element) { element.CaptureMouse();//将鼠标捕获设置为元素 element.Cursor = Cursors.Hand; } } private void MouseUp(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; trackingMouseMove = false; element.ReleaseMouseCapture();//移除鼠标捕获 element.Cursor = null; } private void MouseMove(object sender, MouseEventArgs e) { FrameworkElement element = sender as FrameworkElement; if (trackingMouseMove) { double pointY = e.GetPosition(null).Y - mousePostion.Y; double pointX = e.GetPosition(null).X - mousePostion.X; double newPointY = pointY + (double)element.GetValue(Canvas.TopProperty); double newPointX = pointX + (double)element.GetValue(Canvas.LeftProperty); if (newPointX < 0 || newPointY < 0 || newPointX > 1000 || newPointY > 750) { //限定鼠标移动范围 return; } element.SetValue(Canvas.TopProperty, newPointY); element.SetValue(Canvas.LeftProperty, newPointX); mousePostion = e.GetPosition(null); tb1.Text = "当前相对位置X:" + newPointX + " Y:" + newPointY; ResumeLine(sender as StackPanel); } }
Operate.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Browser; using System.Windows.Media.Imaging; namespace DragElement { public class OperateElement { public Canvas canvas { get; set; } public void btnDelElement_Click(object sender, RoutedEventArgs e) { HtmlWindow html = HtmlPage.Window; if (html.Confirm("确认要删除吗?")) { HyperlinkButton button = sender as HyperlinkButton; StackPanel sp = button.Parent as StackPanel; ResumeLine(sp); canvas.Children.Remove(sp);//删除panel } } private void ResumeLine(StackPanel sp) { List<Path> lp = new List<Path>(); foreach (UIElement ui in canvas.Children) { Path path = ui as Path; if (path != null) { var name = path.Name; foreach (UIElement ui2 in sp.Children) { HyperlinkButton hb = ui2 as HyperlinkButton; if (hb != null && !string.IsNullOrEmpty(hb.Name)) { if (name.Contains(hb.Name)) { lp.Add(path); } } } } } for (int i = 0; i < lp.Count; i++) { canvas.Children.Remove(lp[i]); } } } }
运行效果:
相关文章推荐
- Silverlight系列(右键菜单、控件移动的实现)
- 鼠标右键弹出菜单 上传图片自定义控件 弹出菜单(PopUp 控件) 2 实现数据库插入操作 文件名通过参数传递
- Silverlight实用窍门系列:4.Silverlight 4.0添加鼠标右键菜单和Silverlight全屏模式的进入退出。【附带源码实例】
- win32 listctrl控件右键菜单的实现
- Silverlight 4的是鼠标右键菜单的实现(一)
- Silverlight 4的是鼠标右键菜单的实现(三)
- silverlight实现多级右键菜单
- C#实现Silverlight中右键菜单
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单
- 在Silverlight里实现控件上下移动
- silverlight 制作可拖动控件,并且在控件上添加右键菜单
- Dev系列控件之XtraChart高级应用(模拟flash鼠标移动,点击实现相关信息提示)
- 自定义treeview控件,实现右键菜单编辑功能
- Silverlight实用窍门系列:12.继承于某些固定控件(以Grid为例)的鼠标左键双击事件的实现【附带实例源码】
- Silverlight实用窍门系列:13.基于Popup浮动窗体控件模拟ToolTip的实现【附带实例源码】
- Silverlight 4的是鼠标右键菜单的实现(二)
- Silverlight实用窍门系列:4.Silverlight 4.0添加鼠标右键菜单和Silverlight全屏模式的进入退出。【附带源码实例】
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
- TreeList控件右键菜单功能的实现
- 在控件上右键菜单的实现