Autor Beitrag
csharp2014
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70
Erhaltene Danke: 3


Delphi 11 Pro, C++ (VS 2019)
BeitragVerfasst: Mi 28.06.17 17:38 
Hallo,

habe ein Window mit einem Canvas. In folgendem Code kommt der MouseDown Event nicht:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
private void Canvas1_MouseDown(Object sender, System.Windows.Input.MouseButtonEventArgs e)
{     
  MessageBox.Show("Hallo");
}

private void Canvas1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{          
  IInputElement displayArea = null;
  
  Point pt = e.GetPosition(displayArea);            
                                                    
  if (e.LeftButton != MouseButtonState.Pressed)
  {
    for (int i=0; i<Canvas1.Children.Count; i++)
    {
      if ((Canvas1.Children[i].GetType() == typeof(Rectangle))&&(((Rectangle)Canvas1.Children[i]).Name == "Rectangle"))
      {
        Canvas1.Children.RemoveAt(i);   
        break;
      }
    }      
    
    Rect re1=new Rect(pt.X, pt.Y, 3030); 
                
    Rectangle myRect = new Rectangle();

    myRect.Fill = Brushes.AliceBlue;
    myRect.Stroke = Brushes.Bisque;

    myRect.StrokeThickness = 1;

    myRect.Width = re1.Right-re1.Left;
    myRect.Height = re1.Bottom-re1.Top;

    myRect.Name = "Rectangle";

    Canvas.SetLeft(myRect, re1.Left);
    Canvas.SetTop(myRect, re1.Top);

    Canvas1.Children.Add(myRect);
    
    //

    MainForm.Title=pt.X.ToString()+", "+pt.Y.ToString();
  }
}

private void Canvas1_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

}


Beste Grüße

Moderiert von user profile iconChristian S.: B- durch C#-Tags ersetzt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 28.06.17 17:47 
Wie sieht das XAML dazu aus? Ist das Event dort korrekt verdrahtet?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
csharp2014 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70
Erhaltene Danke: 3


Delphi 11 Pro, C++ (VS 2019)
BeitragVerfasst: Mi 28.06.17 18:05 
Xaml:
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
<Window x:Name="MainForm" x:Class="WpfCALMain.CALMain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfCALMain"
        Title="CALMain" Height="350" Width="525">
  <Canvas Name="Canvas1" Background="White" MouseMove="Canvas1_MouseMove" MouseUp="Canvas1_MouseUp" MouseDown="Canvas1_MouseDown">
    <!--<Button x:Name="Button1" Content="Show Dialog" Canvas.Top="285" Width="116" Height="25" Click="Button1_Click" Canvas.Left="10"/>-->
    <!--<Button x:Name="Button2" Content="Clear Canvas" Canvas.Top="285" Width="116" Height="25" Click="Button2_Click" Canvas.Left="131"/>-->
  </Canvas>
</Window>


Moderiert von user profile iconTh69: XML-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 28.06.17 19:33 
Hallo,

verwende mal eine Background-Color ungleich "White" (=default), s.a. How to make the WPF Canvas mouse click event work?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 28.06.17 19:49 
Also, das Problem konnte ich im wahrsten Sinne des Wortes teilweise nachvollziehen, es trat bei mir bei jedem zweiten Klick auf. Also bei jedem zweiten Klick wird das Event gefeuert. (Allerdings ist das XAML bei mir nicht kompilierbar, in der Canvas muss es x:Name heißen).

Mir ist dann aufgefallen, dass Deinen Move-Methode nicht wirklich optimal ist, weil sie einen Haufen Rectangles erzeugt und wegwirft, obwohl Du nur eins brauchst. Wenn Du die Move-Methode durch unten stehende ersetzt, funktioniert merkwürdigerweise auch das MouseDown. Ich habe aber nicht wirklich einen Plan, warum.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
        private void Canvas1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            IInputElement displayArea = null;

            Point pt = e.GetPosition(displayArea);

            if (e.LeftButton != MouseButtonState.Pressed)
            {
                Rect re1 = new Rect(pt.X, pt.Y, 3030);

                var myRect = Canvas1.Children.OfType<Rectangle>().FirstOrDefault(r => r.Name == "Rectangle");
                if (myRect == null)
                {                    
                    myRect = new Rectangle();

                    myRect.Fill = Brushes.AliceBlue;
                    myRect.Stroke = Brushes.Bisque;

                    myRect.StrokeThickness = 1;

                    myRect.Width = re1.Right - re1.Left;
                    myRect.Height = re1.Bottom - re1.Top;

                    myRect.Name = "Rectangle";

                    Canvas1.Children.Add(myRect);

                }

                Canvas.SetLeft(myRect, re1.Left);
                Canvas.SetTop(myRect, re1.Top);

                MainForm.Title = pt.X.ToString() + ", " + pt.Y.ToString();
            }
        }

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
csharp2014 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70
Erhaltene Danke: 3


Delphi 11 Pro, C++ (VS 2019)
BeitragVerfasst: Mi 28.06.17 22:32 
Danke, hat soweit funktioniert.