關于C#窗體如何實作QQ的靠邊停靠的方法,有好多人問到,在這裡我把ta寫下,希望能幫到一些學弟學妹做UI的時候,想加一些效果的。當時我是這樣寫的,還有比我的代碼品質更好方法,大家不嫌麻煩的話,感興趣的可以去盡情的搜尋,畢竟360搜尋起來了0.0
下面試代碼,大家可以先看看,不懂的留言,或者你有更好的給我發個位址,我改正一下,謝謝大家!
代碼:
窗體中添加定時器控件。
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
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
this.TopMost = true;
Timer checkDockTimer = new Timer();
checkDockTimer.Tick += new EventHandler(Timer1_Tick);
checkDockTimer.Interval = 100;
checkDockTimer.Enabled = true;
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void Timer1_Tick(object sender, EventArgs e)
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top: this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left: this.Location = new Point(0, this.Location.Y);
case AnchorStyles.Right: this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
}
}
else
case AnchorStyles.Top: this.Location = new Point(this.Location.X, (this.Height - 2) * (-1)); break;
case AnchorStyles.Left: this.Location = new Point((-1) * (this.Width - 2), this.Location.Y); break;
case AnchorStyles.Right: this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y); break;
private void mStopAnhor()
{
if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else if (this.Left <= 0)
StopAanhor = AnchorStyles.Left;
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
StopAanhor = AnchorStyles.Right;
else
StopAanhor = AnchorStyles.None;
private void Form1_LocationChanged(object sender, EventArgs e)
this.mStopAnhor();
}
}
建立類FormAutoDock:
public class FormAutoDock
public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
if (DockableForm.WindowState != FormWindowState.Minimized)
_dockTimer.Interval = 1500;
if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
{
DockableForm.Top = 0;
}
else if (DockableForm.Left <= 0)
DockableForm.Left = 0;
else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
else
if (DockFormHeight > 0)
{
DockableForm.Height = DockFormHeight; DockFormHeight = 0;
}
else
if (DockFormHeight < 1)
DockFormHeight = DockableForm.Height;
if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
DockableForm.Top = 3 - DockableForm.Height;
if (DockableForm.Left <= 4)
DockableForm.Left = -5;
else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
else if (DockableForm.Left <= 4)
DockableForm.Left = 3 - DockableForm.Width;
else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
_dockTimer.Interval = 200;
}
本文轉自 吳雨聲 51CTO部落格,原文連結:http://blog.51cto.com/liangxiao/1209084,如需轉載請自行聯系原作者