Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] Logischer Fehler bei Locationberechnung

Empfohlene Antworten

Veröffentlicht

Hi,

wiedereinmal benötige ich eine helfende Hand.

Um mein Problem zu verstehen hole ich mal etwas weiter aus:

Ich habe mir ein Usercontrol geschrieben welches dem zugehörigen ParentForm einen etwas flexibleren look verpasst. Das heißt die Controlbox wird ausgetauscht und demnächst wird noch ein Border hinzukommen. Der Formborderstyle des ParentForms wird dafür auf None gesetzt.

Das Funktioniert soweit so gut und sieht

aus.

Das hat zurfolge das ich das Form nicht mehr über das SizeGrip vergößern kann (da dieses effektiv nichtmehr vorhanden sind).

Daher musste eine eigene Lösung her.

Die Lösung wollte ich direkt im UserControl verankern. Wenn ich es rechts und unten ziehe funktioniert es bereits so wie es soll.

Hier mal der Code (ist in einem test usercontrol):



public partial class testctr : UserControl

    {

        public enum SizeGripRegion { LeftUpperCorner, RightUpperCorner, Top, Left, Right, Bottom, LeftBottomCorner, RightButtomCorner, None };

        private SizeGripRegion sizegrip = SizeGripRegion.None;

        private Point LastLocation = Point.Empty;

        private bool resizeparentform = true;

        private bool isClicked = false;

        public testctr()

        {

            InitializeComponent();

            this.MouseMove += new MouseEventHandler(Mouse_Move);

            this.MouseDown += new MouseEventHandler(Mouse_Down);

            this.MouseUp += new MouseEventHandler(Mouse_Up);

            this.SizeChanged +=new EventHandler(Resize_Ev);

        }

        #region events

        private void Resize_Ev(object sender, EventArgs e) {

            if (resizeparentform && base.ParentForm != null)

            {

                if (this.Size.Width < base.ParentForm.Size.Width || this.Size.Height < base.ParentForm.Size.Height) {

                    base.ParentForm.Size = this.Size;

                }

            }

        }

        private void Mouse_Move(object sender, MouseEventArgs e) {

            if (!isClicked)

            {

                if ((e.Location.X == 0 || e.Location.X <= 5) && (e.Location.Y == 1 || e.Location.Y <= 3))

                {

                    Cursor.Current = Cursors.SizeNWSE;

                    sizegrip = SizeGripRegion.LeftUpperCorner;

                }

                else if ((e.Location.X == base.ClientSize.Width || e.Location.X >= (base.ClientSize.Width - 5)) && (e.Location.Y > 0 && e.Location.Y <= 3))

                {

                    Cursor.Current = Cursors.SizeNESW;

                    sizegrip = SizeGripRegion.RightUpperCorner;

                }

                else if ((e.Location.X >= 5 && e.Location.X <= (base.ClientSize.Width - 5)) && (e.Location.Y == 0 || e.Location.Y <= 3))

                {

                    Cursor.Current = Cursors.SizeNS;

                    sizegrip = SizeGripRegion.Top;


                }

                else if ((e.Location.X == 0 || e.Location.X <= 5) && (e.Location.Y >= 5 && e.Location.Y <= (base.ClientSize.Height - 5)))

                {

                    Cursor.Current = Cursors.SizeWE;

                    sizegrip = SizeGripRegion.Left;

                }

                else if ((e.Location.X == 0 || e.Location.X <= 5) && (e.Location.Y == ClientSize.Height || e.Location.Y >= (ClientSize.Height - 5)))

                {

                    Cursor.Current = Cursors.SizeNESW;

                    sizegrip = SizeGripRegion.LeftBottomCorner;

                }

                else if ((e.Location.X >= 5 && e.Location.X <= (base.ClientSize.Width - 5)) && (e.Location.Y == ClientSize.Height || e.Location.Y >= (base.ClientSize.Height - 5)))

                {

                    Cursor.Current = Cursors.SizeNS;

                    sizegrip = SizeGripRegion.Bottom;

                }

                else if ((e.Location.X == base.ClientSize.Width || e.Location.X >= (base.ClientSize.Width - 5)) && (e.Location.Y == ClientSize.Height || e.Location.Y >= (base.ClientSize.Height - 5)))

                {

                    Cursor.Current = Cursors.SizeNWSE;

                    sizegrip = SizeGripRegion.RightButtomCorner;

                }

                else if ((e.Location.X == base.ClientSize.Width || e.Location.X >= (base.ClientSize.Width - 5)) && (e.Location.Y >= 5 && e.Location.Y <= (base.ClientSize.Height - 5)))

                {

                    Cursor.Current = Cursors.SizeWE;

                    sizegrip = SizeGripRegion.Right;

                }

                else

                {

                    Cursor.Current = Cursors.Arrow;

                    sizegrip = SizeGripRegion.None;

                    LastLocation = Point.Empty;

                }

            }

            else {

                Point screenrect = base.RectangleToScreen(base.ClientRectangle).Location;

                switch (sizegrip) { 

                    case SizeGripRegion.Right:

                        base.Size = new Size(MousePosition.X - screenrect.X, this.Height);

                        Rectangle rect = RectangleToScreen(base.ClientRectangle);

                        Rectangle rect1 = RectangleToScreen(base.ParentForm.ClientRectangle);

                        if (rect.Right >= rect1.Right)

                        {

                            base.ParentForm.Size = new Size(base.ParentForm.Size.Width + (rect.Right - rect1.Right), base.ParentForm.Size.Height);

                        }

                        break;

                    case SizeGripRegion.Top:

                        //Console.WriteLine((PointToScreen(screenrect).Y - MousePosition.Y).ToString());

                        //Console.WriteLine( PointToClient(new Point(PointToScreen(this.Location).X, PointToScreen(this.Location).Y - MousePosition.Y)).ToString());

                       // base.Location = PointToClient(new Point(PointToScreen(this.Location).X, PointToScreen(this.Location).Y - MousePosition.Y));

                        // base.Size = new Size(this.Width, MousePosition.Y + screenrect.Y);

                        break;

                    case SizeGripRegion.Bottom:

                        base.Size = new Size(this.Width, MousePosition.Y - screenrect.Y);

                        Rectangle rect2 = RectangleToScreen(base.ClientRectangle);

                        Rectangle rect3 = RectangleToScreen(base.ParentForm.ClientRectangle);

                        if (rect2.Bottom >= rect3.Bottom) {

                            base.ParentForm.Size = new Size(base.ParentForm.Size.Width, base.ParentForm.Size.Height + ( rect2.Bottom - rect3.Bottom));

                        }

                        break;

                    case SizeGripRegion.Left:

                        //Todo


                        break;

                }

            }

        }

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint(e);

            if (resizeparentform && base.ParentForm != null)

            {

                this.Size = base.ParentForm.ClientRectangle.Size;

                this.Location = new Point(0, 0);

            }

        }

        private void Mouse_Up(object sender, MouseEventArgs e) {

            if (isClicked) {


                isClicked = false;

                LastLocation = Point.Empty;

            }

        }

        private void Mouse_Down(object sender, MouseEventArgs e) {

            if (e.Button == MouseButtons.Left && sizegrip != SizeGripRegion.None) {

                LastLocation = MousePosition;

                    isClicked = true;

            }

        }

        #endregion

    }

Mein Problem liegt momentan darin, das ich die Top Brechnung nicht hinbekomme.

Die Relevanten berechnungen hierfür sind im Mouse_Move Eventhandler.

Wenn isClicked True ist, wird geprüft, welche Seite des User Controls geändert werden soll.

Bei Right und Bottom war es relativ einfach da sich im endefekt nur die breite oder die Höhe verändert.

Bei Top hingegeben muss ich die Location, sowie die Höhe neu berechnen.

Ich muss eigentlich nur die Differenz der Letzten Y Kooridnate mit der aktuellen Y Koordinate als aktuellen Y Wert setzten und eben diese Differenz zusätzlich zu der aktuellen Höhe hinzufügen.

Aber irgenwie klappt das nicht.

Wäre Klassen wenn jemand einen Tipp hätte.

lg

Gateway

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.