private void Cell_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e) { // ShowDialog status panel Rectangle cell = (Rectangle)e.Source; int index0 = int.Parse(cell.Name.Split('_')[1]); int index1 = int.Parse(cell.Name.Split('_')[2]); //MessageBox.Show(cell.Name); PresentationSource source = PresentationSource.FromVisual(this); double dpiX = 0; double dpiY = 0; if (source?.CompositionTarget != null) { dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11; dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22; } var mousePosition = MainCanvas.PointToScreen(e.GetPosition(MainCanvas)); StatusWindow statusWindow = new StatusWindow(_status, cell, false) { Left = mousePosition.X * 96.0 / dpiX, Top = mousePosition.Y * 96.0 / dpiY }; statusWindow.ShowDialog(); _map[index0, index1] = _status.Find(((SolidColorBrush)cell.Fill).Color); }
/// <summary> /// Отобразить данные на MainCanvas /// </summary> /// <param name="data"></param> /// <param name="layerNumber"></param> private void DrawData(TimeMeasureData data, int layerNumber) { double?pos1 = core.WaveformUserControl.GetYPos(data.Variable1); double?pos2 = core.WaveformUserControl.GetYPos(data.Variable2); if ((pos1 != null) && (pos2 != null)) { Point p1 = new Point(core.ScaleManager.GetOffset(data.TimeStart), 80 - layerNumber * LayerHeight); Point p2 = new Point(core.ScaleManager.GetOffset(data.TimeEnd), 80 - layerNumber * LayerHeight); if (Math.Abs(p1.X - p2.X) < 16.0d) { return; } Arrow arrow = new Arrow(); arrow.X1 = p1.X; arrow.Y1 = p1.Y; arrow.X2 = p2.X; arrow.Y2 = p2.Y; arrow.HeadWidth = 10; arrow.HeadHeight = 5; arrow.StrokeThickness = 1; arrow.Stroke = Brushes.Gray; arrow.ContextMenu = Resources["ContextMenuArrow"] as ContextMenu; arrow.Tag = data; arrow.MouseEnter += new MouseEventHandler(element_MouseEnter); arrow.MouseLeave += new MouseEventHandler(element_MouseLeave); Point center = new Point((p1.X + p2.X) / 2.0, (p1.Y + p2.Y) / 2.0 - 10.0); TextBlock textBlock = new TextBlock(); textBlock.Background = Brushes.White; textBlock.Text = TimeInterval.ToString(data.TimeEnd - data.TimeStart); textBlock.FontSize = 10; textBlock.Width = 50; textBlock.Height = 10; textBlock.TextAlignment = TextAlignment.Center; textBlock.TextWrapping = TextWrapping.Wrap; textBlock.HorizontalAlignment = HorizontalAlignment.Stretch; textBlock.VerticalAlignment = VerticalAlignment.Stretch; textBlock.ContextMenu = Resources["ContextMenuArrow"] as ContextMenu; textBlock.Tag = data; textBlock.MouseEnter += new MouseEventHandler(element_MouseEnter); textBlock.MouseLeave += new MouseEventHandler(element_MouseLeave); MainCanvas.Children.Add(textBlock); Canvas.SetLeft(textBlock, center.X - textBlock.Width / 2.0); Canvas.SetTop(textBlock, center.Y); MainCanvas.Children.Add(arrow); Point pt1 = MainCanvas.PointToScreen(p1); Point pt2 = MainCanvas.PointToScreen(p2); Line line1 = new Line(); line1.X1 = pt1.X; line1.X2 = pt1.X; line1.Y1 = pt1.Y; line1.Y2 = pos1.Value; Line line2 = new Line(); line2.X1 = pt2.X; line2.X2 = pt2.X; line2.Y1 = pt2.Y; line2.Y2 = pos2.Value; lines.Add(line1); lines.Add(line2); } }
private void MainWindow_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { AnnotationBox?.Open(MainCanvas.PointToScreen(e.GetPosition(MainCanvas)), MainCanvas); }
private void MainCanvas_MouseDown(object sender, MouseButtonEventArgs e) { if (e.RightButton == MouseButtonState.Pressed) { return; } if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { IsMouseDown = true; initial = Mouse.GetPosition(this); return; } if (TypeDraw == TypeShape.Fill || TypeDraw == TypeShape.Eraser) { return; } if (controlSelector.itemChoose != null && TypeDraw != TypeShape.PolyGon) { controlSelector.OnMouse_Down(); return; } if (TypeDraw == TypeShape.PolyGon) { if (controlSelector.itemChoose != null) { if (e.ClickCount == 2) { TypeDraw = TypeShape.None; controlSelector.itemChoose.Selected = false; controlSelector.itemChoose = null; SaveHistory(); for (int i = 0; i < MainCanvas.Children.Count; ++i) { if (!(MainCanvas.Children[i] is ItemLayer)) { continue; } ItemLayer item = (ItemLayer)MainCanvas.Children[i]; MainCanvas.Children.Remove((ItemLayer)MainCanvas.Children[i]); } return; } // Mouse.GetPosition(MainCanvas); Point CurPoint = Mouse.GetPosition(MainCanvas); CurPoint = MainCanvas.PointToScreen(CurPoint); CurPoint = ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.PointFromScreen(CurPoint); ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.Points.Add(CurPoint); return; } } if (TypeDraw == TypeShape.None) { if (controlSelector.itemChoose != null) { if (e.ClickCount == 2 && controlSelector.itemChoose.Type == TypeShape.Text) { MessageBox.Show("Show tab chỉnh sửa text"); /*Tạo 1 tab mới để chỉnh sửa*/ ItemLayer itemLayer = controlSelector.itemChoose; itemLayer.RectText = "Set lại dòng text"; itemLayer.RectFontSize = 30; itemLayer.ColorStroke = Color.FromRgb(255, 255, 0); itemLayer.RectFontStyle = FontStyles.Italic; itemLayer.RectFontStretch = FontStretches.ExtraExpanded; return; } } controlSelector.OnMouse_Down(); } else if (TypeDraw != TypeShape.Text) { Point CurPoint = Mouse.GetPosition(MainCanvas); ItemLayer item = new ItemLayer(TypeDraw); // TypeDraw loại hình Canvas.SetLeft(item, CurPoint.X); Canvas.SetTop(item, CurPoint.Y); item.Width = 1; item.Height = 1; item.ColorStroke = outlineColor; // tùy chỉnh màu vẽ item.ColorFill = fillColor; // Tùy chỉnh màu fill, không fill thì = new Color(); item.PenWidth = penWidth; // Tùy chỉnh Độ rộng item.MainCanvas_MouseDown += MainCanvas_MouseDown; item.MainCanvas_MouseUp += MainCanvas_MouseUp; item.MainCanvas_MouseMove += MainCanvas_MouseMove; item.StyleLine = styleLine; item.Create(); MainCanvas.Children.Add(item); //history.PushBack(item); controlSelector.SetItemCreate(item); } else { Point CurPoint = Mouse.GetPosition(MainCanvas); ItemLayer item = new ItemLayer(TypeShape.Text); // TypeDraw loại hình Canvas.SetLeft(item, CurPoint.X); Canvas.SetTop(item, CurPoint.Y); item.Width = 1; item.Height = 1; item.MainCanvas_MouseDown += MainCanvas_MouseDown; item.MainCanvas_MouseUp += MainCanvas_MouseUp; item.MainCanvas_MouseMove += MainCanvas_MouseMove; if (textToolsGroup != null) { textToolsGroup.Visibility = Visibility.Visible; } if (MainRibbon != null) { MainRibbon.SelectedTabIndex = 0; } item.Create(); /*Sử dụng sau hàm Create*/ item.RectFontFamily = fontFamilyText; item.RectText = "Helllo31387126378216hfdjisdhfkjsdhkfjdshf"; item.RectFontSize = fontSizeText; item.ColorStroke = textColor; // tùy chỉnh màu vẽ MainCanvas.Children.Add(item); //history.PushBack(item); controlSelector.SetItemCreate(item); } }
private void MainCanvas_MouseMove(object sender, MouseEventArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { if (!IsMouseDown) { return; } ///MainCanvainitial = Mouse.GetPosition(this); //xOffset = //Mouse.GetPosition(this).X - initial.X; //yOffset = //Mouse.GetPosition(this).Y - initial.Y; //MainCanvas.RenderTransform = Translate = new TranslateTransform { X = xOffset + Mouse.GetPosition(this).X - initial.X, Y = yOffset + Mouse.GetPosition(this).Y - initial.Y }; TransformGroup group = new TransformGroup(); group.Children.Add(Translate); group.Children.Add(Transform); MainCanvas.RenderTransform = group; return; } if (isEraser == true && TypeDraw == TypeShape.Eraser) { Point point = Mouse.GetPosition(MainCanvas); BitmapSource bmp = (BitmapSource)MainCanvasImage.Source; if (bmp == null) { return; } MyBitmap = new WriteableBitmap(bmp); MainCanvasImage.Source = MyBitmap; ImageProcess.Rubber((int)point.X, (int)point.Y, penWidth, MyBitmap); } if (TypeDraw == TypeShape.PolyLine) { if (controlSelector.itemChoose != null) { // Mouse.GetPosition(MainCanvas); Point CurPoint = Mouse.GetPosition(MainCanvas); CurPoint = MainCanvas.PointToScreen(CurPoint); CurPoint = ((PolyLineObject)controlSelector.itemChoose.objectBase).rect.PointFromScreen(CurPoint); ((PolyLineObject)controlSelector.itemChoose.objectBase).rect.Points.Add(CurPoint); } return; } if (TypeDraw == TypeShape.PolyGon) { if (controlSelector.itemChoose != null) { // Mouse.GetPosition(MainCanvas); Point CurPoint = Mouse.GetPosition(MainCanvas); CurPoint = MainCanvas.PointToScreen(CurPoint); CurPoint = ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.PointFromScreen(CurPoint); PointCollection list = ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.Points; ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.Points[list.Count - 1] = CurPoint; } } controlSelector.OnMouse_Move(); //Set thông tin statusbar Point p = Mouse.GetPosition(MainCanvas); lblCursorPosition.Text = "X: " + ((int)p.X).ToString() + ", Y: " + ((int)p.Y).ToString(); }