The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Controls auf einem Formular bewegen

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
-

Posts: 1524
Nickname: nitronic
Registered: Jul, 2006

Norbert Eder works as a software architect.
Controls auf einem Formular bewegen Posted: Oct 10, 2007 12:31 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by -.
Original Post: Controls auf einem Formular bewegen
Feed Title: Norbert Eder - Living .NET
Feed URL: http://feeds.feedburner.com/NorbertEder-Livingnet
Feed Description: Copyright (c)2005, 2006 by Norbert Eder
Latest .NET Buzz Posts
Latest .NET Buzz Posts by -
Latest Posts From Norbert Eder - Living .NET

Advertisement
In Foren als auch meiner Inbox taucht immer wieder die Frage auf, wie man denn bewegbare Controls erstellen kann, um beispielsweise ein Diagramm zu zeichnen etc. Deshalb möchte ich hier ein ganz kleines Beispiel zeigen, wie in zwei Minuten eine bewegliche Basisklasse für bewegliche Controls erstellt werden kann. Und hier kommt schon der Sourcecode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MoveableControlDemo
{
    public partial class MoveableBaseControl 
        : UserControl
    {
        private bool _isMoving = false;
        private int _deltaX = 0;
        private int _deltaY = 0;

        public MoveableBaseControl()
        {
            InitializeComponent();
        }

        private void MoveableBaseControl_MouseDown(
            object sender, 
            MouseEventArgs e)
        {
            _isMoving = true;
            _deltaX = e.X;
            _deltaY = e.Y;
        }

        private void MoveableBaseControl_MouseUp(
            object sender, 
            MouseEventArgs e)
        {
            _isMoving = false;
        }

        private void MoveableBaseControl_MouseMove(
            object sender, 
            MouseEventArgs e)
        {
            if (_isMoving && e.Button == MouseButtons.Left)
            {
                this.Location = new Point(
                    this.Location.X + (e.X - _deltaX), 
                    this.Location.Y + (e.Y - _deltaY)
                    );
            }
        }
    }
}

Im Grunde passiert nicht viel. Es wird lediglich festgehalten wann sich das Control bewegen soll und wann nicht (MouseDown und MouseUp). Bei einem MouseMove wird anschließend die Position des Controls berechnet. Zu beachten ist hier nur, dass die MouseEventArgs die Position des Cursors innerhalb des Controls angibt und nicht auf Formular- bzw. Screen-Basis. Daher muss das Delta zum Rand des Controls beim MouseDown festgehalten werden, um dies später in die Berechnung einfließen zu lassen.

Dieses Control kann sozusagen als ein Basis-Control verwendet werden. Zum Test einfach in ein Projekt einbauen, auf ein Formular ziehen, Anwendung starten und mit der Maus über das Formular bewegen.

Read: Controls auf einem Formular bewegen

Topic: Zune Vs iPod Comparison Previous Topic   Next Topic Topic: Once in a Lifetime Experience: SteveB, Surface and Yours Truly

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use