PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [C#]Form bewegen



matze093
02.09.2010, 15:44
Ich habe einen Code geschrieben mit dem ermöglich werden soll, dass mit festgehaltener linken Maustaste die Form verschoben werden soll.
Mein Porblem:
Wenn ich die Form anlicke und versuche zu verschieben "flackert" bzw. beginnt zu springen.

Habt ihr vllt eine Lösung?

Hier mein Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool _MouseDown;
Point ptMousepos;
public Form1()
{
InitializeComponent();
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form_M ouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form_M ouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form_M ouseMove);
System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
this.Controls.Add(label1);
}
public void Form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ptMousepos = e.Location;
_MouseDown = true;
}
}
public void Form_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_MouseDown = false;
}
}
public void Form_MouseMove(object sender, MouseEventArgs e)
{
if (_MouseDown)
{

this.Left += e.X - ptMousepos.X;
this.Top += e.Y - ptMousepos.Y;
ptMousepos = e.Location;

}
}
}
}

inout
02.09.2010, 16:28
Bei diesem Code ist es kein Wunder, dass es flackert, da die Form ständig bei jeder kleinsten Mausbewegung komplett neu gezeichnet wird.

Du hast nun 2 Möglichkeiten:
Lege den Code aus dem MouseMove-Ereignis ins MouseUp-Ereignis, so dass nur einmal gezeichnet wird oder mach' es gleich richtig mit den nötigen APIs:



public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}

chilln
02.09.2010, 16:41
Das DoubleBuffered Property des Forms sollte notfalls auch das Flackern vermindern.