My goal is to prevent partial or total hidden of my form when user move it through mouse. For instance, if my desktop resolution is 1024x768 the following will be "x/y" minimum/maximum value of coordinates for form location properties: 0, 1024 - form.width, 0, 768 - form.height.
I use some native APi to accomplish all this and all works fine, but often I notice a bad refresh (flickering) of my form (I'm managing all on form_move event).
It seems like SuspendLayout
method doesn't works fine, or else the form move event doesn't fire every pixel changed, but maybe more than one (see the below code to realize what I mean).
My code looks like this:
private void Form1_Move(object sender, EventArgs e)
{
this.SuspendLayout();
// Desktop Resolution (API utility)
int x = Desktop.GetXResolution();
int y = Desktop.GetYResolution();
// Taskbar Info (API utility)
Taskbar tb = new Taskbar();
int minX = 0;
int maxX = x - this.Width;
int minY = 0;
int maxY = y - this.Height;
if (!tb.AutoHide)
{
if (tb.Position != TaskbarPosition.Unknown && !tb.Size.IsEmpty)
{
if (tb.Position == TaskbarPosition.Top) minY += tb.Size.Height;
switch (tb.Position)
{
case TaskbarPosition.Top: minY = tb.Size.Height; break;
case TaskbarPosition.Bottom: maxY -= tb.Size.Height; break;
case TaskbarPosition.Left: minX = tb.Size.Width; break;
case TaskbarPosition.Right: maxX -= tb.Size.Width; break;
}
}
}
// Restore X Position
if (this.Location.X < minX) this.Location = new Point(minX, this.Location.Y);
if (this.Location.X > maxX) this.Location = new Point(maxX, this.Location.Y);
// Restore Y Poistion
if (this.Location.Y < minY) this.Location = new Point(this.Location.X, minY);
if (this.Location.Y > maxY) this.Location = new Point(this.Location.X, maxY);
this.ResumeLayout(false);
}
As I already said, often when I restore Location property my winForm take a flickering.
Any suggestion will be appreciated.
See Question&Answers more detail:os