Animation um Window zu vergrößern (Sidebar)

  • WPF

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von miko1204.

    Animation um Window zu vergrößern (Sidebar)

    Hallo Community,

    mein Code ist wie folgt:

    C#-Quellcode

    1. public void MaximizeWindow()
    2. {
    3. Size targetSize = GetTargetSize(true);
    4. Point targetPosition = GetTargetPosition(true);
    5. //Animate window size
    6. DoubleAnimation animHeight = new DoubleAnimation(this.Height, targetSize.Height, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    7. DoubleAnimation animWidth = new DoubleAnimation(this.Width, targetSize.Width, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    8. //Animate window location
    9. DoubleAnimation animX = new DoubleAnimation(this.Left, targetPosition.X, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    10. DoubleAnimation animY = new DoubleAnimation(this.Top, targetPosition.Y, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    11. //Toggle arrow
    12. arrowButton.ArrowDirection = GetTargetArrowDirection(true);
    13. //Animate
    14. this.BeginAnimation(Window.LeftProperty, animX);
    15. this.BeginAnimation(Window.TopProperty, animY);
    16. this.BeginAnimation(Window.HeightProperty, animHeight);
    17. this.BeginAnimation(Window.WidthProperty, animWidth);
    18. //Status
    19. state = WindowState.Maximized;
    20. }
    21. public void MinimizeWindow()
    22. {
    23. Size targetSize = GetTargetSize(false);
    24. Point targetPosition = GetTargetPosition(false);
    25. //Animate window size
    26. DoubleAnimation animHeight = new DoubleAnimation(this.Height, targetSize.Height, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    27. DoubleAnimation animWidth = new DoubleAnimation(this.Width, targetSize.Width, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    28. //Animate window location
    29. DoubleAnimation animX = new DoubleAnimation(this.Left, targetPosition.X, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    30. DoubleAnimation animY = new DoubleAnimation(this.Top, targetPosition.Y, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.HoldEnd);
    31. //Toggle arrow
    32. arrowButton.ArrowDirection = GetTargetArrowDirection(false);
    33. //Animate
    34. this.BeginAnimation(Window.LeftProperty, animX);
    35. this.BeginAnimation(Window.TopProperty, animY);
    36. this.BeginAnimation(Window.HeightProperty, animHeight);
    37. this.BeginAnimation(Window.WidthProperty, animWidth);
    38. //Status
    39. state = WindowState.Minimized;
    40. }
    41. public Point GetTargetPosition(bool maximized)
    42. {
    43. if (maximized)
    44. {
    45. double differenceWidth = GetTargetSize(true).Width - GetTargetSize(false).Width;
    46. return new Point(this.Left - differenceWidth, 0);
    47. }
    48. else
    49. {
    50. double differenceWidth = GetTargetSize(true).Width - GetTargetSize(false).Width;
    51. return new Point(this.Left + differenceWidth, 0);
    52. }
    53. }
    54. public Size GetTargetSize(bool maximized)
    55. {
    56. if (maximized)
    57. {
    58. //Later calculate with col/row
    59. return new Size(250, 250);
    60. }
    61. else
    62. {
    63. return new Size(130, 15);
    64. }
    65. }


    1. Ich habe versucht den Code so zu gestalten, dass der User die Form auch frei entlang der oberen Bildschirmkante bewegen kann.

    2. Der Code soll eine 130x15 Form oben an der Bildschirmkante anzeigen. Bei einem Button Click wird die Methode MaximizeWindow() aufgerufen. Diese soll das Fenster auf die Maße 250x250 vergrößern. Und zwar animiert, damit das ganze auch schön aussieht. Die Animation der Y Achse ist unnötig, ich weiß aber da ich später noch die anderen Bilschirmkanten implementieren will ist das durch aus sinnvoll (ist ja auch egal jetzt). Jedenfalls soll die Form sich "entfalten". Heißt: In gleicher Zeit nach rechts und links ausdehnen, sowie auch nach unten. Das passiert auch aber alles überhaupt nicht gleichzeitig und die alte Position geht hierbei fast immer verloren bzw. werden manche nicht verändert.


    Jemand von euch ne Idee? :thumbsup:

    LG
    Julian


    EDIT: Also der Button zum umschalten befindet sich unten auf der Form. Sowohl in zusammengeklapptem und auseinandergeklapptem Zustand.
    Hmkay. :|

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „jedijaeger“ ()

    hi

    sorry wenn meine Antwort voll daneben ist aber wenn ich dich richtig verstanden habe moechtest du einfach die From animieren von Groesse 130x15 zu 250x250

    also in vb Sprache (kann leider kein C#, aber sollte fuer dich schon klar sein) kannst es so mit einem Timer machen:

    Quellcode

    1. Private size1, size2 As vector
    2. Private out As Boolean
    3. Private speed As Int16 = 8
    4. Private Sub Form1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    5. If Me.Width = size1.w And Me.Height = size1.h Then
    6. out = True
    7. Else
    8. out = False
    9. End If
    10. Timer1.Start()
    11. End Sub
    12. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    13. Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    14. size1.w = 130
    15. size1.h = 15
    16. size2.w = 250
    17. size2.h = 250
    18. Me.Width = size1.w
    19. Me.Height = size1.h
    20. End Sub
    21. Private Structure vector
    22. Public w As Double
    23. Public h As Double
    24. End Structure
    25. Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    26. Select Case out
    27. Case True
    28. If Me.Width < size2.w Then Me.Width += speed
    29. If Me.Height < size2.h Then Me.Height += speed
    30. If Me.Width >= size2.w And Me.Height >= size2.h Then
    31. Me.Width = size2.w
    32. Me.Height = size2.h
    33. Timer1.Stop()
    34. End If
    35. Case False
    36. If Me.Width > size1.w Then Me.Width -= speed
    37. If Me.Height > size1.h Then Me.Height -= speed
    38. If Me.Width <= size1.w And Me.Height <= size1.h Then
    39. Me.Width = size1.w
    40. Me.Height = size1.h
    41. Timer1.Stop()
    42. End If
    43. End Select
    44. End Sub
    Schäm dich nicht "Zu fragen", schäm dich "Nicht zu wissen". ?(